<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="monthly_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="monthly_report" class="btn btn-primary" value="Monthly Report" style="margin-left:100px;" />
                    </div>
                </form>
        
            </div>
    	</div>
	</div>
	</div>

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

    if(isset($_GET["monthly_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');
            }
		?>

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

                <h5 style="padding:5px;float:left;margin-top:-15px;">
                    <span style="color:blue;">Sunday</span> - <span style="width:20px;height:20px;background:black;color:white;padding:5px;">S</span>&emsp;&emsp;
                    <span style="color:blue;">Absent</span> - <span style="width:20px;height:20px;background:red;color:white;padding:5px;">A</span>&emsp;&emsp;
                    <span style="color:blue;">Holiday</span> - <span style="width:20px;height:20px;background:#00CED1;color:white;padding:5px;">H</span>&emsp;&emsp;
                    <span style="color:blue;">Present</span> - <span style="width:20px;height:20px;background:green;color:white;padding:5px;">P</span>&emsp;&emsp;
                    <span style="color:blue;">Late</span> - <span style="width:20px;height:20px;background:#FFD700;color:white;padding:5px;">L</span>&emsp;&emsp;
                    <span style="color:blue;">Early</span> - <span style="width:20px;height:20px;background:blue;color:white;padding:5px;">E</span>&emsp;&emsp;
                    <span style="color:blue;">OD</span> - <span style="width:20px;height:20px;background:brown;color:white;padding:5px;">O</span>&emsp;&emsp;
                    <span style="color:blue;">Half</span> - <span style="width:20px;height:20px;background:orange;color:white;padding:5px;">HF</span>&emsp;&emsp;
                    <span style="color:blue;">Leave</span> - <span style="width:20px;height:20px;background:purple;color:white;padding:5px;">LV</span>
                </h5>
                <table class="table" id="tblData" border="1" cellspacing="0">
                <thead>
                    <tr>
                        <th>S.No</th>
                        <th>Name</th>
                        <th>Designation</th>
               			<?php for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i)); ?>
    					<th> <?php echo $i; ?> </th>
						<?php } ?>
						<th>Working Days</th>
						<th>Total Present</th>
						<th>Total Absent</th>
						<th>Total Holidays</th>
						<th>Total Sundays</th>
						<th>Leave</th>
						<th>Half Day</th>
						<th>OD</th>
						<th>Late Days</th>
						<th>Early Departure</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>
                        <?php for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i)); ?>
                        <td>
                        <?php
                        // $ColorClass="success";
                        $daycolor = "background:red;color:white;";
                        $ColorClass="absent";
                        $msg = "A";
                        // $y=$_GET["fyear"];
    					$chkHoli=$db->getVal("select status from staff_holiday where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$y."-".$mn."-".$i."' and teacher='".$clist['idno']."'");

                if(strlen($i)==1){
                    $i='0'.$i;
                }

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

                if(strtolower($day)=="sun") {$ColorClass="danger sunday"; $msg="S"; $daycolor="background:black;color:white;"; $totalsundays++; ?> 
                    <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                    <?php   }
                elseif($chkHoli===1) {$ColorClass="danger holiday";  $msg="H";  $daycolor="background:	#00CED1;color:white;"; $totalholidays++;
					    ?>
                <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }
                elseif($chkAtt['att_type']=='OD') {$msg="O"; $od++; $totalpresent++;  $daycolor="background:brown; color:white;";
					    ?>
                <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }
                elseif($chkAtt['att_type']=='HF') {$msg="HF"; $hf++; $totalpresent++;  $daycolor="background:orange; color:white;";
					    ?>
                <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }
                elseif($chkAtt['att_type']=='LV') {$msg="LV"; $lv++;  $daycolor="background:purple; color:white;";
					    ?>
                <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                <?php   }

				elseif( $chkAtt['att_type'] == "IN") {
    					   $totalpresent++;

                            $att_time = $db->getRow("SELECT * FROM attendance_timing WHERE date = '".$y."-".$mn."-".$i."' and teacher='$clist[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="L";$daycolor="background:#FFD700;color:white;";
        					    ?>
    					        <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                                <?php
        					    }

        					    $outtime = $db->getVal("SELECT timestamp FROM attendance_new WHERE cast(timestamp as date) = '".$y."-".$mn."-".$i."' and teacher_id='".$clist["idno"]."' and att_type='OUT' and school_id='".$_SESSION["admin"]["school_id"]."' ");
            					    $att_out = strtotime($outtime); 
            					    if($att_out < $attend){$early++;
            					         $msg="E";$daycolor="background:blue;color:white;";
            					        ?>
            					            <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                                        <?php
            					    }
                            }
                            if($msg!='L' && $msg!='E'){
                                $ColorClass="success";  $msg="P";   $daycolor="background:green;color:white;";
    					        ?>
    					            <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                                <?php
                            }
    					}
                        else {
                            ?>
    					        <span type="button" class="btn myclass" style="<?php echo $daycolor; ?>"><?php echo $msg; ?></span>
                                <?php
                            if($msg=="A"){
            					    $totalabsent+=1;
            					}
                        }
    					?>
                        </td>
                        <?php } ?>
                        <td><?php echo $totalpresent+$totalabsent+$lv; ?> </td>
                        <td><?php echo $totalpresent; ?></td>
                        <td><?php echo $totalabsent; ?></td>
                        <td><?php echo $totalholidays; ?></td>
                        <td><?php echo $totalsundays; ?></td>
                        <td><?php echo $lv; ?></td>
                        <td><?php echo $hf; ?></td>
                        <td><?php echo $od; ?></td>
                        <td><?php echo $late; ?></td>
                        <td><?php echo $early; ?></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>