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

	$idno=$_REQUEST['idno'];
	$session=$_REQUEST['session'];
	$school_id=$_REQUEST['school_id'];
	$idno=mysql_real_escape_string($idno);
	$session=mysql_real_escape_string($session);
	$school_id=mysql_real_escape_string($school_id);
	
	
function total_sundays($month,$year)
{
$sundays=0;
    $m=date("m");
    $d=date("d");
    if($month==$m){
        $total_days=$d;
      }else{
        $total_days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
      }
for($i=1;$i<=$total_days;$i++)
if(date('N',strtotime($year.'-'.$month.'-'.$i))==7)
$sundays++;
return $sundays;
}

function total_sun($month,$year)
{
    $sundays=0;
    $total_days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
    for($i=1;$i<=$total_days;$i++)
    if(date('N',strtotime($year.'-'.$month.'-'.$i))==7)
    $sundays++;
    return $sundays;
}
//echo total_sun(7,2019);	

	$year=explode("-",$session);
    	$y=$year[0];
		$y1=$year[1];
//echo $y;exit;
$monthsList = array(
    "1"=>'April',
    "2"=>'May',
    "3"=>'June',
    "4"=>'July ',
    "5"=>'August',
    "6"=>'September',
    "7"=>'October',
    "8"=>'November',
    "9"=>'December',
	"10"=>'January',
    "11"=>'February',
    "12"=>'March'
    
);

//print_r($monthsList);die;	
$ij=0;$data=array();$a=0;

	foreach ($monthsList as $key => $value){$ij++;
		$m=$key;
		//$month = date("F", mktime(0, 0, 0, $m));
		$month=$value;
		
		$mn = date('m',strtotime($y."-".$month."-1"));
		//echo $mn;exit;
		 $current_month =  date("M");
		if($month==$current_month){
		$maxDays = date("d");
		}else{
		$maxDays = cal_days_in_month(CAL_GREGORIAN, $mn, $y);
		}
		
		//$maxDays = cal_days_in_month(CAL_GREGORIAN, $mn, $y);
		$data[$ij]["order"]=$ij;
		
		$data[$ij]["month"]=$month;
		
		//echo $ij.'<br>';
		
		//echo $y1;
		//echo '<br>';
		
               if($m<10)$data[$ij]["year"]=$y;
		else $data[$ij]["year"]=$y1;
		
		
		if($ij<10)
		{
		$y=$year[0];
		}
		else {$y=$year[1];}
		//echo $y1; die;
		//$j=0;
		/*for($d=1; $d<=31; $d++){
			
 		   $time=mktime(12, 0, 0, 2, $d, $y);          
   		 
			 if(date('m', $time)==2)       
    	   
			    $list[]=date('Y-m-d-D', $time);
		}
echo "<pre>";
print_r($list);
echo "</pre>";die();*/

//echo $m; die;
		$data1=array();$pr=0;
		for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i));
		
		$chkHoli=$db->getVal("select status from holiday where school_id='".$school_id."' and date='".$y."-".$mn."-".$i."' and session='".$session."' and month='".$month."'");
		$chkAtt=$db->getVal("select status from attandenc where school_id='".$school_id."' and date='".$y."-".$mn."-".$i."' and type='2' and idno='".$idno."' and session='".$session."' and month='".$month."'");
		
		$chkLeave=$db->getVal("select status from attandenc where school_id='".$school_id."' and date='".$y."-".$mn."-".$i."' and type='3' and idno='".$idno."' and session='".$session."' and month='".$month."'");
					//echo $db->getLastQuery();exit;
					if(strtolower($day)=="sun")  $data1[$i]['att']="H"; 
					
					elseif($chkHoli=="1") $data1[$i]['att']="H"; 
					
					elseif($chkAtt=="0") $data1[$i]['att']="A"; 
					
					elseif($chkLeave=="0") $data1[$i]['att']="L"; 
					
					else $pr++; $to_p+= $data1[$i]['att']="P"; 
					 
					$current_date=date("Y-F-d");
					
					if($i<10) $ii="0".$i; else $ii=$i;
					
					$c_date=$y."-".ucwords($month)."-".$ii;
					
				if($current_date == $c_date){ $a=1; break 1;}
		}
		

//echo $pr;"</pre>-";

   $u=total_sun($mn,$y);		
          				
	$chkAtt_a=$db->getRows("select idno from attandenc where school_id='".$school_id."'  and type='2' and status='0' and idno='".$idno."' and session='".$session."' and month='".$month."'");
	
	$chkL=$db->getRows("select status from attandenc where school_id='".$school_id."'  and type='3' and status='0' and idno='".$idno."' and session='".$session."' and month='".$month."'");
	
	$chkH=$db->getRows("select status from holiday where school_id='".$school_id."'  and session='".$session."' and month='".$month."'");

$num_of_days=cal_days_in_month(CAL_GREGORIAN,$mn,$y);
//echo count($data1); "</pre>-";
	$total_absent=count($chkAtt_a)+count($chkL)+count($chkH)+$u;
		$p=count($data1)-$total_absent;
		$data[$ij]["days"]=$data1;
		
		$d["present"]=$pr;
		$d["absent"]=count($chkAtt_a);
		$d["leave"]=count($chkL);
		$d["holidays"]=count($chkH)+$u;
		$d["total"]=count($data1);
		$data[$ij]["attendance_counts"]=$d;
		
		if($current_date == $c_date)break;
		//if($month=="May") die("first".$i."==".$a."--".$c_date."==".$current_date);
		//$datee=date('F Y');
		//$current=$month." ".$y;
		//if($datee == $current) break;
	}
	

	//print_r($data); die;
	if(is_array($data) && count($data)>0)
	{  
		echo json_encode(array('status' => 1, 'msg'=>'Message found Successfully..!', "data"=>$data));    
			
	}else
	{		
	 	echo json_encode(array('status' => 0, 'msg'=>'No message Found..!'));
	}