<?php
require "commonincludefiles.php";
require "data/sendingsms_studentpresant.php";
if ($_SESSION["VedantnrCollegeId"] != "" && isset($_SESSION["VedantnrCollegeId"])) {
    $objStudentDetailMgmt = new StudentDetailMgmt;
    $objtransportfacilityMgmt = new transportfacilityMgmt;
    $transport_id = $_REQUEST['ID'];
    $transport_facility = $_REQUEST['transport_facility'];
    $updown_strdate = $_REQUEST['updown_strdate'];
    $updown_enddate = $_REQUEST['updown_enddate'];
    $hostel_admdate = $_REQUEST['hostel_admdate'];
    $hostel_candate = $_REQUEST['hostel_candate'];
    $year_id = $_REQUEST['year_hidden'];
    $student_id = $_REQUEST['student_id'];

    $condition = " AND transport_facility = $transport_facility AND student_id = $student_id ";
    if ($transport_id != "") {
        $condition .= " AND transport_id = $transport_id ";
    }
    $order_field = "transport_id";
    $order_type = "DESC";
    $limit = 1;
    $start = 0;
    $objtransportfacilityMgmt->student_id = $student_id;
    $out = $objtransportfacilityMgmt->getTransportRecord($limit, $start, $condition, $order_field, $order_type);
    if (dbRows($out) > 0) {
        $array = dbArray($out);
        $transport_id = $array['transport_id'];
        $objtransportfacilityMgmt->transport_facility = $transport_facility;
        $objtransportfacilityMgmt->transport_id = $transport_id;
        $objtransportfacilityMgmt->year_id = $year_id;
        if ($transport_facility == 1) {
            $objtransportfacilityMgmt->hostel_admdate = $hostel_admdate;
            $objtransportfacilityMgmt->hostel_candate = $hostel_candate;
        } else {
            $objtransportfacilityMgmt->updown_strdate = $updown_strdate;
            $objtransportfacilityMgmt->updown_enddate = $updown_enddate;
        }
        $objtransportfacilityMgmt->Edit();
    } else {
        $transport_facility1 = $transport_facility == 1 ? 0 : 1;
        $condition = " AND transport_facility = $transport_facility1 AND  student_id = $student_id ";
        if ($transport_id != "") {
            $condition .= " AND transport_id = $transport_id ";
        }
        $out = $objtransportfacilityMgmt->getTransportRecord($limit, $start, $condition, $order_field, $order_type);
        if (dbRows($out) > 0) {
            $array = dbArray($out);
            $objtransportfacilityMgmt->transport_id = $array['transport_id'];
            $objtransportfacilityMgmt->transport_facility = $transport_facility1;

            $objtransportfacilityMgmt->year_id = $array['year_id'];
            if ($transport_facility1 == 1) {
                $objtransportfacilityMgmt->hostel_admdate = $hostel_admdate;
                $objtransportfacilityMgmt->hostel_candate = $hostel_candate;
            } else {
                $objtransportfacilityMgmt->updown_strdate = $updown_strdate;
                $objtransportfacilityMgmt->updown_enddate = $updown_enddate;
            }
            $objtransportfacilityMgmt->Edit();
        }
        $objtransportfacilityMgmt = new transportfacilityMgmt;
        $objtransportfacilityMgmt->year_id = $year_id;
        $objtransportfacilityMgmt->transport_facility = $transport_facility;
        $objtransportfacilityMgmt->student_id = $student_id;
        if ($transport_facility == 1 && $hostel_admdate != "") {

            $objtransportfacilityMgmt->hostel_admdate = $hostel_admdate;
            $objtransportfacilityMgmt->hostel_candate = $hostel_candate;
            $objtransportfacilityMgmt->Add();

        } else if ($transport_facility == 0 && $updown_strdate != "") {
            $objtransportfacilityMgmt->updown_strdate = $updown_strdate;
            $objtransportfacilityMgmt->updown_enddate = $updown_enddate;
            $objtransportfacilityMgmt->Add();
        }
    }

    echo json_encode(
        array(
            "success" => true,
            "msg" => "Details Updated Successfully"
        )
    );

}
