<?php
include("../db.php");
$ch=$_REQUEST["type"];
if($ch=="getAttandencCalender"){
	$aryList=$db->getRows("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."'");
	if(is_array($aryList) && count($aryList)>0)
	{
		$mn=date('m',strtotime($_REQUEST["year"]."-".$_REQUEST["month"]."-1"));$y=0;
		$y=$_REQUEST["year"];
		$maxDays=date('t',strtotime($y."-".$mn."-1"));
		?>
        <table style="margin:20px">
            <thead>
                <tr>
                    <th rowspan="2">Id No(Name)</th>
                    <?php for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i)); ?>
							<th><?php echo $day; ?></th>
					<?php } ?>
                    <th></th>
                </tr>
            </thead>
            
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['idno'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $clist["idno"] ?>(<?php echo $clist["name"] ?>)</td>
                   <?php for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i)); ?>
                    <td>
                    <?php $ColorClass="success";
					$chkHoli=$db->getVal("select status from holiday where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$y."-".$mn."-".$i."'");
					$chkAtt=$db->getVal("select status from attandenc where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$y."-".$mn."-".$i."' and type='1' and idno='".$clist["idno"]."'");
					//echo $db->getLastQuery();
					if(strtolower($day)=="sun") $ColorClass="danger sunday";
					elseif($chkHoli=="1") $ColorClass="danger holiday";
					elseif($chkAtt=="0") $ColorClass="default absent";
					
					?>
                    <span type="button" onclick="addAttandenc('<?php echo $clist["idno"] ?>','<?php echo $i ?>',this)" class="btn btn-<?php echo $ColorClass; ?>">
					<?php echo $i; ?>
                    </span></td>
                    <?php } ?>
                </tr>
            <?php } ?>
            </tbody>
           
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
}
elseif($ch=="addAttandenc"){
	$date=$_REQUEST["year"]."-".$_REQUEST["month"]."-".$_REQUEST["date"];
	$dataAry=array( "date"=>$date,
					"idno"=>$_REQUEST["id"],
					"status"=>$_REQUEST["status"],
					
"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
					"type"=>"1"
	);
	$db->insertAry("attandenc",$dataAry);
	if($_REQUEST["status"]=="0"){
		sms(mydetail("mobile",$_REQUEST["id"],"staff"),"Dear ".mydetail("name",$_REQUEST["id"],"staff").", Your are absent on ".$date,"faculty_salery");
	}
	echo "Attandence Add successfully".$db->getErMsg();
}
elseif($ch=="removeAttandenc"){
	$date=$_REQUEST["year"]."-".$_REQUEST["month"]."-".$_REQUEST["date"];
	$db->delete("attandenc","where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$date."' and idno='".$_REQUEST["id"]."' and status=0 and type=1");
	//echo $db->getLastQuery();
	echo "Attandence remove successfully";
}

/*
///////////////////////////////////////////Student ssection//////////////////////////////////////////////
*/
elseif($ch=="getAttandencCalenderStudent"){
	$aryList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."' order by name");
	if(is_array($aryList) && count($aryList)>0)
	{
		$mn=date('m',strtotime($_REQUEST["year"]."-".$_REQUEST["month"]."-1"));$y=0;
		$y=$_REQUEST["year"];
		$maxDays=date('t',strtotime($y."-".$mn."-1"));
		?>
        <table style="margin:20px">
            <thead>
                <tr>
                    <th rowspan="2">Name</th>
                    <?php for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i)); ?>
							<th><?php echo $day; ?></th>
					<?php } ?>
                    <th></th>
                </tr>
            </thead>
            
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['idno'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo ucfirst($clist["name"]); ?></td>
                   <?php for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i)); ?>
                    <td>
                    <?php $ColorClass="success";
					$chkHoli=$db->getVal("select status from holiday where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$y."-".$mn."-".$i."'");
					$chkAtt=$db->getVal("select status from attandenc where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$y."-".$mn."-".$i."' and type='2' and idno='".$clist["idno"]."'");
					//echo $db->getLastQuery();
					if(strtolower($day)=="sun") $ColorClass="danger sunday";
					elseif($chkHoli=="1") $ColorClass="danger holiday";
					elseif($chkAtt=="0") $ColorClass="default absent";
					
					?>
                    <span type="button" onclick="addAttandenc('<?php echo $clist["idno"] ?>','<?php echo $i ?>',this)" class="btn btn-<?php echo $ColorClass; ?>">
					<?php echo $i; ?>
                    </span></td>
                    <?php } ?>
                </tr>
            <?php } ?>
            </tbody>
           
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
}
elseif($ch=="addAttandencStudent"){
	$date=$_REQUEST["year"]."-".$_REQUEST["month"]."-".$_REQUEST["date"];
	$dataAry=array( "date"=>$date,
					"idno"=>$_REQUEST["id"],
					"status"=>$_REQUEST["status"],
					
"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
					"type"=>"2"
	);
	$db->insertAry("attandenc",$dataAry);
	if($_REQUEST["status"]=="0"){
		sms(mydetail("mobile",$_REQUEST["id"],"student"),"Dear ".mydetail("fname",$_REQUEST["id"],"student").", Your Child(".mydetail("name",$_REQUEST["id"],"student").") is absent on ".$date,"faculty_salery");
	}
	echo "Attandenc add successfully";
}
elseif($ch=="removeAttandencStudent"){
	$date=$_REQUEST["year"]."-".$_REQUEST["month"]."-".$_REQUEST["date"];
	$db->delete("attandenc","where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$date."' and idno='".$_REQUEST["id"]."' and status=0 and type=2");
	echo "Attandenc Remove successfully";
	//echo $db->getLastQuery();
}elseif($ch=="getHolidayCalender"){
	$y=$_REQUEST["year"];
	?><table style="margin:20px"><?php
	for($m = 1;$m <= 12; $m++){ 
		?><tr><?php
		for($j=0; $j<= 31;$j++){
			$month =  date("F", mktime(0, 0, 0, $m)); 
			$maxDays=date('t',strtotime($y."-".$m."-1"));
			$day=date('D',strtotime($y."-".$m."-".$j));
			$chkHoli=$db->getVal("select status from holiday where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$y."-".$m."-".$j."'");
			$ColorClass="default";if(strtolower($day)=="sun") $ColorClass="danger";
			elseif($chkHoli=="1") $ColorClass="info";
			?><td>
            <?php if($j<=$maxDays){ if($j==0){ 
			echo $month;
			}else{?>
            	<button title="<?php echo $day; ?>" type="button" onclick="addHoliday('<?php echo $y."-".$m."-".$j ?>',this)" class="btn btn-<?php echo $ColorClass; ?>">
					<?php echo $j; ?>
                  </button>
            <?php }} ?>
			</td><?php
		}
		?><tr><?php
	}
	?></table><?php
}
elseif($ch=="addHoliday"){
	$date=$_REQUEST["date"];
	$chk=$db->getVal("select * from holiday where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$date."'");
	if($chk==""){
		$dataAry=array( "date"=>$date,
		
"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
						"reasion"=>$_REQUEST["reasion"]
		);
		$db->insertAry("holiday",$dataAry);
	}
	echo "Holiday inserted successfully".$db->getErMsg();
}elseif($ch=="removeHoliday"){
	$row=$db->delete("holiday","where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$_REQUEST["date"]."'");
	echo "Holiday remove successfully";
}
?>