<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="single_day_report";
$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
}
	?>

    
    <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" name="attendance_report" class="btn btn-primary" value="Single Day Report" style="margin-left:100px;" />
                    </div>
                </form>
        
            </div>
    	</div>
	</div>
	</div>

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

    if(isset($_GET["attendance_report"])){
        $where  =   "";
		if(isset($_GET["fyear"]) && trim($_GET["fyear"])!="" && isset($_GET["fmonth"]) && trim($_GET["fmonth"])!="" && isset($_GET["fdate"]) && trim($_GET["fdate"])!="" && isset($_GET["tyear"]) && trim($_GET["tyear"])!="" && isset($_GET["tmonth"]) && trim($_GET["tmonth"])!="" && isset($_GET["tdate"]) && trim($_GET["tdate"])!=""){
			$fdate=$_GET["fyear"]."-".$_GET["fmonth"]."-".$_GET["fdate"];
			$tdate=$_GET["tyear"]."-".$_GET["tmonth"]."-".$_GET["tdate"];
			$where.=" (timestamp between '$fdate' and '$tdate') ";
		}

        $totalpresent = $totalhf = $totalod = $totallv = $totalabsent = 0;
        
        $records = $db->getRows("SELECT * FROM staff WHERE school_id='".$_SESSION["admin"]["school_id"]."' and status='1' ");
        ?>

        <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">
				<!--<input  class="btn btn-primary" type="button"  onclick="$('.table').tableExport({type:'excel',escape:'false'});" id="btnExport" value=" Export To Excel " />-->
                    <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;">Single Day Report</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($records); ?> &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" id="tblData" border="1" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S.No</th>
                            <th>Name</th>
                            <th>Designation</th>
                            <th>In</th>
                            <th>Out</th>
                            <th>Remarks</th>
                        </tr>
                    </thead>
                    <tbody> 
                        <?php
                             $i=0;
                             foreach($records as $rec){ $i++;
                             $s=0;
                                $result = $db->getRow("Select * from attendance_new where teacher_id='$rec[idno]' and timestamp like '$_GET[date]%' and school_id='".$_SESSION["admin"]["school_id"]."' and att_type='IN' ");
                            ?>
                            <tr>
                                <td><?php echo $i; ?></td>
                                <td><?php echo $rec['name']; ?></td>
                                <td><?php echo $rec['designation']; ?></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='$rec[idno]' and timestamp like '$_GET[date]%' and school_id='".$_SESSION["admin"]["school_id"]."' and att_type='OUT' ");
                                        if($out){
                                            echo date('H:i:s', strtotime($out));
                                        }
                                    }
                                    ?>
                                </td>
                        <td style="font-weight:900;">
                        <?php
                        $day=date('D',strtotime($_GET['date']));
                        $daycolor = "color:red;";
                        $ColorClass="absent";
                        $msg = "Absent";
                        $y=$_GET["fyear"];
    					$chkHoli=$db->getVal("select status from staff_holiday where school_id='".$_SESSION["admin"]["school_id"]."' and date='$fdate' and teacher='".$rec['idno']."'");

                if(strlen($i)==1){
                    $i='0'.$i;
                }
                $chkAtt = $db->getRow("SELECT * FROM attendance_new WHERE  timestamp like '$_GET[date]%' and teacher_id='".$rec["idno"]."' and school_id='".$_SESSION["admin"]["school_id"]."' ");

                if(strtolower($day)=="sun") {$ColorClass="danger sunday"; $msg="Sunday"; $daycolor="color:black;"; ?>
                    <span type="button" class="myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                    <?php   }
                elseif($chkHoli===1) {$ColorClass="danger holiday";  $msg="Holiday";  $daycolor="color:#00CED1;";
					    ?>
                <span type="button" class="myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }
                elseif($chkAtt['att_type']=='OD') {$msg="On Duty"; $totalod++;  $daycolor="color:#10BAB5;";
					    ?>
                <span type="button" class="myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }
                elseif($chkAtt['att_type']=='HF') {$msg="Half Day"; $totalhf++;  $daycolor="color:orange;";
					    ?>
                <span type="button" class="myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }
                elseif($chkAtt['att_type']=='LV') {$msg="Leave"; $totallv++;  $daycolor="color:purple;";
					    ?>
                <span type="button" class="myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }
                elseif($chkAtt['att_type']=='IN') {$msg="Present"; $totalpresent++;  $daycolor="color:green;";
					    ?>
                <span type="button" class="myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }

                        else {
                            ?>
    					        <span type="button" class="myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                                <?php
                            if($msg=="Absent"){
            					    $totalabsent++;
            					}
                        }
    					?>
                        </td>
                            </tr>
                        <?php   }
                        ?>
                    </tbody>

                </table>
                <div>

                </div>

		    </div>

        </div>
        </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; ?>);
        </script>
        
        <?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){
    $(".myclass").css("color", "black");   
    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();
    }
    window.location.reload();
    e.preventDefault();
    
}

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