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

/*
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
*/

$session = $_REQUEST['session'];

// $session = mysql_real_escape_string($session);
// $arr=$db->getRows("select * from assignment where added_by='".$Added_by."' and date='$date' and session='$session' and school_id= '$school_id' and status='1'");

$sql = $db->getRows("select * from teacher_leave where  session='$session' ORDER BY id DESC");
// $result = mysql_query($sql);
// while ($row = mysql_fetch_assoc($result))

$array_store = array();
//$jsonn = array();

foreach ($sql as $row) {

    $name = $db->getVal("select name from staff where idno='" . $row['teacher_id'] . "' ");
    $mobile = $db->getVal("select mobile from staff where idno='" . $row['teacher_id'] . "' ");

    $experience = $db->getVal("select experience from staff where idno='" . $row['teacher_id'] . "' ");

    $designation = $db->getVal("select designation from staff where idno='" . $row['teacher_id'] . "' ");

    $photo = $db->getVal("select photo from staff where idno='" . $row['teacher_id'] . "' ");

    $ct_id = $row['content_id'];

    $cl_count = $db->getVal("SELECT cl_count FROM staff WHERE idno = '$row[teacher_id]'");
    $usedRow = $db->getRows("SELECT id,is_half_day FROM teacher_leave WHERE teacher_id = '$row[teacher_id]' AND session = '$session' AND leave_status = '1' AND content_id != '0' ");

    $used_leave = 0;
    foreach ($usedRow as $single_row) {
        if ($single_row['is_half_day'] == 1){
            $used_leave = $used_leave + 0.5;
        }else{
            $used_leave = $used_leave + 1;
        }
    }

    $cl_remaining = ($cl_count - $used_leave);

  //  $date = new DateTime($row['date']);
  //  $new_date = $date->format('d-m-Y');

    $tmp = array("id" => $row['id'], "date" => $row['date'], "leave_status" => $row['leave_status'], "is_half_day" => $row['is_half_day']);

    if ($ct_id == 0) {

        $jsonn[] = array("id" => $row['id'], "teacher_id" => $row['teacher_id'], "name" => $name, "mobile" => $mobile,
            "photo" => "media/photo/" . $photo, "subject" => $experience, "designation" => $designation, "subject" => $row['subject'],
            "details" => $row['details'], "date" => $row['date'], "image" => "uploads/assignment/" . $row['image'],
            "leave_status" => $row['leave_status'], "remarks" => $row['remarks'], "leave_date" => /*array($new_date)*/array($tmp), "content_id"=>$ct_id,
            "cl_count" => $cl_count, "cl_remaining" => $cl_remaining);
    } else {
        if (array_key_exists($ct_id, $array_store)) {
            $array_store[$ct_id]['leave_date'][] = $tmp; //$new_date;
        } else {
            $array_store[$ct_id] = array("id" => $row['id'], "teacher_id" => $row['teacher_id'], "name" => $name, "mobile" => $mobile,
                "photo" => "media/photo/" . $photo, "subject" => $experience, "designation" => $designation, "subject" => $row['subject'],
                "details" => $row['details'], "date" => $row['date'], "image" => "uploads/assignment/" . $row['image'],
                "leave_status" => $row['leave_status'], "remarks" => $row['remarks'], "leave_date" => array(/*$new_date*/$tmp), "content_id"=>$ct_id,
                "cl_count" => $cl_count, "cl_remaining" => $cl_remaining);
        }
    }


}

foreach ($array_store as $item) {
    $jsonn[] = $item;
}

usort($jsonn, function ($a,$b){ return $a['id'] < $b['id']; });

$json['status'] = 1;
$json['msg'] = "successful";
$json['data'] = $jsonn;

if (is_array($jsonn) && count($jsonn) > 0) {
    echo json_encode($json);
} else {
    echo json_encode(array('status' => 0, 'msg' => 'data not found..!'));
}