<?php
include("../../db.php");
header('Content-Type: application/json');

$session=$_REQUEST['session'];

$from = $_REQUEST['from'];
$to = $_REQUEST['to'];

$array = array();
$status=0;
$msg = "Unknown error";

$staff = $db->getRows("select idno,name,mobile,designation,photo,cl_count from staff where session='".$session."' and status=1 order by name");

if (count($staff) > 0){
    $status = 1;
    $msg = "Success";

    foreach ($staff as $key => $rowData) {

        if ($rowData['photo'] != ""){
            $staff[$key]['photo'] = URL_ROOT."media/photo/".$rowData['photo'];
        }

        $usedRow = $db->getRows("SELECT id FROM teacher_leave WHERE teacher_id = '$rowData[idno]' AND session = '$session' AND leave_status = '1' AND content_id != '0' ");
        $usedCount = ($rowData['cl_count'] - count($usedRow));

        $staff[$key]['cl_remaining'] = ($usedCount < 0) ? 0 : $usedCount;
        $staff[$key]['approved_count'] = count($usedRow);

        $date_array = array();
        $leave_dates = $db->getRows("SELECT date FROM teacher_leave WHERE teacher_id = '$rowData[idno]' AND session = '$session' AND leave_status = '1' AND content_id != '0' AND DATE(date) BETWEEN '$from' AND '$to'");

        foreach ($leave_dates as $model) {
            //$date_array[] = $model['date'];
            $date_array[] = date("d-m-Y", strtotime($model['date']));
        }

        $staff[$key]['leave_dates'] = $date_array;
    }

    $array = $staff;

}

echo json_encode(array("status"=>$status, "data"=>$array, "msg"=>$msg));