<?php
include('../include/header.php');

$sql_atd = "select *from tbl_emp_attendance where emp_id='".$row_emp['emp_id']."' and date_ = '".date('Y-m-d')."' ";
						$result_atd = $objhr->viewdata($sql_atd);
						
						
	
	
	function gps2Num($coordPart){
    $parts = explode('/', $coordPart);
    if(count($parts) <= 0)
    return 0;
    if(count($parts) == 1)
    return $parts[0];
    return floatval($parts[0]) / floatval($parts[1]);
}
function get_image_location($image = ''){

    $exif = exif_read_data($image, 0, true);
    if($exif && isset($exif['GPS'])){
        $GPSLatitudeRef = $exif['GPS']['GPSLatitudeRef'];
        $GPSLatitude    = $exif['GPS']['GPSLatitude'];
        $GPSLongitudeRef= $exif['GPS']['GPSLongitudeRef'];
        $GPSLongitude   = $exif['GPS']['GPSLongitude'];
        
        $lat_degrees = count($GPSLatitude) > 0 ? gps2Num($GPSLatitude[0]) : 0;
        $lat_minutes = count($GPSLatitude) > 1 ? gps2Num($GPSLatitude[1]) : 0;
        $lat_seconds = count($GPSLatitude) > 2 ? gps2Num($GPSLatitude[2]) : 0;
        
        $lon_degrees = count($GPSLongitude) > 0 ? gps2Num($GPSLongitude[0]) : 0;
        $lon_minutes = count($GPSLongitude) > 1 ? gps2Num($GPSLongitude[1]) : 0;
        $lon_seconds = count($GPSLongitude) > 2 ? gps2Num($GPSLongitude[2]) : 0;
        
        $lat_direction = ($GPSLatitudeRef == 'W' or $GPSLatitudeRef == 'S') ? -1 : 1;
        $lon_direction = ($GPSLongitudeRef == 'W' or $GPSLongitudeRef == 'S') ? -1 : 1;
        
        $latitude = $lat_direction * ($lat_degrees + ($lat_minutes / 60) + ($lat_seconds / (60*60)));
        $longitude = $lon_direction * ($lon_degrees + ($lon_minutes / 60) + ($lon_seconds / (60*60)));

        return array('latitude'=>$latitude, 'longitude'=>$longitude);
    }else{
        return false;
    }
}

?>

 <!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Bread crumb -->
            <div class="row page-titles">
                <div class="col-md-5 align-self-center">
                    <h3 class="text-primary">Dashboard</h3> </div>
                <div class="col-md-7 align-self-center">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item active">Submit Attendance</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->
           <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row justify-content-center">
                    <div class="col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="form-validation">
								<?php
									if($result_atd->num_rows > 0){
								?>
								<h4 style="color:#009933;">Today's Attendance Submitted.</h4>
								<?php }else{?>
                                    <form class="form-valide" method="post" enctype="multipart/form-data">
                                       
                                        
										<div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username"> Submitting Attendance By? <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <select class="form-control" id="submit_by" name="submit_by" onchange="sub_by()" required >
												<option value="">Select</option>
												<option value="1">Office</option>
												<option value="2">Field</option>
												
												</select>
											
                                            </div>
                                        </div>
										
										<div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username"> </label>
                                            <div class="col-lg-6">
                                                <input type="text" style="display:none"  class="form-control" id="num_of_day" name="num_of_day" placeholder="Attendance Pass" >
												<input type="file" style="display:none" class="form-control" id="files" name="files" accept="image/*" capture="camera"  >
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
                                                <button type="submit" style="background:#009933; color:#FFFFFF;" class="btn" name="save">Presented</button>
                                            </div>
                                        </div>
										<div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
                                               <p id="inst" style="display:none"><span class="text-danger" style="font-size:18px;">Instruction:</span> 1] To Upload photo first turn on your GPS(location). <br />
											   2] Next open camera, go to camera setting and turn on location info.<br />
						3] Click and Upload.
											   </p>
                                            </div>
                                        </div>
                                    </form>
									<?php }?>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
			
<?php

if(isset($_POST['save'])){

$sql_ecod = "select * from tbl_emp_master where id='".$row_emp['emp_id']."' ";
						$result_ecod = $objhr->viewdata($sql_ecod);
					$rows_ecod = $result_ecod->fetch_assoc();
if($_POST['submit_by']==1){
$sub_by = $_REQUEST['submit_by'];
$num = $_REQUEST['num_of_day'];
$date = date('Y-m-d');

 $sql_cod = "select * from tbl_attendance_pass where date='$date' ";
						$result_cod = $objhr->viewdata($sql_cod);
					$rows_cod = $result_cod->fetch_assoc();
					if($num==$rows_cod['code']){
					
 $sql3 = "INSERT INTO `tbl_emp_attendance` (`emp_id`, `present_type_id`, `date_`, `status`) VALUES ('".$row_emp['emp_id']."', '1', '$date', '1');";
	$result3 = $objhr->insertion($sql3);
						echo "<script>alert('Attendance submitted'); location='';</script>";
						}else{echo "<script>alert('Sorry, Your Attendance Not Submitted, Please Check Pass.'); location='';</script>";}
						}
						
						
						if($_POST['submit_by']==2){

    $errors= array();
	 

		$file_name = $_FILES['files']['name'];
		$random_digit=date('Y_m_d');
		 $ext = pathinfo($file_name , PATHINFO_EXTENSION);
		 $file_name=$random_digit.".".$ext;
		$file_tmp =$_FILES['files']['tmp_name'];
		$file_type=$_FILES['files']['type'];	
      		
		
        $desired_dir="images/location/".$rows_ecod['emp_code'];
        
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);		// Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{									// rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;				
            }
			  $imageURL = "images/location/".$rows_ecod['emp_code']."/".$file_name;
     //get location of image
$imgLocation = get_image_location($imageURL);
if(!empty($imgLocation)){
//latitude & longitude
$imgLat = $imgLocation['latitude'];
$imgLng = $imgLocation['longitude'];

  $str = "INSERT INTO `tbl_loc_dtl` (`ent_dt`, `emp_id`, `lat`, `lang`, `image`) VALUES ('".date('Y-m-d')."', '".$row_emp['emp_id']."', '$imgLat', '$imgLng', '$imageURL');";
	$result=$objhr->viewdata($str);
	
	
	 $sql3 = "INSERT INTO `tbl_emp_attendance` (`emp_id`, `present_type_id`, `date_`, `status`) VALUES ('".$row_emp['emp_id']."', '1', '".date('Y-m-d')."', '1');";
	$result3 = $objhr->insertion($sql3);
		 
		?>
		<script>
		alert('Attendance submitted.'); 
        location="";
		</script>
		<?php
	
			
}
if(empty($imgLocation)){
 
?>
		<script>
		alert('error: location not found on image, Your Attendance not submitted.'); 
        location="";
		</script>
		<?php
}	

  
		 
		
				
       
    

  
						
						}
						
						
						
						
}
?>
<?php
include('../include/footer.php');
?>
<script>
function sub_by(){
var sub_val = $('#submit_by').val();

if(sub_val==1){
$('#num_of_day').show();
$('#files').hide();
$('#inst').hide();
}else if(sub_val==2){
$('#num_of_day').hide();
$('#files').show();
$('#inst').show();
}else{
$('#num_of_day').hide();
$('#files').hide();
$('#inst').hide();
}
}
</script>