<?php
include("../../config/database.php");
$conct  = new connectionCreat();
$conct->connectToDB();
$conct->selectDB();
function addten_type($id)
{
	$sql			   = mysql_query(" select `details` from `tbl_attend_type` where `id`='$id' ")or die(mysql_error());
	$row			   = mysql_fetch_array($sql);	
	return $row['details'];
}
$from_date			= date("Y-m-d",strtotime($_GET['from_date']));
$to_date			= date("Y-m-d",strtotime($_GET['to_date']));
$state				= $_GET['state'];
$attand_typ['id']	= array();
$user_list			= array();
$attand_typ['sym']	= array();
$sql				= mysql_query("SELECT * FROM `tbl_attend_type` where `status`='1' order by `sort_order` ASC")or die(mysql_error());
while($row			= mysql_fetch_array($sql))
{
	array_push($attand_typ['id'],$row['id']);
    array_push($attand_typ['sym'],$row['details']);
}
$begin      = new DateTime( $from_date );
$end        = new DateTime( $to_date);
for($j = $begin; $begin <= $end; $j->modify('+1 day'))
{
    $j++;
    $date   =  $j->format("Y-m-d");
    $date_list[] = date("d-M",strtotime($date));
}
$i					= 0;
$sql_sub  			=  mysql_query("select * from `tbl_emp_records` where `state`='$state' and `status`='0' order by `emp_name` ASC") or die(mysql_error());
if(mysql_num_rows($sql_sub)>0)
{
    while($row_sub		= mysql_fetch_array($sql_sub))
    {$i++;
                $begin = new DateTime( $from_date );
                $end   = new DateTime( $to_date);
                $emp_status          = array();
                for($j = $begin; $begin <= $end; $j->modify('+1 day'))
                {
                  $j++;
                  $date   		= $j->format("Y-m-d");
                  $name_status  = "";
    			  $sql_check	= mysql_query("SELECT * FROM `tbl_attendence` where `emp_id`='".$row_sub['id']."' and `date`='$date' ")or die(mysql_error());
                  $emp_status_option   = array();
    			  if(mysql_num_rows($sql_check)==0)
                  {
    					$k	= 0;
                        $name_status = "";
    					foreach($attand_typ['id'] as $key)
    					{
    					    $emp_status_option[]     = array("key"=>$key,"name"=>$attand_typ['sym'][$k],"name_status"=>$name_status );
    						$k++;
    					}
    			  }
    			  else
    			  {
    				    $row_check	= mysql_fetch_array($sql_check);
    			    	$k	= 0;
    					foreach($attand_typ['id'] as $key)
    					{
                            if($key==$row_check['mark'])
                            {
                                $name_status = "selected";
                            }
                            else
                            {
                                $name_status = "";
                            }
    					    $emp_status_option[]     = array("key"=>$key,"name"=>$attand_typ['sym'][$k],"name_status"=>$name_status );
    						$k++;
    					}
    			  }
                  $emp_status[]     = array("state"=>$state,"date"=>$date,"emp_status_option"=>$emp_status_option );
    			}
    $emp_list[]     = array("name"=>$row_sub['emp_name']." F/H/o ".$row_sub['fname'],"emp_id"=>$row_sub['id'],"emp_status"=>$emp_status);
}
$arr  = array("date_list"=>$date_list,"emp_list"=>$emp_list);
}
else
{
    $arr  = array("msg"=>"No Employee found!!!");
}
echo json_encode($arr)  ;
mysql_close();
?>