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

    $fdate  =   $_GET["fdate"];
	$tdate  =   $_GET["tdate"];

	$tilldate = date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $_GET['tdate']) ) ));
	$where.=" (date between '$fdate' and '$tilldate') ";	
	$whereface.= " (timestamp between '$fdate' and '$tdate') ";

    $session    = $_REQUEST['session'];

	$array = array();
	$status=1;
	
    $staffArray = array();

    $currentmonth = date('m');

    $total_days_month = 0;
    $totalsundays = 0;

    $start = new DateTime("$fdate");
    $end = new DateTime("$tdate");
    $days = $start->diff($end, true)->days;

    $totalsundays = intval($days / 7) + ($start->format('N') + $days % 7 >= 7);

    // $holidays=$db->getRows("SELECT COUNT(*) FROM staff_holiday WHERE session='$session' AND $where  GROUP BY id HAVING COUNT(*) >= '1' ");

    // $totalholidays = count($holidays);

    // $totalworkingdays= ($days - ($totalsundays + $totalholidays));

    $records = $db->getRows("SELECT * FROM staff WHERE session='$session' and status='1' order by name ");

    foreach($records as $rec){
        $totalholidays=0; 

        $holidays=$db->getRows("SELECT COUNT(*) FROM staff_holiday WHERE teacher='$rec[idno]' and session='$session' AND $where  GROUP BY id HAVING COUNT(*) >= '1' ");
        // $holidays=$db->getRows("SELECT COUNT(*) FROM staff_holiday WHERE teacher='$stu[idno]' and session='".$_SESSION["admin"]["session"]."' AND $where  GROUP BY id HAVING COUNT(*) >= '1' ");
        $totalholidays = count($holidays);

        $totalworkingdays = ($days - ($totalsundays + $totalholidays));

        $result1 = $db->getRows("SELECT COUNT(id) as present FROM attendance_new WHERE teacher_id='$rec[idno]' and att_type='IN' AND $whereface AND session='$session' ");

        $totalabsent    =   0;

        foreach ($result1 as $row2) {
            $totalabsent = $totalworkingdays-$row2['present'];

            array_push($staffArray, [ 
                                    "idno"          =>  $rec['idno'],
                                    "name"          =>  $rec['name'],
                                    "fname"         =>  $rec['fname'],
                                    "present"       =>  $row2['present'],
                                    "absent"        =>  $totalabsent,
                                    "working_days"  =>  $totalworkingdays,
                                    "holidays"      =>  $totalholidays
                                ]);
        }
    }


   // $array['data'] = $staffArray;

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