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

    $idno   =   $_REQUEST['idno'];
	$month  =   $_GET["month"];

    $session    = $_REQUEST['session'];

    $years = explode("-",$session);

	if($month < 4){
	    $y = $years[1];
	} else {
	    $y = $years[0];
	}

	$mn=date('m',strtotime($y."-".$month."-1"));

	$maxDays=date('t',strtotime($y."-".$mn."-1"));

    if(date('m')==$_GET['month']){
        $maxDays = date('d');
    }

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

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

        $totalpresent = 0;
        $totalabsent = 0;
    	$totalholidays = 0;

        for($i=1; $i<= $maxDays;$i++){

            $fdate  = $y."-".$mn."-".$i;
            $cdate  = $i+1;
            $todate = $y."-".$mn."-".$cdate;
            
            $result = $db->getRow("Select * from attendance_new where teacher_id='$aryList[idno]' and (timestamp>='$fdate' and timestamp<='$todate') and session='$session' and att_type='IN' ");

            $out = "";
            if(count($result)>1){
                $out = $db->getVal("select timestamp from attendance_new where teacher_id='$aryList[idno]' and (timestamp>='$fdate' and timestamp<='$todate') and session='$session' and att_type='OUT' ");
            }

            $day=date('D',strtotime($y."-".$mn."-".$i));
            
            $ColorClass="red";
            $msg = "Absent";
			$chkHoli=$db->getVal("select status from staff_holiday where date='".$y."-".$mn."-".$i."' and teacher='".$aryList['idno']."'");

            $chkAtt=$db->getVal("SELECT id FROM attendance_new WHERE cast(timestamp as date) = '".$y."-".$mn."-".$i."' and teacher_id='".$aryList["idno"]."' and att_type='IN' ");

			if(strtolower($day)=="sun") {$ColorClass="red"; $msg="Sunday";}
			elseif($chkHoli===1) {$ColorClass="red";  $msg="Holiday"; $totalholidays+=1;}
			elseif($chkAtt > 0) {$ColorClass="";  $msg="Present"; $totalpresent+=1;}

            if($msg=="Absent"){
			    $totalabsent+=1;
			}

            array_push($staffArray, [
                                    "Date"      =>  $i,
                                    "In"        =>  "$result[timestamp]",
                                    "Out"       =>  "$out",
                                    "Remarks"   =>  $msg,
                                ]);

    }

    $array['data'] = $staffArray;

   echo json_encode(array("status"=>"$status", "present"=>$totalpresent, "absent"=>$totalabsent, "data"=>$staffArray));
?>