<?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);
	$i                     = 0;
	foreach($users as $key)
	{
		$emp_id			   = $key;
		$mark			   = $_POST["att_type_".$key];
		$town_id		   = $_POST["town_id_".$key];
		$dis_id  		   = $_POST["dis_id_".$key];
		if(!empty($mark))
		{	
			$sql			   = mysql_query(" select * from `tbl_attendence` where `emp_id`='$emp_id' and `town_id`='$town_id' and `dis_id`='$dis_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', `town_id`='$town_id', `dis_id`='$dis_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 Attendence</h1>
        </div>
        <div class="block-fluid">
            <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
              <fieldset>
              
              <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>Date</label>
                        	<!--<input type="text" onchange="get_staff();" class="datetimepicker2" required autocomplete="off" name="date" placeholder="Date" value="<?php echo date("d-m-Y");?>" id="date"/>-->
                        	<input type="text" onblur="get_staff();" class="datetimepicker2" required autocomplete="off" name="date" placeholder="Date" value="<?php echo date("d-m-Y");?>" id="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" id="set_as_div" style="display:none;">
                        	<label>Set As</label>
                            <select name="set_as" id="set_as">
                            <option value="">Select by Status</option>
                            <?php 
                            $modify_menu         = mysql_query("SELECT * FROM `tbl_attend_type` WHERE `id` !='3' ");
                            while($show_modify   = mysql_fetch_array($modify_menu)){
                            ?>
                            <option value="<?php echo $show_modify['id'];?>"><?php echo $show_modify['details'];?></option>
                            <?php } ?>
                            </select>
                        </div>
                        <div class="span2">
                    	<label>&nbsp;</label>
                        <img src="images/loader9.gif" style="display:none;" id="load_img"/>
                        </div>
                     </div>
                     <div id="st_user_list">
                     </div>   
                    <div class="span2">
                    	<label>&nbsp;</label>
                        <input type="submit" name="submit" class="btn btn-block" value="submit"/>
                        </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>
$("#set_as").change(function () {
    	var set_as = $('#set_as option:selected').val();
    	var user_list = $("#user_list").val();
    	
        var strArr = user_list.split('&');
        var intArr = [];
        for(i=0; i < strArr.length; i++)
        { 
    	var $radios = $('input:radio[name=att_type_'+strArr[i]+']');
    	$radios.filter('[value='+set_as+']').prop('checked', true);  
        }
});
function get_distributor(state_id,emp_id,val)
{
    if (val == '') {
        alert("Please Select Town Name !!!");
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                document.getElementById(emp_id).innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","ajax/get_distributor.php?state_id="+state_id+"&town_id="+val,true);
        xmlhttp.send();
    }
    
}
function get_staff()
{
	document.getElementById("st_user_list").innerHTML="";
	$("#set_as_div").css("display","none");
	var date    = $("#date").val();
	var state   = $("#state").val();
	if (date == '') {
		alert("Please Select Date !!!");
		return;
	}
	else if (state == '') {
		alert("Please Select State !!!");
		return;
	} else {
	    $("#load_img").css("display","block");
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("st_user_list").innerHTML=xmlhttp.responseText;
				//alert(xmlhttp.responseText);
				$("#load_img").css("display","none");
				$("#set_as_div").css("display","block");
				
			}
		}
		xmlhttp.open("GET","ajax/get_atd_user.php?state="+state+"&date="+date,true);
		xmlhttp.send();
	}
}
function capital_letter(id)
{
    var arr 		  = $('#'+id).val().split(' ');
    var result 		  = "";
    for (var x=0; x<arr.length; x++)
        result+=arr[x].substring(0,1).toUpperCase()+arr[x].substring(1)+' ';
    $('#'+id).val(result.substring(0, result.length-1));
}


/*setInterval(function(){ setSession(); alert();}, 3000);*/

</script>   
