<link rel="stylesheet" href="https://cdn.datatables.net/fixedcolumns/3.2.6/css/fixedColumns.dataTables.min.css">
<script type="text/javascript" src="https://cdn.datatables.net/fixedcolumns/3.2.6/js/dataTables.fixedColumns.min.js"></script>

<?php if(isset($_SESSION['msg']) ){?>
<div class="alert alert-info">
<h4>Info!</h4>
<?php echo $_SESSION['msg']; unset($_SESSION['msg']); ?>
</div> <?php }  ?>
 <?php
 if(isset($_REQUEST['submit']))
 {

    $date	 	           = date("Y-m-d",strtotime($_POST['date']));
    $state	 	           = $_POST['state'];
    $user_list 	           = $_POST['user_list'];
    $users				   = explode("&",$user_list);
    foreach($users as $key)
    {
        $emp_id			   = $key;
        $mark			   = $_POST["att_type_".$key];
        $sql			   = mysql_query(" select * from `tbl_attendence` where `emp_id`='$emp_id' and `date`='$date' ")or die(mysql_error());
        if(mysql_num_rows($sql)>0)
        {
            $insert		   	   = mysql_query(" update `tbl_attendence` set `mark`='$mark' where `emp_id`='$emp_id' and `date`='$date' ")or die(mysql_error());
            $_SESSION['msg']   = 'Attendence Updated Sucessfully!!';
        }
        else
        {
            $insert			   = mysql_query(" insert into `tbl_attendence` set `emp_id`='$emp_id', `state`='$state', `date`='$date', `mark`='$mark' ")or die(mysql_error());
            $_SESSION['msg']   = 'Attendence Added Sucessfully!!';
        }
    }
    header("location:home.php?page=add_atten");
 }
 ?>
<div class="row-fluid" >
    <div class="span12">
        <div class="head clearfix">
            <div class="isw-ok"></div>
            <h1>Add Old Attendence</h1>
        </div>
        <div class="block-fluid">
            <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
              <fieldset>
              <?php
               if(isset($_REQUEST['page_id']))
               {
                   $id            =       $_REQUEST['page_id'];
                   $modify_menu   =       mysql_query("SELECT * FROM `tbl_attend_type` WHERE `id` = '$id'");
                   $show_modify   =       mysql_fetch_array($modify_menu);
               }
              ?>
              <input type="hidden" name="id" placeholder="Page Name" <?php if(isset($_REQUEST['page_id'])){?> required value="<?php echo $show_modify['id'];  ?>"<?php } ?> id="txt_name" autocomplete="off" />
                     <div class="row-form clearfix">
                        <div class="span2">
                        	<label>From Date</label>
                        	<input type="text" class="datetimepicker2" required autocomplete="off" name="from_date" placeholder="Date" value="<?php echo date("d-m-Y");?>" id="from_date"/>
                        </div>
                        <div class="span2">
                        	<label>To Date</label>
                        	<input type="text" class="datetimepicker2" required autocomplete="off" name="to_date" placeholder="Date" value="<?php echo date("d-m-Y");?>" id="to_date"/>
                        </div>
                        <div class="span2">
                        	<label>State</label>
                            <select name="state" id="state" onchange="get_staff();" required>
                            <option value="">Select State</option>
                            <?php
                            if($_SESSION['userid']==4)
                            {
                                $st		= array();
                                $sql_st	= mysql_query("SELECT distinct `state` FROM `tbl_emp_records` ORDER BY `state` ASC ")or die(mysql_error());
                                while($row_st= mysql_fetch_array($sql_st))
                                {
                                    array_push($st,$row_st['state']);
                                }
                            }
                            else
                            {
                                $st	  = explode('&',$_SESSION['post']);
                            }
                            foreach($st as $key)
                            {
                                $type = '';
                                if(isset($_REQUEST['page_id'])&&($show_modify['state']==$key)){ $type ='selected';}
                                echo "<option value='$key' $type>$key</option>";
                            }
                            ?>
                            </select>
                        </div>
                        <div class="span2">
                    	<label>&nbsp;</label>
                        <img src="images/loader9.gif" style="display:none;" id="load_img"/>
                        </div>
                     </div>
                     <div class="row-form clearfix"></div>
                     <div id="st_user_list">

                     </div>

              </fieldset>
            </form>
            <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
            	<input type="hidden" id="state_val" value="<?php echo $_SESSION['post'] ;?>" />
                <input type="hidden" id="month_val" value="<?php echo date("M, Y") ;?>" />
              	<div class="row-form clearfix">
                        <div class="span12">
                        <p>Please Click on below button after completing attandence for the month of <strong><?php echo date("M, Y");?>.</strong></p>
                        <p>कृपया संबन्धित महीने की उपस्थिति दर्ज करने के बाद नीचे दिए SUBMIT Button पर Click कर के हमे अपना Confirmation दे </p>
                        <p id="mail_msg" style="color:red;text-align:center;display:none;">Mail Successfully Sent to Admin</p>
                        </div>
                </div>
                <div class="row-form clearfix">
                	<div class="span2">
                    	<label>&nbsp;</label>
                        <img src="images/loader9.gif" style="display:none;" id="mail_load_img"/>
                        </div>
                    <div class="span2">
                	<input type="button" value="Click Here" style="background:#F00;" class="btn btn-block" onclick="return SendMailCompelteAddendence();" />
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function get_staff()
{
    document.getElementById("st_user_list").innerHTML="";
    $("#load_img").css("display","block");

    var from_date    = $("#from_date").val();
    var state   	 = $("#state").val();
    var to_date 	 = $("#to_date").val();
    if(new Date(from_date)>new Date(to_date))
    {
        alert("Please Select Right Date Period !!!!!!!");
        return false;
    }
    else if (state == '') {
        alert("Please Select State !!!");
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
              ///  alert(xmlhttp.responseText);
                var result  = JSON.parse(xmlhttp.responseText);
                var data = '<table style="width:100%" class="table" id="example">';
                ////Table Heading
                data += '<thead><tr><th>S No.</th><th>Staff Name</th>';
                var date_list = result.date_list;
                $.each( date_list, function(i, n){
                     data += '<th style="text-align:center;">'+n+'</th>';
                });
                data += '</tr></thead>';
              ///Table Rows
                data += '<tbody>';
                var emp_list  = result.emp_list;
                $.each( emp_list, function(i, n)
                {
                    data += '<tr><td>'+parseInt(i+1)+'</td><td>'+n.name+'</td>';
                    var emp_status = emp_list[i].emp_status;
                    $.each( emp_status, function(i1, n1)
                    {
                        var state  = "'"+n1.state+"'";
                        var date   = "'"+n1.date+"'";
                        data += '<td id="'+n1.state+'_'+n1.date+'_'+n.emp_id+'"><select name="" style="width:100px;" onchange="submit_attendence(this.value,'+date+','+n.emp_id+','+state+');"><option value="">--Select--</option>';
                        var emp_status = emp_list[i].emp_status;
                        var emp_status_option = emp_status[i1].emp_status_option;
                        $.each( emp_status_option, function(i1, n1)
                        {
                            data += '<option value="'+emp_status_option[i1].key+'" '+emp_status_option[i1].name_status+' >'+emp_status_option[i1].name+'</option>';
                        });
                        data += '</select></td>';
                    });
                    data += '</tr>';
                });
                data += '</tbody>';
                data += '</table>';
              ///alert(data);
            document.getElementById("st_user_list").innerHTML=data;
            //alert(xmlhttp.responseText);
            $("#load_img").css("display","none");

        var table = $('#example').DataTable( {
        scrollY:        "300px",
        scrollX:        true,
        scrollCollapse: true,
        paging:         false,
        fixedColumns:   {
            leftColumns: 2
        }
    } );


            }
        }
        xmlhttp.open("GET","ajax/get_old_atd_user.php?state="+state+"&from_date="+from_date+"&to_date="+to_date,true);
        xmlhttp.send();
    }
}
function submit_attendence(atten,atten_dt,emp_id,state)
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            document.getElementById(state+"_"+atten_dt+"_"+emp_id).innerHTML=xmlhttp.responseText;
            //		alert(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET","ajax/submit_attendence.php?atten="+atten+"&atten_dt="+atten_dt+"&emp_id="+emp_id+"&state="+state,true);
    xmlhttp.send();
}
</script>
<style>
/* Ensure that the demo table scrolls */
    th, td { white-space: nowrap; }
    div.dataTables_wrapper {
        width: 1280px;
        margin: 0 auto;
    }
</style>