<?php 
ob_start();
session_start();
include('../dbconnect.php');

function computeDuration($reqDate){

    //$reqDate;
    
    $curdate = strtotime(date('Y-m-d H:i'));
    // $curdate = strtotime(date('Y-m-d 2354'));
   
 
   $datadate = strtotime($reqDate);
    $diff =   $datadate -$curdate;
   $sec  = $diff;
   $min = floor($diff/(60));
   $hrs = floor($diff/(60*60));
   $days = floor($diff/(60*60*24));
 
 return $min;
 
 
 
 
  
   
 
 
  
 
 
}
 

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

 $random_pin = substr(rand(),0, 4);



 $sql = $conn->query("UPDATE empinfo SET upassword = '$random_pin'  WHERE empid= '".$_POST['empid']."'");

 echo $random_pin;
 




}
if(isset($_POST['getsched'])){

    $user = $_POST['empid'];
    $shift_date = $_POST['shiftdate']; 
     
     
     $query = $conn->query("SELECT * FROM empinfo where userid='$user' ");
     $row = $query->fetch_assoc();
     $work_from_home = $row['work_from_home'];
      $date_now = date('Y-m-d');
     
     $get_sched = $conn->query("SELECT * FROM schedule WHERE date='$shift_date' and empid='$user' ");
     $sched_row = $get_sched->fetch_assoc();
      $starttime = $sched_row['pststarttime'];
     $endtime = $sched_row['endtime'];
     
     $req_date = $shift_date.' '.$starttime;
     
        $minutes_duration = computeDuration($req_date);
     
     
      $get_upass = $conn->query("SELECT * FROM empinfo where userid='$user'");
      $row_pass = $get_upass->fetch_assoc();
     
      //echo $work_from_home;
     
     
     if($work_from_home == '1' && $minutes_duration <= 70 && $minutes_duration >= 0 && $row_pass['upassword'] == ''){
     
       $response['buttons'] = "";
       
     
      
     }else{
       $response['buttons']= "disabled";
     
     }

     
     echo json_encode($response);
}
?>