<?php
include("../../db.php");
error_reporting(E_ALL);
header("Access-Control-Allow-Origin: * ");

$abc = $_POST['json'];

$idata = json_decode($abc, true);
$session = $idata['session'];
$school_id = $idata['school_id'];
$date = $idata['date'];
$idno = $idata['idno'];
$delete_entry = $idata['delete_entry'];
$added_by = $idata['added_by'];


$bus = $idata['bus'];

$mnth = date("F");

foreach ($idno as $x => $x_value) {

    $class = $db->getVal("select class from student where idno='$x_value' ");;
    $sec   = $db->getVal("select section from student where idno='$x_value' ");;

    $aryData = array(

        "date" => $date,
        "idno" => $x_value,
        "class" => $class,
        "section" => $sec,
        "bus" => $bus,
        "added_by" => $added_by,
        "session" => $session,
        "type" => 2

    );
//echo $x_value;


    $check_data = $db->getRows("select * from bus_attendance where idno='$x_value' and date='$date' and type='2'");

    if (count($check_data) > 0) {


    } else {

        $insert_complain = $db->insertAry("bus_attendance", $aryData);


    }

}

if ($delete_entry == '0') {


} else {

    foreach ($delete_entry as $p => $p_value) {

        $type = 2;

        $arr = $db->delete("bus_attendance", " where idno='$p_value' and date='$date' and type='$type' ");

        if ($arr > 0) {
            $msg = "Delete successfully.!";
        }
    }

}


//echo $inser_complain;

if ($insert_complain > 0) {

    $msg = "Attendance Added successfully.!";


}


if ($msg != "") {
    echo json_encode(array('status' => 1, 'msg' => 'Done', "data" => $msg));

} else {
    echo json_encode(array('status' => 0, 'msg' => 'Attendance not Added. Please try again later..!'));
}
