<script type="text/javascript" src="js/tableExport.js"></script>

<script type="text/javascript" src="js/jquery.base64.js"></script>

<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;}
.dynamic{display:none}
table td {
padding: 6px !important;
}
legend{line-height: 16px;font-weight:100;}

.outer .MigrateLeftFee{
  background-color: #DADADA !important;
  border: 5px solid #181616 !important;
  min-height: 440px;
  min-width:700px;
}
.st {
	width: 55px;
	font-size:10px;
	display: inline-block;
	color: #021EF0;
}
.size{
	font-size:12px;
}
.recepit {
	width: 717px !important;
	height: 344px !important;
}
.total{
	color: darkgreen;
    font-weight: 800;
}
</style>

<?php
    $pgMod="teacher_wise";
    $pgAct="view";

$monthsList = array(
    'January',
    'February',
    'March',
    'April',
    'May',
    'July ',
    'August',
    'September',
    'October',
    'November',
    'December',
);
$monthsList1 = array(
    1=>'Apr',
    2=>'May',
    3=>'Jul ',
    4=>'Aug',
    5=>'Sep',
    6=>'Oct',
    7=>'Nov',
    8=>'Dec',
	9=>'Jan',
    10=>'Feb',
    11=>'Mar'
);

if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
if($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("student","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Attendance", "page_id"=>$pgMod)));
}
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
}

?>
    <form method="get" action="">
    <input type="hidden" name="src" value="Attendance" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
	<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="controls controls-row" style="padding: 20px;">
                            <select name="month" id="month" style="margin-left:55px;display:inline-block;">
                                <option value="">Month</option>
                             <?php
                                for($m = 1;$m <= 12; $m++){ 
                                    $month = date("F", mktime(0, 0, 0, $m, 10));
                                    $selected="";
                                    if($_GET["month"]==$m){
									    $selected=" selected "; 
									}
                                    echo "<option $selected value='$m'>$month</option>";
                                }
                             ?> 
                            </select>

                            <select name="staff" id="staff" style="margin-left:55px;display:inline-block;">
                                <option value="">--Select Staff--</option>
                            <?php
                                $staff = $db->getRows("select * from staff where status=1 and school_id='".$_SESSION["admin"]["school_id"]."' order by name ");
                                foreach($staff as $st){
                                    $selected="";
                                    if($_GET["staff"]==$st['idno']){
									    $selected=" selected "; 
									}
                                    echo "<option value='$st[idno]' $selected>$st[name]</option>";
                                }
                            ?>
                            </select>

                        <button type="submit" name="monthly_report" style="background:#FFC224;color:black;font-weight:bold;font-size:16px;border-radius:7px;padding:5px;margin-left:55px;display:inline-block;">MONTHLY REPORT</button>
                    </div>
				</div>
			</div>
    </div>
    </form>

	<?php
	$where=array();
	$qry="";

    if(isset($_GET["monthly_report"])){

    	$aryList=$db->getRow("select * from staff where idno='$_GET[staff]' and school_id='".$_SESSION["admin"]["school_id"]."' and status=1 ");
    	if(is_array($aryList) && count($aryList)>0)
    	{

        	$month = $_GET["month"];

        //     $years = explode("-",$_SESSION["admin"]["session"]);

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

            $y = date("Y");

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

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

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

        <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 class="btn btn-success" onclick="exportTableToExcel('tblData', 'Attendance Detail')" style="margin-left:35px;">Excel</button>
                        <button type="button" onclick="printDiv('printDiv2')" class="btn btn-primary">Print</button>
                    </div>
                </div>

            <div class="well no-padding" id="printDiv2">

                <h4 style="text-align:center;color:crimson;"> Teacher Wise Report</h4>
                <h5 style="float:right;">Month: <?php echo date('F', mktime(0, 0, 0, $mn, 10)); ?></h5>
                <h5 style="float:left;">Staff: <?php echo $aryList["name"]; ?> </h5>

                <h5 style="float:left;width:100%;">
                    Total Present: <span id="total_present"></span> &emsp;
                    Total Absent: <span id="total_ab"></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 Early: <span id="total_early"></span> &emsp;
                    Total Late: <span id="total_late"></span> &emsp;
                </h5>

                <table class="table" id="tblData" border="1" cellspacing="0">
                <thead>
                    <tr style="color:white;">
                        <th>S.No.</th>
                        <th>Date</th>
                        <td>Day</td>
                        <th>In</th>
						<th>Out</th>
						<th>Remarks</th>
                    </tr>
                </thead>

                <tbody>
                <?php
                $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 school_id='".$_SESSION["admin"]["school_id"]."' and att_type='IN' ");
                ?>
                    <tr>
                        <td><?php echo $i; ?></td>
                        <td><?php echo date('d-m-Y',strtotime($y."-".$mn."-".$i)); ?></td>
                        <td><?php echo date("l", mktime(0,0,0,$mn,$i,$y)); ?></td>
                        <td><?php if(count($result)>1){ echo date('H:i:s', strtotime($result['timestamp'])); } ?></td>
                        <td><?php
                                if(count($result)>1){
                                    $out = $db->getVal("select timestamp from attendance_new where teacher_id='$aryList[idno]' and (timestamp>='$fdate' and timestamp<='$todate') and school_id='".$_SESSION["admin"]["school_id"]."' and att_type='OUT' ");
                                    if($out){
                                        echo date('H:i:s', strtotime($out));
                                    }
                                }
                            ?>
                        </td>

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

                        $chkAtt=$db->getRow("SELECT * FROM attendance_new WHERE cast(timestamp as date) = '".$y."-".$mn."-".$i."' and teacher_id='".$aryList["idno"]."' and school_id='".$_SESSION["admin"]["school_id"]."' ");

    					if(strtolower($day)=="sun") {$ColorClass="black"; $msg="Sunday"; ?>
        				    <span  style="color:<?php echo $ColorClass; ?>;"><?php echo $msg; ?></span>
        				<?php }
    					elseif($chkHoli===1) {$ColorClass="#00CED1";  $msg="Holiday"; $totalholidays+=1; ?>
        				    <span  style="color:<?php echo $ColorClass; ?>;"><?php echo $msg; ?></span>
        				<?php }
        				elseif($chkAtt['att_type']=="OD") {$ColorClass="#10BAB5";  $msg="OD"; $totalpresent++; $totalod++; ?>
        				    <span  style="color:<?php echo $ColorClass; ?>;"><?php echo $msg; ?></span>
        				<?php }
        				elseif($chkAtt['att_type']=="HF") {$ColorClass="orange";  $msg="Half Day"; $totalpresent++; $totalhf++; ?>
        				    <span  style="color:<?php echo $ColorClass; ?>;"><?php echo $msg; ?></span>
        				<?php }
        				elseif($chkAtt['att_type']=="LV") {$ColorClass="purple";  $msg="Leave"; $totallv++; ?>
        				    <span  style="color:<?php echo $ColorClass; ?>;"><?php echo $msg; ?></span>
        				<?php }
        				elseif($chkAtt['att_type']=="IN") {
                            $totalpresent++;
        				    $ColorClass="green";  $msg="Present";
        				    ?>
					            <span  style="color:<?php echo $ColorClass; ?>;"><?php echo $msg; ?></span>
                            <?php
        				    
        				    $att_time = $db->getRow("SELECT * FROM attendance_timing WHERE date = '".$y."-".$mn."-".$i."' and teacher='$aryList[idno]' and school_id='".$_SESSION["admin"]["school_id"]."' ");
        				    
        				    if(count($att_time)>1){

                                $attstart = strtotime("$att_time[date] $att_time[start_time]");
                                $attend   = strtotime("$att_time[date] $att_time[end_time]");
    
        					    $att_in = strtotime($chkAtt['timestamp']); 
        					    if($att_in > $attstart){$late++; $msg=" - Late";$ColorClass="#FFD700";
        					    ?>
    					        <span  style="color:<?php echo $ColorClass; ?>;"><?php echo $msg; ?></span>
                                <?php
        					    }

        					    $outtime = $db->getVal("SELECT timestamp FROM attendance_new WHERE cast(timestamp as date) = '".$y."-".$mn."-".$i."' and teacher_id='".$aryList["idno"]."' and att_type='OUT' and school_id='".$_SESSION["admin"]["school_id"]."' ");
            					    $att_out = strtotime($outtime); 
            					    if($att_out < $attend){$early++;
            					         $msg=" - Early";$ColorClass="blue;";
            					        ?>
            					            <span  style="color:<?php echo $ColorClass; ?>;"><?php echo $msg; ?></span>
                                        <?php
            					    }
                            }

        				}
        				else {
    					?>
                        <span  style="color:<?php echo $ColorClass; ?>;">
        					<?php echo $msg;
            					if($msg=="Absent"){
            					    $totalabsent+=1;
            					}
        					?>
                        </span>
                        <?php } ?>
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
            </div>

            <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 $totalabsent; ?>);
                $("#total_early").html(<?php echo $early; ?>);
                $("#total_late").html(<?php echo $late; ?>);
            </script>
    
            </div>
        </div>
    		<?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
    	}
    }
	

?>

	<script type="text/javascript">
	$(document).ready(function(e) {
		var oTable = $('.table').dataTable( {
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": true,
			"bSort": true,
			"bInfo": true,
			"bAutoWidth": false
		});
	});
	function activeNowMe(id){
		$.ajax({
			url: "<?php echo URL_ROOT ?>Promote/ajax.php?type=activeMe&id="+id
		}).done(function(data) {
			$("#row"+id).hide("slow");
		});
	}
	function getStudentPopup(){
		var url="";
		$(".checkboxes").each(function(){
			if($(this).prop('checked')==true){
				var idno=$(this).val();
				var name=$(".name"+idno).html()
				var left=$(".left"+idno).html()
				url+="&"+name+"`"+idno+"="+left;
			}
		})
		if(url!=""){
			getPopup("<?php echo URL_ROOT ?>Promote/?page_id=MigrateLeftFee&class=<?php echo $_GET["class"] ?>"+url,"1","MigrateLeftFee");
			$(".close").click(function() {
				$(this).parent().remove();
			});
			$(".outer").click(function() {
				$(this).remove();
			});

		}else{
			alert("select atleast one student to mgrate");
		}
	}
	
	    function exportTableToExcel(tableID, filename = '',e){
    
    var tableType = document.getElementById('tblData').getAttribute("data-tblname");

    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    e.preventDefault();
}

	</script>
    <style>
    tfoot{display: table-footer-group !important;}
	.MigrateLeftFee{width:650px !important}
    </style>