<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/Program.php';
$program = new Program();

$programs = $program->read_all_programs();

if(isset($_GET['all_programs'])){
    echo json_encode(
        array("data"=>$programs)
    ); 
}

if(isset($_POST['add_program'])){
    $program_name = $_POST['program_name'];
    $program_duration = $_POST['program_duration'];
    $program_cost = $_POST['program_cost'];
    $data = array(
        "program_name"=>$program_name,
        "program_duration"=>$program_duration,
        "program_cost"=>$program_cost
    );
    
    $success = $program->add_program($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_program'])){
    $program_id = $_POST['program_id'];
    $program_name = $_POST['program_name'];
    $program_duration = $_POST['program_duration'];
    $program_cost = $_POST['program_cost'];
    $data = array(
        "program_name"=>$program_name,
        "program_duration"=>$program_duration,
        "program_cost"=>$program_cost
    );
    $key = array(
        "program_id"=>$program_id
    );
    $success = $program->update_program($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_program'])){
    $program_id = $_POST['program_id'];
    $data = $program->remove_program($program_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}