<?php
date_default_timezone_set('Asia/Calcutta');
$a_date = date("Y-m-d");
//echo date("Y-m-t", strtotime($a_date));
$out_date		= date("Y-m-t", strtotime($a_date));
if(isset($_POST['okey']) && !empty($_POST['okey']))
{
	
$_SESSION['att_month']          = $_POST['month'];
$_SESSION['att_year']           = $_POST['year'];
$_SESSION['att_emp_name']       = $_POST['emp_name'];	
$_SESSION['att_state']     		= $_POST['state'];	
}
function attend_type($id)
{
	$sql_st	= mysql_query("SELECT `symbol` FROM `tbl_attend_type2` where `id`='$id' ")or die(mysql_error());
	$row_st	= mysql_fetch_array($sql_st);
	return $row_st['symbol'];
}
?>
<script src="https://cdn.datatables.net/fixedcolumns/3.2.4/js/dataTables.fixedColumns.min.js"></script>
  <style>
  .table1 td{vertical-align: middle;text-align: center;}
  .color_1{background:#0C0 !important;color:#000 !important;}
  .color_2{background:#F7B008 !important;color:#fff !important;}
  .color_3{background:#33F !important;color:#FFF !important;}
  .color_4{background:#F39 !important;color:#FFF !important;}
  .color_5{background:#306 !important;color:#FFF !important;}
  .color_6{background:#90F !important;color:#FFF !important;}
  .color_7{background:#FF6 !important;color:#000 !important;}
  [class*="block"] .table tr th,td {
    padding: 4px 8px;
    font-size: 11px;
    background-color: #F1F1F1;
    color: #666;
    white-space: nowrap !important;
    }
  </style>
         
<div class="span12" style="margin-left:0px;">
    <div class="head clearfix">
        <div class="isw-ok"></div>
        <h1>RSP ATTENDANCE REPORT</h1>                        
    </div>  
    <div class="block-fluid">  
       <form  method="POST" enctype="multipart/form-data" action="" > 
        <div class="row-form clearfix">
            <div class="span1">        
                <label>Month</label>		
                <select name="month">
                    <option value="">Month</option>
                    <option value="01" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='01') { echo "selected";}  ?>>Jan</option>
                    <option value="02" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='02') { echo "selected";}  ?>>Feb</option>
                    <option value="03" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='03') { echo "selected";}  ?>>Mar</option>
                    <option value="04" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='04') { echo "selected";}  ?>>Apr</option>
                    <option value="05" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='05') { echo "selected";}  ?>>May</option>
                    <option value="06" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='06') { echo "selected";}  ?>>Jun</option>
                    <option value="07" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='07') { echo "selected";}  ?>>Jul</option>
                    <option value="08" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='08') { echo "selected";}  ?>>Aug</option>
                    <option value="09" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='09') { echo "selected";}  ?>>Sep</option>
                    <option value="10" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='10') { echo "selected";}  ?>>Oct</option>
                    <option value="11" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='11') { echo "selected";}  ?>>Nov</option>
                    <option value="12" <?php if(isset($_SESSION['att_month']) && $_SESSION['att_month']=='12') { echo "selected";}  ?>>Dec</option>
               </select>
            </div>
           
            <div class="span1">        
               <label>Year</label>		
                <select name="year">
                    <option value="">Year</option>
                    <?php for($i=2010;$i<2056;$i++){?>
                    <option value="<?php echo $i;?>" <?php if(isset($_SESSION['att_year']) && $_SESSION['att_year']==$i) { echo "selected";}  ?> ><?php echo $i;?></option>
                    <?php } ?>
                </select>
            </div>  
            
            <!--<div class="span3">
                <label>State</label>
                <select name="state" onchange="get_staff_name(this.value);" required>
                    <option value="">SELECT STATE</option>
                    <?php
            		$st		= array();
            		$sql_st	= mysql_query("SELECT distinct `state` FROM `tbl_attendence_demo_team` ORDER BY `state` ASC ")or die(mysql_error());
            		while($row_st= mysql_fetch_array($sql_st))
            		{
            			array_push($st,$row_st['state']);
            		}        
                    foreach($st as $key)
                    {
                    $type = '';
                    if(isset($_SESSION['att_state'])&&($_SESSION['att_state']==$key)){ $type ='selected';}
                    echo "<option value='$key' $type>$key</option>";}?>
                </select>
            </div> --> 
            
            <div class="span3">        
            <label>Employee Name</label>
               <select name="emp_name" id="emp_name" required >
                   <option value='ALL'>ALL</option>
                   <?php
                   if(isset($_SESSION['att_state']))
                    {
                        
                        $sql_st	= mysql_query("SELECT `emp_name`,`id`,`fname` FROM `tbl_emp_demo` where `state`='".$_SESSION['att_state']."' and `status`='0' ORDER BY `emp_name` ASC")or die(mysql_error());
                        while($row_st= mysql_fetch_array($sql_st))
                        {
                        ?>
                        <option value="<?php echo $row_st['id'];?>" <?php if($row_st['id']==$_SESSION['att_emp_name']){echo "selected";}?>><?php echo $row_st['emp_name'];?> S/o <?php echo $row_st['fname'];?></option>
                        <?php }}?>
               </select>
            </div>
           
            <div class="span2"> 
                <label>&nbsp;</label>
                <input type="submit" name="okey" value="Search"/>
            </div>
            </div>                       
   </form>
   </div>   
   
</div>
                
<?php 
if(isset($_REQUEST['okey']))
{
  unset($_SESSION['att_emp_name']);unset($_SESSION['att_year']);unset($_SESSION['att_month']);
  /// days calculate in a month
	 $month	= $_SESSION['att_month']	= $_REQUEST['month'];
	 $year	= $_SESSION['att_year']		= $_REQUEST['year'];
	 $d		= cal_days_in_month(CAL_GREGORIAN,$month,$year);
	 $_SESSION['att_emp_name']  =  $emp_name 	= $_REQUEST['emp_name'];
	 $_SESSION['att_state']     =  $state	 	= $_REQUEST['state'];
?>         
<div class="span12" style='margin-top: 10px;margin-left: 0px;'>                    
        <div class="head clearfix">
            <div class="isw-grid"></div>
            <h1>RSP ATTENDANCE REPORT</h1>  
            <a onClick="print_div('print_div')" style="float:right;padding:4px;"><img src="images/print.png"  width="30px" height="30px;"/></a>                                                   
        </div>
        <div class="block-fluid" id="print_div">
        <h4 align="center">RUDRAANSH HOLDINGS ATTENDENCE REPORT</h4>  
        <p style="font-size:13px;"><span style="float:left;"><strong>Month-Year: <?php echo $month ; ?>-<?php echo $year ; ?></strong></span></p>
        <table id="example" class="table" cellspacing="0" width="100%" border="1">
            <thead>
            <tr>
                <th width="15%">Staff Name</th>
                <th width="15%">Staff Code</th>
                <th width="15%">Father's Name</th>
                <?php 
                for($i=1;$i<=$d;$i++) { if($i<10) echo "<th width='2%'>0".$i."</th>";else echo "<th width='2%'>".$i."</th>"; }?>
                <th width="5%">Present</th>
                <th width="5%">Off</th>
                <th width="5%">Leave</th>
                <th width="5%">Absent</th>
                <th width="5%">Weekly Off</th>
                <th width="5%">Half Day</th>
                <th width="5%">Seprated</th>
            </tr>
            </thead>
            <tbody>
            <?php
            if($emp_name=='ALL') 
            {
              //$sql			= mysql_query(" SELECT * FROM `tbl_emp_demo` WHERE `status`='0' and `state`='$state' order by `emp_name` ASC")or die(mysql_error());
              $sql			= mysql_query(" SELECT * FROM `tbl_emp_demo` WHERE `status`='0' order by `emp_name` ASC")or die(mysql_error());
            }
            else
            {
              //$sql			= mysql_query(" SELECT * FROM `tbl_emp_demo` WHERE `status`='0' and `state`='$state' and `id`='$emp_name' ")or die(mysql_error());
              $sql			= mysql_query(" SELECT * FROM `tbl_emp_demo` WHERE `status`='0' and `id`='$emp_name' ")or die(mysql_error());
            }
            $k=1;	
            while($row		= mysql_fetch_array($sql))
            {
                $emp		= $row['id'];
                echo "<tr><td>".$row['emp_name']."</td>";
                echo "<td>".$row['emp_code']."</td>";
				echo "<td>".$row['fname']."</td>";                
				$p=0;$a=0;$l=0;$h=0;$o=0;$wo=0;$s=0;
                for($i=1;$i<=$d;$i++) { 
                if($i<10)
                {	$date		= $year."-".$month."-0".$i;}
                else
                {	$date		= $year."-".$month."-".$i; }
                if($date<=$out_date)
                {		
                    $sql2 = mysql_query(" SELECT * FROM `tbl_attendence_demo_team` WHERE `date`='$date' and `emp_id` = '$emp' " )or die(mysql_error());
                    if(mysql_num_rows($sql2)>0)
                    {
                        $row2		= mysql_fetch_array($sql2);
						if($row2['mark']==1) 	$p	= $p + 1;
						if($row2['mark']==2) 	$o	= $o + 1;
						if($row2['mark']==3) 	$l	= $l + 1;
						if($row2['mark']==4) 	$a	= $a + 1;
						if($row2['mark']==5) 	$wo	= $wo + 1;
						if($row2['mark']==6) 	$h	= $h + 1;
						if($row2['mark']==7) 	$s	= $s + 1;
						//if($row2['mark']==8) 	$s	= $s + 1;
						//if($row2['mark']==9) 	$s	= $s + 1;
                        echo "<td class='color_".$row2['mark']."'><a href='' title='Town Name :: ".($row2['town_id'])."\nDistributor Name :: ".($row2['dis_id'])."'>".attend_type($row2['mark'])."</a></td>";
                    }
                    else
                    {
                        echo "<td style='background:red;'></td>";
                    }
                    
                }
                else
                {
                    echo "<td class='n_color'>-</td>"; 
                }
                
                }
				echo "<td>".$p."</td>";
				echo "<td>".$o."</td>";
				echo "<td>".$l."</td>";
				echo "<td>".$a."</td>";
				echo "<td>".$wo."</td>";
				echo "<td>".$h."</td>";
                echo "<td>".$s."</td>";
				echo "</tr>";
                $k++;
            }
            ?>
            </tbody>
        </table>
</div>
</div>
<?php }?>
<script>
function get_staff_name(state)
{
	if (state == '') {
		alert("Please Select State !!!");
		return;
	} else {
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("emp_name").innerHTML=xmlhttp.responseText;
				//alert(xmlhttp.responseText);
			}
		}
		xmlhttp.open("GET","ajax/get_rsp_name.php?state="+state,true);
		xmlhttp.send();
	}
}
$(document).ready(function() {
    var table = $('#example').DataTable( {
        scrollY:        "500px",
        scrollX:        true,
        scrollCollapse: true,
        paging:         false,
        fixedColumns:   {
            leftColumns: 1
        }
    } );
} );
function print_div(print_div)
{
var newstr = document.getElementById("print_div").innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = newstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
function popup(emp_id,M,Y)
{
 var url	= 'pages/attendece/popup.php?emp_id='+emp_id+"&M="+M+"&Y="+Y;	
 var width  = 950;
 var height = 600;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=yes';
 params += ', status=no';
 params += ', toolbar=no';
 newwin22=window.open(url,'windowname22', params);
 if (window.focus) {newwin22.focus()}
}
</script>