<?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);
}
if(isset($_POST['reportSched'])){
  $column = array('employee_id', 'firstname', 'team', 'date','pststarttime', 'starttime');

  $team =$conn->query("SELECT * FROm team where name='".$_POST['empid']."'");
  if($team->num_rows > 0){

    $query = "
    SELECT empdetails.team,firstname,surname,schedule.date,pststarttime,pstendtime,starttime,endtime, schedule.empid AS 'employee_id' FROM schedule JOIN empinfo ON schedule.empid =  empinfo.empid JOIN empdetails ON schedule.empid = detailid WHERE empdetails.team = '".$_POST['empid']."' AND schedule.date BETWEEN '".$_POST['datefrom']."' AND '".$_POST['dateto']."'

    ";


  }else{

    if($_POST['empid'] == 'All'){

      $query = "
      SELECT empdetails.team,firstname,surname,schedule.date,pststarttime,pstendtime,starttime,endtime, schedule.empid AS 'employee_id' FROM schedule JOIN empinfo ON schedule.empid =  empinfo.empid JOIN empdetails ON schedule.empid = detailid WHERE  schedule.date BETWEEN '".$_POST['datefrom']."' AND '".$_POST['dateto']."'
  
      ";
  
    }else{
  
      $query = "
      SELECT empdetails.team,firstname,surname,schedule.date,pststarttime,pstendtime,starttime,endtime, schedule.empid AS 'employee_id' FROM schedule JOIN empinfo ON schedule.empid =  empinfo.empid JOIN empdetails ON schedule.empid = detailid WHERE schedule.empid='".$_POST['empid']."' AND schedule.date BETWEEN '".$_POST['datefrom']."' AND '".$_POST['dateto']."'
  
      ";
  
    }

  }



  
   $query .= ' ORDER BY  schedule.empid ASC,schedule.date ASC ';
   
  
  if($_POST["length"] != -1)
  {
    $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
  }
    $statement = $conn->query($query);
  
    $number_filter_row = $statement->num_rows;
    echo $conn->error;

    $statement = $conn->query($query.$query1);
    //echo  $query;

    $result = mysqli_fetch_all($statement, MYSQLI_ASSOC);
  foreach($result as $row){
    $sub_array = array();

    $sub_array[]  = $row['employee_id'];
    $sub_array[]  = $row['firstname'].' '.$row['surname'];
    $sub_array[]  = $row['team'];
    $sub_array[]  = date('m/d/Y', strtotime($row['date']));

    $sub_array[]  = $row['starttime'] == 'OFF' || $row['starttime'] == 'HOLIDAY OFF'? $row['starttime'].' - '.$row['endtime'] : date('h:i A', strtotime($row['starttime'])).' - '.date('h:i A', strtotime($row['endtime']));

    $data[] = $sub_array;
    
  }

  function count_all_data($conn,$qry)
    {
        
  
    
        $statement = mysqli_query($conn, $qry);
     return mysqli_num_rows($statement);
    }
    
    $output = array(
     "draw"       =>  intval($_POST["draw"]),
     "recordsTotal"   =>  count_all_data($conn, $query),
     "recordsFiltered"  =>  $number_filter_row,
     "data"       =>  $data
    );
    
    echo json_encode($output);

    
    

}

if(isset($_GET['export'])){

  $f = fopen('php://memory', 'w'); 
  // loop over the input array


  $team =$conn->query("SELECT * FROm team where name='".$_GET['empid']."'");
  if($team->num_rows > 0){

    $result = $conn->query("
    SELECT empdetails.team,firstname,surname,schedule.date,pststarttime,pstendtime,starttime,endtime, schedule.empid AS 'employee_id' FROM schedule JOIN empinfo ON schedule.empid =  empinfo.empid JOIN empdetails ON schedule.empid = detailid WHERE empdetails.team = '".$_GET['empid']."' AND schedule.date BETWEEN '".$_GET['datefrom']."' AND '".$_GET['dateto']."'

    ");


  }else{

    if($_GET['empid'] == 'All'){

      $result = $conn->query("
      SELECT empdetails.team,firstname,surname,schedule.date,pststarttime,pstendtime,starttime,endtime, schedule.empid AS 'employee_id' FROM schedule JOIN empinfo ON schedule.empid =  empinfo.empid JOIN empdetails ON schedule.empid = detailid WHERE  schedule.date BETWEEN '".$_GET['datefrom']."' AND '".$_GET['dateto']."'
  
      ");
  
    }else{
  
      $result = $conn->query("
      SELECT empdetails.team,firstname,surname,schedule.date,pststarttime,pstendtime,starttime,endtime, schedule.empid AS 'employee_id' FROM schedule JOIN empinfo ON schedule.empid =  empinfo.empid JOIN empdetails ON schedule.empid = detailid WHERE schedule.empid='".$_GET['empid']."' AND schedule.date BETWEEN '".$_GET['datefrom']."' AND '".$_GET['dateto']."'
  
      ");
  
    }

  }

  $header = ['Employee ID','Name','Team','Date','Schedule(Manila)'];\
  fputcsv($f, $header); 


  while ($row = $result->fetch_assoc()) { 
      // generate csv lines from the inner arrays
      $pststarttime = $row['pststarttime'] == 'OFF' || $row['pststarttime'] == 'HOLIDAY OFF'? $row['pststarttime'].' - '.$row['pstendtime'] : date('h:i A', strtotime($row['pststarttime'])).' - '.date('h:i A', strtotime($row['pstendtime']));

      $mnlstartime =  $row['starttime'] == 'OFF' || $row['starttime'] == 'HOLIDAY OFF'? $row['starttime'].' - '.$row['endtime'] : date('h:i A', strtotime($row['starttime'])).' - '.date('h:i A', strtotime($row['endtime']));


      $empid = $row['employee_id'];
      $name = $row['firstname'].' '.$row['surname'];
      $team = $row['team'];
      $date =date('m/d/Y', strtotime($row['date']));


      $line = [$empid, $name, $team, $date, $mnlstartime];

      fputcsv($f, $line); 
  }
  // reset the file pointer to the start of the file
  fseek($f, 0);

  $filename=$_GET['empid'].'_'.'schedule.csv';
  // tell the browser it's going to be a csv file
  header('Content-Type: application/csv');
  // tell the browser we want to save it instead of displaying it
  header('Content-Disposition: attachment; filename="'.$filename.'";');
  // make php send the generated csv lines to the browser
  fpassthru($f);
}

?>