<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/Student.php';
require_once '../Model/Course.php';
require_once '../Model/Program.php';
require_once '../Model/AcademicYear.php';
$student = new Student();
$course = new Course();
$program = new Program();
$academic_year = new AcademicYear();

$students = $student->read_all_students();

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

if(isset($_POST['add_student'])){
    $student_sex = $_POST['student_sex'];
    $student_name = $_POST['student_name'];
    $student_phone = $_POST['student_phone'];
    $student_email = $_POST['student_email'];
    $student_created_at = $_POST['student_created_at'];
    $student_digital_address = $_POST['student_digital_address'];
    $program_name = $_POST['program_name'];
    $academic_year_name = $_POST['academic_year_name'];
    $academic_year_details = $academic_year->get_academic_year_by_name($academic_year_name);
    $student_academic_year = $academic_year_details[0]['academic_year_id'];
    $student_course = '';
    $program_detail = $program->get_program_by_name($program_name);
    if($program_detail){
        $student_program = $program_detail[0]['program_id'];
    }
    $data = array(
        "student_sex"=>$student_sex,
        "student_name"=>$student_name,
        "student_phone"=>$student_phone,
        "student_email"=>$student_email,
        "student_digital_address"=>$student_digital_address,
        "student_created_at"=>$student_created_at,
        "student_course"=>$student_program,
        "student_academic_year"=>$student_academic_year
    );
    $success = $student->add_student($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_student'])){
    $student_id = $_POST['student_id'];
    $student_sex = $_POST['student_sex'];
    $student_name = $_POST['student_name'];
    $student_phone = $_POST['student_phone'];
    $student_email = $_POST['student_email'];
    $student_created_at = $_POST['student_created_at'];
    $student_digital_address = $_POST['student_digital_address'];
    $program_name = $_POST['program_name'];
    $student_course = '';
   $program_detail = $program->get_program_by_name($program_name);
    $academic_year_name = $_POST['academic_year_name'];
    $academic_year_details = $academic_year->get_academic_year_by_name($academic_year_name);
    $student_academic_year = $academic_year_details[0]['academic_year_id'];
    
    if($program_detail){
        $student_program = $program_detail[0]['program_id'];
    }
    $data = array(
        "student_sex"=>$student_sex,
        "student_name"=>$student_name,
        "student_phone"=>$student_phone,
        "student_email"=>$student_email,
        "student_digital_address"=>$student_digital_address,
        "student_created_at"=>$student_created_at,
        "student_course"=>$student_program,
        "student_academic_year"=>$student_academic_year
    );
    $key = array(
        "student_id"=>$student_id
    );
    $success = $student->update_student($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_student'])){
    $student_id = $_POST['student_id'];
    $data = $student->remove_student($student_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}