<?php
ob_start();
include("../dbconfig.php");
include "../dbfunction.php";
/*
echo '<pre>';
print_r($_FILES);
print_r($_POST);
echo '</pre>';
exit;
*/
//var_dump($_FILES);
//exit;
if (isset($_POST['first_name'])) {
   // include Database connection file 
   $condition=$_POST;
   // get values 
   if(isset($_POST['driver_id']))
   {
   $driver_id = $_POST['driver_id'];
   }
   else
   {
   $driver_id="";
   }
   if(isset($_POST['first_name']))
   {
   $first_name = $_POST['first_name'];
   }
   else
   {
   $first_name= "";
   }
   if(isset($_POST['phone']))
   {
   $phone = $_POST['phone'];
   }
   else
   {
   $phone="";
   }
   if(isset($_POST['dob']))
   {
   $dob= date("Y-m-d", strtotime(str_replace('/', '-', $_POST['dob'])));
   }
   else
   {
   $dob="";
   }
   if(isset($_POST['email']))
   {
   $email = $_POST['email'];
   }
   else
   {
   $email="";
   }
   if(isset($_POST['address']))
   {
   $address = $_POST['address'];
   }
   else
   {
   $address="";
   }
   if(isset($_POST['post_code']))
   {
   $post_code = $_POST['post_code'];
   }
   else
   {
   $post_code="";
   }
   if(isset($_POST['pco_number']))
   {
   $pco_number = $_POST['pco_number'];
   }
   else
   {
   $pco_number="";
   }
   if(isset($_POST['current_photo']))
   {
   $current_photo = $_POST['current_photo'];
   }
   else
   {
   $current_photo="";
   }
   if(isset($_POST['badge_number']))
   {
   $badge_number = $_POST['badge_number'];
   }
   else
   {
   $badge_number="";
   }
   
    if(isset($_POST['reg_plate']))
	 {
	 $reg_plate=$_POST['reg_plate'];
	 }
	 else
	 {
	 $reg_plate="";
	 }
	 if(isset($_POST['n_i_number']))
	 {
	 $n_i_number=$_POST['n_i_number'];
	 }
	 else
	 {
	 $n_i_number="";
	 }
   
   if(isset($_POST['insurance_date']))
   {
   $insurance_date = $_POST['insurance_date'];
    $insurance_date = date("Y-m-d", strtotime(str_replace('/', '-', $insurance_date)));
    $sql_up="UPDATE employees SET email_status1='0' WHERE id=".$driver_id;	
		 mysqli_query($con,$sql_up);
   }
   else
   {
   $insurance_date="";
   }
  
    if(isset($_POST['pco_expiry']))
	{
    $pco_expiry = date("Y-m-d", strtotime(str_replace('/', '-', $_POST['pco_expiry'])));
	 $sql_up="UPDATE employees SET email_status3='0' WHERE id=".$driver_id;	
		 mysqli_query($con,$sql_up);
    }
	else
	{
	$pco_expiry="";
	}
	if(isset($_POST['tfl_expiry']))
	{
	$tfl_expiry = date("Y-m-d", strtotime(str_replace('/', '-', $_POST['tfl_expiry'])));
	 $sql_up="UPDATE employees SET email_status2='0' WHERE id=".$driver_id;	
		 mysqli_query($con,$sql_up);
	}
	else
	{
	$tfl_expiry="";
	}
	if(isset($_POST['badge_expiry']))
	{
	$badge_expiry = date("Y-m-d", strtotime(str_replace('/', '-', $_POST['badge_expiry'])));
	}
	else
	{
	$badge_expiry="";
	}
}

///////////Get Driver Photo/////////////////////////////
if (!empty($_FILES['driver']['name'][0])) {
	
$driver_photo=$_FILES['driver']['name'][0];
$first_name = str_replace(' ', '_', $first_name);
$driver_photo=$first_name."-".$driver_photo;
}else{$driver_photo=$current_photo;

}

$sql="UPDATE `employees` SET
first_name='$first_name', 
phone= '$phone',
dob='$dob',
email= '$email',
address= '$address', 
post_code= '$post_code',
pco_number=  '$pco_number',
badge_number= '$badge_number',
pco_expiry= '$pco_expiry',
badge_expiry= '$badge_expiry',
tfl_expiry= '$tfl_expiry',
driver_photo='$driver_photo',
reg_plate= '$reg_plate',
n_i_number='$n_i_number'
WHERE id=$driver_id
";
 
//echo $sql;
//exit;
if (!mysqli_query($con,$sql)) {echo $sql;
        die('Update 02 -Error : ' . mysqli_error($con));
		exit;
         echo $sql;
}

// Now update car database
if(isset($_POST['make']))
{
   $make = $_POST['make'];
   }
   else
   {
    $make="";   
   }
   if(isset($_POST['passengers']))
   {
   $passengers = $_POST['passengers'];
   }
   else
   {
   $passengers="";
   }
   if(isset($_POST['mot_date']))
   {
   $mot_date = $_POST['mot_date'];
   $mot_date = date("Y-m-d", strtotime(str_replace('/', '-', $mot_date)));
    $sql_up="UPDATE employees SET email_status='0' WHERE id=".$driver_id;	
		 mysqli_query($con,$sql_up);
   }
   else
   {
   $mot_date="";
   }
  /* if(isset($_POST['insurance_date']))
   {
   $insurance_date = $_POST['insurance_date'];
   $insurance_date = date("Y-m-d", strtotime(str_replace('/', '-', $insurance_date)));
    $sql_up="UPDATE employees SET email_status1='0' WHERE id=".$driver_id;	
		 mysqli_query($con,$sql_up);
   }
   else
   {
   $insurance_date="";
   }*/
   if(isset($_POST['color']))
   {
   $color = $_POST['color'];
   }
   else
   {
   $color="";
   }
   if(isset($_POST['reg']))
   {
   $reg = $_POST['reg'];
   }
   else
   {
   $reg="";
   }
$sql_car="UPDATE `vehicles` SET
`make`= '$make',
`passengers`= '$passengers',
`mot_date`= '$mot_date',
`color`='$color',
insurance_date= '$insurance_date',
`reg`='$reg'
 WHERE vehicles.driver_id=$driver_id
";
//echo "CAR SQL _".$sql_car;
//exit;
if(!mysqli_query($con,$sql_car)){
	echo $sql_car;
echo '<script>alert("Driver Record was not created");</script>';
die('Insert 03 -Error : ' . mysqli_error($con));
exit;	
}


// Now upload photos in the folder
if(is_array($_FILES)) {
	//$count=count($_FILES['driver']['name']);
	
foreach ($_FILES['driver']['name'] as $name => $value){
if(is_uploaded_file($_FILES['driver']['tmp_name'][$name])) {
	
$sourcePath = $_FILES['driver']['tmp_name'][$name];
   if (!$result = mysqli_query($con, $sql)) {
      }
$targetPath = "../drivers/".$first_name."-".$_FILES['driver']['name'][$name];
//echo $sourcePath;
if(move_uploaded_file($sourcePath,$targetPath)) {
}}}}
echo "success";


?>
