<?php 
include("../config.php");
if(!empty($_POST)){
	$driver_code         = driveCode($conn);
	$name             	 = mysqli_real_escape_string($conn, $_POST['name']);
    $address          	 = mysqli_real_escape_string($conn, $_POST['address']);
    $city             	 = mysqli_real_escape_string($conn, $_POST['city']);
    $state            	 = mysqli_real_escape_string($conn, $_POST['state']);
    $contact          	 = mysqli_real_escape_string($conn, $_POST['contact']);
    $lic_no           	 = mysqli_real_escape_string($conn, $_POST['lic_no']);
    $lic_issue_date   	 = date('Y-m-d', strtotime($_POST['lic_issue_date']));
    $lic_valid_date   	 = date('Y-m-d', strtotime($_POST['lic_valid_date']));
    $file_img       	 = mysqli_real_escape_string($conn, strtoupper($_POST['file_img']));
    $status           	 = mysqli_real_escape_string($conn, $_POST['status']);
	$company_id          = $_SESSION['company_id'];
	$branch_id           = $_SESSION['branch_id'];
	$created_datetime    = date('Y-m-d H:i:s');
	$created_id          = $_SESSION['user_detail']['id'];
	$updated_datetime    = date('Y-m-d H:i:s');
	$updated_id          = $_SESSION['user_detail']['id'];
	
	$attachment = NULL;
	if(NULL != $_FILES) {
		$attachment = $_FILES;
	}

	if(isset($attachment) && !empty($attachment)) {
		$file_extension = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);

		if (!in_array($file_extension, $config['attachment']['valid_types'])) {
			echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                    die();
		}else if ($config['attachment']['file_size'] < $_FILES["image"]["size"]) {
      		echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
            die();
        }
	}
	
	$image_name         = rand(1,99999).'.'.basename($_FILES['image']['name']);
	$upload_file_path   = $config['attachment']['driver_folder_path'] . $image_name;
	
    @move_uploaded_file($attachment["image"]["tmp_name"], $upload_file_path);

	if($_POST["id"] != ''){
  	 	$type               = 'driver_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
  	 	$insert = "UPDATE driver_mst SET driver_code='$driver_code',name='$name',address='$address',city='$city',state='$state',contact='$contact',`file_img`='$image_name',company_id='$company_id',branch_id='$branch_id',status='$status',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE id='".$_POST['id']."'";
  	 	$result = mysqli_query($conn,$insert) or die( mysqli_error($conn) );
		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$acttivity_res = mysqli_query($conn,$activiti);

  }else{
	$type               = 'driver_add';
	$user_id            = $_SESSION['user_detail']['id'];
	$data               = serialize($_POST);
	$insert = "INSERT INTO `driver_mst`(`driver_code`, `name`, `address`, `city`, `state`, `contact`, `lic_no`, `lic_issue_date`, `lic_valid_date`,`file_img`, `company_id`, `branch_id`, `status`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$driver_code."','".$name."','".$address."','".$city."','".$state."','".$contact."','".$lic_no."','".$lic_issue_date."','".$lic_valid_date."','".$image_name."','".$company_id."','".$branch_id."','".$status."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";

	$result = mysqli_query($conn,$insert);
	
	$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	$acttivity_res = mysqli_query($conn,$activiti);

	
  }
  echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=driver/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=driver/add.php') );die();
} 