<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

    include('../../config/config.php');

    $abba_campus_id = $_POST['abba_campus_id'];
    $abba_student_id = $_POST['abba_student_id'];
    $abba_get_student_first_name = mysqli_real_escape_string($link,$_POST['abba_get_student_first_name']);
    $abba_get_student_middle_name = mysqli_real_escape_string($link,$_POST['abba_get_student_middle_name']);
    $abba_get_student_last_name = mysqli_real_escape_string($link,$_POST['abba_get_student_last_name']);
    $studentphonefull = $_POST['studentphonefull'];
    $abba_get_student_email = mysqli_real_escape_string($link,$_POST['abba_get_student_email']);
    $abba_get_student_disability = mysqli_real_escape_string($link,$_POST['abba_get_student_disability']);
    $abba_get_student_alergy = mysqli_real_escape_string($link,$_POST['abba_get_student_alergy']);
    $abba_get_student_gender = $_POST['abba_get_student_gender'];
    $abba_get_student_dob = $_POST['abba_get_student_dob'];
    $abba_get_student_blood_group = $_POST['abba_get_student_blood_group'];
    $abba_get_student_religion = mysqli_real_escape_string($link,$_POST['abba_get_student_religion']);
    $abba_display_student_country = $_POST['abba_display_student_country'];
    $abba_display_student_state = $_POST['abba_display_student_state'];
    $abba_display_student_lga = $_POST['abba_display_student_lga'];
    $abba_get_student_city = mysqli_real_escape_string($link,$_POST['abba_get_student_city']);
    $abba_get_student_address = mysqli_real_escape_string($link,$_POST['abba_get_student_address']);
    $abba_display_create_student_class = $_POST['abba_display_create_student_class'];
    $abba_get_student_session = $_POST['abba_get_student_session'];
    $abba_get_student_doa = $_POST['abba_get_student_doa'];
    $abba_get_student_reg_number = mysqli_real_escape_string($link,$_POST['abba_get_student_reg_number']);
    $abba_get_student_password = mysqli_real_escape_string($link,md5($_POST['abba_get_student_password']));
    $abba_get_student_type = $_POST['abba_get_student_type'];
    $abba_get_student_parent = $_POST['abba_get_student_parent'];

    $abba_sql_insert_student = ("UPDATE `student` SET `ParentID`='$abba_get_student_parent',`SecParentID`='0',`CampusID`='$abba_campus_id',`StudentTypeBoardingOrDay`='$abba_get_student_type',`PrefectID`='0',`StudentFirstName`='$abba_get_student_first_name',`StudentMiddleName`='$abba_get_student_middle_name',`StudentLastName`='$abba_get_student_last_name',`StudentDOB`='$abba_get_student_dob',`StudentGender`='$abba_get_student_gender',`StudentPhone`='$studentphonefull',`StudentEmail`='$abba_get_student_email',`WalletBalance`='0',`WalletBank`='0',`WalletAccountName`='0',`WalletAccountNumber`='0',`WalletAccountReference`='0',`StudentCountry`='$abba_display_student_country',`StudentState`='$abba_display_student_state',`StudentLga`='$abba_display_student_lga',`StudentCity`='$abba_get_student_city',`StudentAddress`='$abba_get_student_address',`StudentCountryAddress`='0',`StudentDOA`='$abba_get_student_doa',`StudentBloodGroup`='$abba_get_student_blood_group',`StudentAlergies`='$abba_get_student_alergy',`StudentDisabilities`='$abba_get_student_disability',`StudentReligion`='$abba_get_student_religion',`StudentTrashStatus`='0',`AlumniStatus`='0',`tagID`='0',`Lang_Val`='0',`OTP`='0' WHERE StudentID = '$abba_student_id'");
    $abba_result_insert_student = mysqli_query($link, $abba_sql_insert_student);
    
    if($abba_result_insert_student == true)
    {
        
        $sql_classordepartmentstudentallocation = ("SELECT * FROM `classordepartmentstudentallocation` WHERE CampusID ='$abba_campus_id' AND StudentID = '$abba_student_id' AND Session = '$abba_get_student_session' AND `ClassOrDepartmentID`='$abba_display_create_student_class'");
        $result_classordepartmentstudentallocation = mysqli_query($link, $sql_classordepartmentstudentallocation);
        $row_classordepartmentstudentallocation = mysqli_fetch_assoc($result_classordepartmentstudentallocation);
        $row_cnt_classordepartmentstudentallocation = mysqli_num_rows($result_classordepartmentstudentallocation);
        
        if($row_cnt_classordepartmentstudentallocation > 0){
            echo 'done';
        }
        else
        {
            
            $sql_classordepartmentstudentallocation_session = ("SELECT * FROM `classordepartmentstudentallocation` WHERE CampusID ='$abba_campus_id' AND StudentID = '$abba_student_id' AND Session = '$abba_get_student_session'");
            $result_classordepartmentstudentallocation_session = mysqli_query($link, $sql_classordepartmentstudentallocation_session);
            $row_classordepartmentstudentallocation_session = mysqli_fetch_assoc($result_classordepartmentstudentallocation_session);
            $row_cnt_classordepartmentstudentallocation_session = mysqli_num_rows($result_classordepartmentstudentallocation_session);
            
            if($row_cnt_classordepartmentstudentallocation_session > 0){
                
                $abba_assign_student_to_class = "UPDATE `classordepartmentstudentallocation` SET `ClassOrDepartmentID`='$abba_display_create_student_class' WHERE StudentID = '$abba_student_id' AND `Session`='$abba_get_student_session'";
                
                if(mysqli_query($link, $abba_assign_student_to_class))
                {
                    echo 1;
                }
                else{
                    echo 0;
                }
            }
            else
            {
                
                $sql_classordepartmentstudentallocation_session = ("SELECT * FROM `classordepartmentstudentallocation` WHERE CampusID ='$abba_campus_id' AND StudentID = '$abba_student_id' AND `ClassOrDepartmentID`='$abba_display_create_student_class'");
                $result_classordepartmentstudentallocation_session = mysqli_query($link, $sql_classordepartmentstudentallocation_session);
                $row_classordepartmentstudentallocation_session = mysqli_fetch_assoc($result_classordepartmentstudentallocation_session);
                $row_cnt_classordepartmentstudentallocation_session = mysqli_num_rows($result_classordepartmentstudentallocation_session);
                
                if($row_cnt_classordepartmentstudentallocation_session > 0){
                    
                    $abba_assign_student_to_class = "UPDATE `classordepartmentstudentallocation` SET `Session`='$abba_get_student_session' WHERE StudentID = '$abba_student_id' AND `ClassOrDepartmentID`='$abba_display_create_student_class'";
                    
                    if(mysqli_query($link, $abba_assign_student_to_class))
                    {
                        echo 1;
                    }
                    else{
                        echo 0;
                    }
                }
                else
                {
                    $abba_assign_student_to_class = "INSERT INTO `classordepartmentstudentallocation`( `CampusID`, `ClassOrDepartmentID`, `Session`, `StudentID`) VALUES ('$abba_campus_id', '$abba_display_create_student_class', '$abba_get_student_session', '$abba_student_id')";
                    
                    if(mysqli_query($link, $abba_assign_student_to_class))
                    {
                        echo 1;
                    }
                    else{
                        echo 0;
                    }
                }
            }
        }
        
    }
    else
    {
        echo 0;

        // echo "Error: " . $sql . "<br>" . mysqli_error($link);
    }
?>