<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_leave";
$pgAct="view";

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"=>"Attandence", "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
}
function inst($str){
	$arr1 = str_split($str);
	if($arr1[1]=="p"){
		$i="Fee ";
		if($arr1[0]=="b")$i="Bus ";
		return $i." "." Installment ".$arr1[2];
	}
	else
		return $str;
}
function instFine($str,$reasion=""){
	global $db;
	$arr1 = explode("-",$str);
	if($arr1[1]=="class")
		$i="<span style='color:#FF0000'>$reasion (Class Fine)</span>";
	elseif($arr1[1]=="bus")
		$i="<span style='color:#00CC00'>$reasion (Bus Fine)</span>";
	elseif($arr1[1]=="otherfee")
		$i="<span style='color:#660000'>$reasion (Other Fee)</span>";
	elseif($arr1[1]=="hostel")
		$i="<span style='color:#FF00FF'>$reasion (Hostel Fine)</span>";
	else{
		$det=$db->getVal("select type from extrafee_type where id='".str_replace("extra","",$arr1[1])."'");
		if($det=="")
		$i="<span style='color:#660000'>$reasion (".$arr1[1].")</span>";
		else
		$i="<span style='color:#660000'>$reasion (".$det.")</span>";
	}
	//if($reasion!="")$i.=" ".$reasion."";
	return $i.$db->getErMsg();;
}
	?>
    <form method="get" action="">
    <input type="hidden" name="src" value="Attandence" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
	<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">

      <!--              <fieldset style="width:100%; float:left">Detail Section</br>-->
						<!--<button type="submit" name="attendance" class="btn-classic margin">SINGLE DAY CLASS WISE</button>-->
						<!--	<button type="submit" name="attendance_CLASS" class="btn-classic margin">SINGLE DAY OVERALL CLASS</button>-->
                     		
                     		

      <!--              </fieldset>-->
            <p>Leave Report</p>
				</div>
				<div class="well no-padding">

<div class="controls controls-row">
                        <div class="control-group span6">
                            <label class="control-label" for="class">From Date</label>
                            <div class="controls">
                                     <select class="span3" name="fdate"  id="date" style="width: 65px !important;">
                                    <option value="">Date</option>
                                    <?php
									$ndate=date("d");
									$mdate=date("m");
									$ydate=date("Y");
                                    for($m = 01;$m <= 31; $m++){ 
                                        $selected="";
										if($_GET["fdate"]==$m)
										{
											$selected=" selected ";
										}
										if($ndate==$m)
										{
											$selected=" selected ";
										}
                                        echo "<option $selected value='$m'>$m</option>";
                                    }
                                    ?> 
                                </select>
                                <select class="span5" name="fmonth" id="fmonth" style="width: 90px !important;">
                                    <option value="">Month</option>
                                 <?php
                                    for($m = 1;$m <= 12; $m++){ 
                                        $newDate=date("Y")."-".$m."-1";
                                        $month =  date("F",strtotime($newDate));//."(".($ij+3).")";
                                        //$month =  date("F", mktime(0, 0, 0, $m)); 
                                        $selected="";if($_GET["fmonth"]==$m)
										{
											$selected=" selected ";
										}
										
										if($mdate==$m)
										{
											$selected=" selected ";
										}
                                        echo "<option $selected value='$m'>$month</option>";
                                    }
                                 ?> 
                                </select>
                                <select class="span4" name="fyear" id="fyear" style="width: 73px !important;">
                                    <option value="">Year</option>
                                    <?php
                                    for($m = 2013;$m <= 2050; $m++){  
                                        $selected="";if($_GET["fyear"]==$m)
										{	
										$selected=" selected ";
										}										
										if($ydate==$m)
										{	
										$selected=" selected ";
										}
                                        echo "<option $selected value='$m'>$m</option>";
                                    }
                                    ?> 
                                </select>
                                </div>
                        </div>
                        <div class="control-group span6">
                            <label class="control-label" for="medium">To date</label>
                            <div class="controls">
                                    <select class="span3" name="tdate" id="tdate" style="width: 65px !important;">
                                    <option value="">Date</option>
                                    <?php
                                    for($m = 01;$m <= 31; $m++){ 
                                         $selected="";
										 if($_GET["tdate"]==$m)
										 {
											 $selected=" selected ";
										 }
										 if($ndate+1==$m)
										{
											$selected=" selected ";
										}
                                        echo "<option $selected value='$m'>$m</option>";
                                    }
                                    ?> 
                                </select>
                                <select class="span5" name="tmonth" id="tmonth" style="width: 90px !important;">
                                    <option value="">Month</option>
                                 <?php
                                    for($m = 1;$m <= 12; $m++){ 
                                        $newDate=date("Y")."-".$m."-1";
                                        $month =  date("F",strtotime($newDate));//."(".($ij+3).")";
                                        //$month =  date("F", mktime(0, 0, 0, $m)); 
                                        $selected="";if($_GET["tmonth"]==$m)
										{
										$selected=" selected ";
										}
										if($mdate==$m)
										{
											$selected=" selected ";
										}
                                        echo "<option $selected value='$m'>$month</option>";
                                    }
                                 ?> 
                                </select>
                                <select class="span4" name="tyear" id="tyear" style="width: 73px !important;">
                                    <option value="">Year</option>
                                    <?php
                                    for($m = 2013;$m <= 2050; $m++){  
                                         $selected="";if($_GET["tyear"]==$m)
										 {	 
										 $selected=" selected ";
										 }
										 if($ydate==$m)
										{
											$selected=" selected ";
										}
                                        echo "<option $selected value='$m'>$m</option>";
                                    }
                                    ?> 
                                </select>
                                </div>
                        </div>
                    </div>

                	<div class="controls controls-row">

                      <br>  
                    <!--<div style="float:left;">-->
                         
                        <!--<input type="date" name="date" class="input-block-level" style="display:inline-block;width:200px;"  />-->

                        <!--<button type="submit" name="attendance_report" style="background:#FFC224;color:black;font-weight:bold;font-size:16px;border-radius:7px;padding:5px;margin-left:55px;display:inline-block;">Single Day Report</button>-->
 
                        <button type="submit" name="total_attendance_report" style="background:#FFC224;color:black;font-weight:bold;font-size:16px;border-radius:7px;padding:5px;margin-left:55px;">DATE WISE REPORT</button>
                        &emsp;&emsp;&emsp;&emsp;&emsp;
                            <select class="span6" name="month" id="month" style="width: 120px !important;margin-left:55px;display:inline-block;">
                                <option value="">Month</option>
                             <?php
                                for($m = 1;$m <= 12; $m++){
                                    $newDate=date("Y")."-".$m."-1";
                                    $month =  date("F",strtotime($newDate));//."(".($ij+3).")";
                                    //$month =  date("F", mktime(0, 0, 0, $m)); 
                                    $selected="";if($_GET["tmonth"]==$m)
									{
									$selected=" selected "; 
									}
									if($mdate==$m)
									{
										$selected=" selected ";
									}
                                    echo "<option $selected value='$m'>$month</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>-->
                    <br><br><br>


                    </div>
                    
				</div>
			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";

    if(isset($_GET["total_attendance_report"])){
        $where  =   "";
        $whereface = "";
		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.=" (date between '$fdate' and '$tdate') ";
			$whereface = " (date between '$fdate' and '$tdate') ";
// 			$conwhere1 .=" AND date = '$fdate' ";
		}

        ?>

        <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">

                <table class="table" id="tblData" border="1" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S.No</th>
                            <th>Teacher Name</th>
                            <th>Total Leave</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                             $students = $db->getRows("Select * from staff where session='".$_SESSION["admin"]["session"]."'  and status='1'  order by name");

                             $i=0; 
                             foreach($students as $stu){ $i++;
                             $totalabsent = $db->getVal("SELECT COUNT(id) FROM teacher_leave WHERE teacher_id='$stu[idno]' and leave_status='1' AND $whereface AND session='".$_SESSION["admin"]["session"]."' ");
                        ?>
                        <tr>
                        <td><?php echo $i; ?></td>
                        <td><?php echo $stu['name']; ?></td>
                        <td><?php echo $totalabsent; ?></td>
                        </tr>
                        <?php  } ?>
                    </tbody>

                </table>

		    </div>

        </div>

        <?php
    }
    
    if(isset($_GET["monthly_report"])){
        $where  =   "";
        $whereface = "";
		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.=" (date between '$fdate' and '$tdate') ";
			$whereface = " (date between '$fdate' and '$tdate') ";
// 			$conwhere1 .=" AND date = '$fdate' ";
		}

        	$month = $_GET["month"];

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

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

			$fdate  =   $y."-".$month."-"."01";
			$tdate  =   $y."-".$month."-"."31";
			$whereface = " (date between '$fdate' and '$tdate') ";


        ?>

        <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">

                <table class="table" id="tblData" border="1" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S.No</th>
                            <th>Teacher Name</th>
                            <th>Total Leave</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                             $students = $db->getRows("Select * from staff where session='".$_SESSION["admin"]["session"]."'  and status='1'  order by name");

                             $i=0; 
                             foreach($students as $stu){ $i++;
                             $totalabsent = $db->getVal("SELECT COUNT(id) FROM teacher_leave WHERE teacher_id='$stu[idno]' and leave_status='1' AND $whereface AND session='".$_SESSION["admin"]["session"]."' ");
                        ?>
                        <tr>
                        <td><?php echo $i; ?></td>
                        <td><?php echo $stu['name']; ?></td>
                        <td><?php echo $totalabsent; ?></td>
                        </tr>
                        <?php  } ?>
                    </tbody>

                </table>

		    </div>

        </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>