<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/AcademicYear.php';
$academic_year = new AcademicYear();

$academic_years = $academic_year->read_all_academic_years();

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

if(isset($_POST['add_academic_year'])){
    $academic_year_name = $_POST['academic_year_name'];
    $data = array(
        "academic_year_name"=>$academic_year_name
    );
    $success = $academic_year->add_academic_year($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_academic_year'])){
    $academic_year_id = $_POST['academic_year_id'];
    $academic_year_name = $_POST['academic_year_name'];
    $data = array(
        "academic_year_name"=>$academic_year_name
    );
    $key = array(
        "academic_year_id"=>$academic_year_id
    );
    $success = $academic_year->update_academic_year($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_academic_year'])){
    $academic_year_id = $_POST['academic_year_id'];
    $data = $academic_year->remove_academic_year($academic_year_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}