<?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'];

$period_param = $idata['period'];

//  $idno=mysql_real_escape_string($idno);
// 	 $session=mysql_real_escape_string($session);
// 	 $school_id=mysql_real_escape_string($school_id);
// 	 $date=mysql_real_escape_string($date);
// 	 $type=mysql_real_escape_string($type);

$mnth = date("F");


//$_POST["mobile"]='7827018991';


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

    $aryData = array(

        "idno" => $x_value,
        "school_id" => $school_id,
        "session" => $session,
        "date" => $date,
        "month" => $mnth,
        "status" => 0,
        "added_by" => $added_by,
        "type" => 2,
        "period" => json_encode(array($period_param))

    );
//echo $x_value;


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

    if (count($check_data) > 0) {

        if ($period_param != null) {
            $att_row_id = $check_data[0]["id"];
            $period_array = json_decode($check_data[0]["period"]);
            $period_array[] = $period_param;
            $flgUp = $db->updateAry("attandenc", array("period" => json_encode($period_array)), "where id='$att_row_id'");
        }

    } else {

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


    }

}

if ($delete_entry == '0') {


} else {

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

        $type = 2;

        //////////


        $res_qry = $db->getRow("select period from attandenc where idno='$p_value' and date='$date' and type='$type' and status='0'");
        $period_json = json_decode($res_qry["period"], true);

        //remove given period

        /*foreach (array_keys($period_json, $period_param) as $key) {
            unset($period_json[$key]);
        }*/

        if (($key = array_search($period_param, $period_json)) !== false) {
            unset($period_json[$key]);
        }


        if (empty($period_json)){
            //delete whole row
            $arr = $db->delete("attandenc", " where idno='$p_value' and date='$date' and type='$type' and status='0'");
        }else{
            //update some data is avail
            $remove_and_update = $db->updateAry("attandenc", array("period" => json_encode(array_values($period_json))), " where idno='$p_value' and date='$date' and type='$type' and status='0'");
        }

        /////

        if ($arr > 0 || $remove_and_update) {
            $msg = "Record updated successfully.!";
        }
    }

}


//echo $inser_complain; 

if ($insert_complain > 0 || $flgUp) {

    $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..!'));
}
