<style>
    .top-bar-minimize{display:none;}
    thead, tfoot {display: table-header-group;}
    .top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 100% !important;overflow: inherit !important;} .check{ width: 133px; padding: 5px; display: inline-block; border-right: 1px dotted;}
    .check input[type="checkbox"] { margin: 0 6px;}
</style>

<?php
$pgMod="manual_face_attendance";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']); 

if($_POST){
    
    // print_r($_POST);
    // die(); 
    if(isset($_POST["save"])){
     $month =  date("F");
	if(is_array($_POST["student"]) && count($_POST['student'])>0){ $i=0;
		foreach($_POST["student"] as $idno){
            $att_type = $_POST['stud'.$idno];

            $contentid = $db->getVal("select content_id from teacher_leave order by content_id DESC limit 1 ")+1;

            $checkvalue=$db->getVal("select att_type from attendance_new where teacher_id='$idno' and timestamp LIKE '$_GET[date]%' and school_id='".$_SESSION["admin"]["school_id"]."' ");
            if($att_type){
                // print_r($_GET['date']);
                // die();
                $array=array(
                            "timestamp"     =>  date("$_GET[date] H:i:s"),
                            "teacher_id"    =>  $idno,
                            "att_type"      =>  $att_type,
							"school_id"     =>  $_SESSION["admin"]["school_id"]
							);

                if($checkvalue == ""){

                    $db->insertAry("attendance_new", $array);
                    if($att_type != "IN"){
                        if($att_type == "HF"){
                            $ishalf = 1;
                        }
                        if($att_type == "LV"){
                            $ishalf = 0;
                        }
                        if($att_type == "OD"){
                            $ishalf = 2;
                        }
                        $leave = array(
                                "teacher_id"    =>  $idno,
                                "admin_id"      =>  $_SESSION["admin"]["uid"],
                                "subject"       =>  "Software Manual",
                                "details"       =>  "Software Manual",
                                "date"          =>  $_GET["date"],
                                "leave_status"  =>  1,
                                "remarks"       =>  "",
                                "content_id"    =>  $contentid,
                                "is_half_day"   =>  $ishalf,
                                "school_id"     =>  $_SESSION["admin"]["school_id"]
                            );
                            
                        $db->insertAry("teacher_leave", $leave);
                    }

                }

                if($checkvalue == "IN" && ($att_type == "HF" || $att_type == "OD" || $att_type == "LV" ) ){
                    
                    if($att_type == "LV"){
                        $ishalf = 0;
                    }
                    if($att_type == "HF"){
                        $ishalf = 1;
                    }
                    
                    if($att_type == "OD"){
                        $ishalf = 2;
                    }
                    $leave = array(
                            "teacher_id"    =>  $idno,
                            "admin_id"      =>  $_SESSION["admin"]["uid"],
                            "subject"       =>  "Software Manual",
                            "details"       =>  "Software Manual",
                            "date"          =>  $_GET["date"],
                            "leave_status"  =>  1,
                            "remarks"       =>  "",
                            "content_id"    =>  $contentid,
                            "is_half_day"   =>  $ishalf,
                            "school_id"     =>  $_SESSION["admin"]["school_id"]
                        );
                        
                        $db->insertAry("teacher_leave", $leave);
                        
                        $updary = array("att_type" => $att_type);
                        
                        $db->updateAry("attendance_new", $updary, "where teacher_id='$idno' and timestamp LIKE '$_GET[date]%' and school_id='".$_SESSION["admin"]["school_id"]."' ");
                }

                if($checkvalue !="" && $checkvalue != "IN" && ($att_type != "IN" ) ){
                    
                    if($att_type == "LV"){
                        $ishalf = 0;
                    }
                    if($att_type == "HF"){
                        $ishalf = 1;
                    }
                    
                    if($att_type == "OD"){
                        $ishalf = 2;
                    }
                    $leave = array(
                            "is_half_day"   =>  $ishalf
                        );
                        
                        $db->updateAry("teacher_leave", $leave, "where teacher_id='$idno' and date='$_GET[date]' and school_id='".$_SESSION["admin"]["school_id"]."' ");
                        
                        $updary = array("att_type" => $att_type);
                        
                        $db->updateAry("attendance_new", $updary, "where teacher_id='$idno' and timestamp LIKE '$_GET[date]%' and school_id='".$_SESSION["admin"]["school_id"]."' ");
                }

                
                if($att_type == "IN" && ($checkvalue == "HF" || $checkvalue == "OD" || $checkvalue == "LV" ) ){
                    
                }

            } else {
                $db->delete("attendance_new", " where teacher_id='$idno' and timestamp LIKE '$_GET[date]%' and school_id='".$_SESSION["admin"]["school_id"]."' AND att_type='IN'  ");
            }

            $i++;
            
		}
		
	 }
    }
}

if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
?>

    <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
            </div>
            <div class="well no-padding">
                <div class="control-group">
            
            		<form method="get" action="">
             		    <input type="hidden" name="src" value="Attendance" />
            		    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
            		    <input type="hidden" name="attendance_day_wise" />
                        <label class="control-label" for="rollno">Select Date</label>
                        <div class="controls">
                            <input type="date" name="date" value="<?php if($_GET['date']!=""){echo $_GET['date']; } else { echo date("Y-m-d"); } ?>" />
                            <input type="submit" class="btn btn-primary" value="Proceed" style="margin-left:100px;" />
                        </div>
                    </form>
            
                </div>
        	</div>
    	</div>
    </div>

<?php
	if(isset($_GET["attendance_day_wise"])){
	    
	    $totalpresent = $totalhf = $totalod = $totallv = $totalabsent = 0;
        $studentList = $db->getRows("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and status=1 $where ");

		?>
		<form method="post" action="">
		<div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Detail</h3>
                <div style="float:right; margin-top:5px">
                    <button type="button" onclick="printDiv('printDiv2')" class="btn btn-primary">Print</button>
                    <input type="submit" value="save" name="save" class="btn btn-default pull-right" >
                </div>
            </div>
            <div class="well no-padding" id="printDiv2">
                
            <h4 style="text-align:center;color:crimson;">Manual Face Attendance</h4>
            <h5 style="float:right;">Date: <?php echo date('d-M-Y', strtotime($_GET['date'])); ?></h5>
            <h5 style="float:left;">
                Total Staff: <?php echo count($studentList); ?> &emsp;
                Total Present: <span id="total_present"></span> &emsp;
                Total Half Day: <span id="total_hf"></span>&emsp;
                Total OD: <span id="total_od"></span>&emsp;
                Total Leave: <span id="total_lv"></span>&emsp;
                Total Absent: <span id="total_ab"></span>
            </h5>

            	<table class="table" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S.No</th>
                            <th>Name</th>
                            <th>Father Name</th>
                            <th>Designation</th>
                            <!--<th>Roll No.</th>-->
                            <th>Present</th>
                            <th>Half Day</th>
                            <th>OD</th>
                            <th>Leave</th> 
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $i=1;
                        foreach($studentList as $student){
                        $checkvalue=$db->getVal("select att_type from attendance_new where teacher_id='".$student['idno']."' and timestamp LIKE '$_GET[date]%' and school_id='".$_SESSION["admin"]["school_id"]."' ");
                            ?> 
                            <input type="hidden" name="student[]" value="<?php echo $student['idno'] ;?>" />
                            <tr>
                                <td><?php echo $i; ?></td>
                                <td><?php echo $student['name']; ?></td>
                                <td><?php echo $student['fname']; ?></td>
                                <td><?php echo $student['designation']; ?></td>
                                <td><input name="stud<?php echo $student['idno']; ?>" value="IN" type="radio" style="width:20px;height:20px;" <?php if($checkvalue == "IN"){ echo "checked"; $totalpresent++; } ?> /></td>
                                <td><input name="stud<?php echo $student['idno']; ?>" value="HF" type="radio" style="width:20px;height:20px;" <?php if($checkvalue == "HF"){ echo "checked"; $totalhf++; } ?> /></td>
                                <td><input name="stud<?php echo $student['idno']; ?>" value="OD" type="radio" style="width:20px;height:20px;" <?php if($checkvalue == "OD"){ echo "checked"; $totalod++; } ?> /></td>
                                <td><input name="stud<?php echo $student['idno']; ?>" value="LV" type="radio" style="width:20px;height:20px;" <?php if($checkvalue == "LV"){ echo "checked"; $totallv++; } ?> /></td>
                            </tr>
                        <?php
                            $i++;
                        }
                        ?>
        
                    </tbody>
                    </table>
                </div>
                </div>
                </form>
		<?php
	}
?>

<script>
    $("#total_present").html(<?php echo $totalpresent; ?>);
    $("#total_hf").html(<?php echo $totalhf; ?>);
    $("#total_od").html(<?php echo $totalod; ?>);
    $("#total_lv").html(<?php echo $totallv; ?>);
    $("#total_ab").html(<?php echo count($studentList)-$totalpresent-$totalhf-$totalod-$totallv; ?>);
</script>



