<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 = "leave_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; ?>" />
                    <label class="control-label">Select Month</label>
                    <div class="controls">
                        <select 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++){
                                    $month = date("F", mktime(0, 0, 0, $m, 10));
                                    $selected="";
                                    if($_GET["month"]==$m){
									    $selected=" selected "; 
									}
                                    echo "<option $selected value='$m'>$month</option>";
                                }
                            ?>
                        </select>
                        <input type="submit" name="leave_report" class="btn btn-primary" value="Leave Report" style="margin-left:100px;" />
                    </div>
                </form>
        
            </div>
    	</div>
	</div>
	</div>

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

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

    	$aryList=$db->getRows("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and status=1 order by name");
    	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');
            }
            
            $fdate = $y."-".$month."-"."1";
            $tdate = $y."-".$month."-".$maxDays;
    		?>

		 <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;"> Total Leave Report </h4>
                <h5 style="float:right;margin-top:-15px;">Month: <?php echo $month = date("F", mktime(0, 0, 0, $_GET['month'], 10)); ?></h5><br>

                <table class="table" id="tblData" border="1" cellspacing="0">
                <thead>
                    <tr>
                        <th>S.No</th>
                        <th>Name</th>
                        <th>Designation</th>
                        <th>Total Leave</th>
                        <th>Total Used</th>
                        <th>Total Remaining</th>
                        <th>
                            <?php echo $month; ?> Leave
                        </th>
                    </tr>
                </thead>

                <tbody>
                <?php
                $ijk=0;
    			 foreach($aryList as $clist){ $i++; $ijk++;
                    $aryPgAct["id"]=$clist['idno'];
                    $totalpresent = 0;
                    $totalabsent = 0;
                    $totalholidays = 0;
                    $totalsundays = 0;
                    $late = 0;
                    $early = 0;
                    $hf = 0;
                    $od = 0;
                    $lv = 0;
                ?>
                    <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                        <td><?php echo $ijk; ?></td>
                        <td><?php echo ucfirst($clist["name"]); ?></td>
                        <td><?php echo $clist['designation']; ?></td>
                        <td><?php echo $total_leave = $clist['cl_count']; ?></td>
                        <td>
                            <?php  $leaveused = 0; $extraleaves = 0;
                            $leavedata =  $db->getRows("select * from teacher_leave where teacher_id='$clist[idno]' and leave_status='1' and school_id='".$_SESSION["admin"]["school_id"]."' ");
                            foreach($leavedata as $data){
                              if($data['leave_status']=="1" && $data["is_half_day"]=="0"){
                                  $leaveused++;
                              }
                              if($data['leave_status']=="1" && $data["is_half_day"]=="1"){
                                  $leaveused=$leaveused+0.5;
                              } 
                              if($data['leave_status']=="1" && $data["is_half_day"]=="2"){
                                
                              }
                            }
                            echo $leaveused;
                            ?>
                        </td>
                        <td><?php echo $total_leave-$leaveused; ?></td>
                        <td>
                            <?php  $monthleave = 0;
                                    $monthlyleaves = $db->getRows("select * from teacher_leave where (date between '$fdate' and '$tdate') and teacher_id='$clist[idno]' and leave_status='1' and school_id='".$_SESSION["admin"]["school_id"]."' ");
                                    foreach($monthlyleaves as $data){
                                      if($data['leave_status']=="1" && $data["is_half_day"]=="0"){
                                          $monthleave++;
                                      }
                                      if($data['leave_status']=="1" && $data["is_half_day"]=="1"){
                                          $monthleave=$monthleave+0.5;
                                      }
                                      if($data['leave_status']=="1" && $data["is_half_day"]=="2"){
                                        
                                      }
                                    }
                                    
                                    echo $monthleave;
                            ?>
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
               
            </table>
            </div>

        </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){
    $(".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>
    
    