<?php 
include("../db.php");

header('Content-Type: application/json');

        $idno       =   $_GET['idno'];
		$class      =   $_GET['class'];
		$monthnum   =   $_GET['month'];
		$session    =   $_GET['session'];
		$monthname  =   date('F', mktime(0, 0, 0, $monthnum, 1));
		$currentyear =  date("Y");

		if($monthnum > 3){
		    $year = ($currentyear-1);
		}
		else {
		    $year = $currentyear;
		}

        $holidayAry = array();
        $holidays=$db->getRows("SELECT date FROM holiday WHERE session='".$session."' AND month='$monthname' AND class='$class' ");
        foreach($holidays as $h){
            array_push($holidayAry, $h['date']);
        }

        $absentAry = array();
        $absent = $db->getRows("SELECT date FROM attandenc WHERE idno='$idno' AND month='$monthname' AND session='".$session."' ");
        foreach($absent as $a){
            array_push($absentAry, $a['date']);
        }
        
        $total_days=cal_days_in_month(CAL_GREGORIAN, $monthnum, $year);
		function total_sun($monthnum,$year)
        {
            $sundays=0;
            $total_days=cal_days_in_month(CAL_GREGORIAN, $monthnum, $year);
            for($i=1;$i<=$total_days;$i++)
            if(date('N',strtotime($year.'-'.$monthnum.'-'.$i))==7)
            $sundays++;
            return $sundays;
        }
        $totalsundays=total_sun($monthnum, $year);

        $totalholidays=count($holidays);
        
        $totalworkingdays= ($total_days - ($totalsundays + $totalholidays));

        $array['holidays'] = $holidayAry;
        $array['absents'] = $absentAry;
        $array['workingdays'] = $totalworkingdays;

        echo json_encode($array);


/*
function showCurrentMonth($current_Month, $year, $holidayAry, $absentAry)
{
$date = mktime(12, 0, 0, $current_Month, 1, $year);
$numberOfDays =cal_days_in_month(CAL_GREGORIAN,$current_Month, $year);
$offset = date("w", $date);
$row_number = 1;
// time to draw the month header
echo "<table style='color:blue; border:1px solid blue; width:500px; height:300px;' border='1'><br/>";
echo "<tr style='font-size:30px;'><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr> <tr>";
// this will print the additional td record in case the month is not starting with the Sunday.
for($i = 1; $i <= $offset; $i++)
{
echo "<td></td>";
}
//  this will print the number of days.
for($day = 1; $day <= $numberOfDays; $day++)
{
if( ($day + $offset - 1) % 7 == 0 && $day != 1)
{
echo "</tr> <tr>";
$row_number++;
}

$date1 = date_create("$year-$current_Month-$day");
$chkdate = date_format($date1,"Y-m-d");

if(in_array("$chkdate", $holidayAry)){
    echo "<td style='color:green;font-weight:bold;font-size:25px;'>$day <span style='color:green;float:right;margin-bottom:0px;font-weight:normal;'>H</span></td>";
}
else if(in_array("$chkdate", $absentAry)){
    echo "<td style='color:red;font-weight:bold;font-size:25px;'>$day <span style='color:red;float:right;margin-bottom:0px;font-weight:normal;'>A</span></td>";    
}
else  {
echo "<td style='font-weight:bold;font-size:25px;'>" . $day . "</td>";
}

}
while( ($day + $offset) <= $row_number * 7)
{
echo "<td></td>";
$day++;
}
echo "</tr></table>";
}

showCurrentMonth($monthnum, $year, $holidayAry, $absentAry);
*/

?>