<?php 
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";
$today_date = date('Y-m-d');
$session_id = $session_id;

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[random_int(0, $charactersLength - 1)];
    }
    return $randomString;
}
$image_today_date = date('YmdHis');


if(isset($_POST['type']) && $_POST['type']=='create_driver'){
     try{
         
        $pdo->beginTransaction(); 
        
            $name = $_POST['name'];
            $mobile_no = $_POST['mobile_no'];
            $alter_mobile_no = $_POST['alter_mobile_no'];
            $license_no = $_POST['license_no'];
            $vehicle_no = $_POST['vehicle_no'];
            $path = $_FILES['id_proof']['name'];
            $status = $_POST['status'];
            $vehicle_type = $_POST['vehicle_type'];
            
            
            
            if($path!=''){
        		    $path_tmp = $_FILES['id_proof']['tmp_name'];  
                    $ext = pathinfo( $path, PATHINFO_EXTENSION );
        		    $file_name = basename( $path, '.' . $ext );
        		    $final_name1 = $today_date.'-'.$image_today_date.'-1'.'.'.$ext;
        		    move_uploaded_file( $path_tmp, '../img/driver-proof/'.$final_name1 );
            }else{
                    $final_name1='';
            }
            
           
         
        
        $statement = $pdo->prepare("INSERT INTO `tbl_drivers`(`name`, `mobile_no`, `alter_mobile_no`, `license_no`, `id_proof`,
                                    `vehicle_no`, `status`, `created_by`, `vehicle_type`) VALUES (?,?,?,?,?,
                                    ?,?,?,?)");
    	$result = $statement->execute(array($name, $mobile_no, $alter_mobile_no, $license_no, $final_name1,
                                	$vehicle_no, $status, $session_id, $vehicle_type));

        if($result){
            $last_inserted_id = $pdo->lastInsertId();
            $pdo->commit();
            $return_arr['message']='Drivers Added Successfully...';
            $return_arr['status']=200;

        }else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
            
            
             
     }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}


if(isset($_POST['type']) && $_POST['type']=='update_driver'){
     try{
         
        $pdo->beginTransaction(); 
        
        
            $ids = $_POST['id'];
            $name = $_POST['name'];
            $mobile_no = $_POST['mobile_no'];
            $alter_mobile_no = $_POST['alter_mobile_no'];
            $license_no = $_POST['license_no'];
            $path = $_FILES['id_proof']['name'];
            $vehicle_no = $_POST['vehicle_no'];
            $status = $_POST['status'];
            $vehicle_type = $_POST['vehicle_type'];
            
            
            if($path!=''){
    		    $path_tmp = $_FILES['id_proof']['tmp_name'];  
                $ext = pathinfo( $path, PATHINFO_EXTENSION );
    		    $file_name = basename( $path, '.' . $ext );
    		    $final_name1 = $today_date.'-'.$image_today_date.'-1'.'.'.$ext;
    		    move_uploaded_file( $path_tmp, '../img/driver-proof/'.$final_name1 );
            }else{
                $final_name1=$_POST['old_id_proof'];
            }
            
            
            
      
        $statement = $pdo->prepare("UPDATE `tbl_drivers` SET  `name`=?, `mobile_no`=?, `alter_mobile_no`=?, `license_no`=?, `id_proof`=?, 
        `status`=?, `updated_by`=?, `vehicle_no`=?, `vehicle_type`=? WHERE id=?");
       	$result = $statement->execute(array($name, $mobile_no, $alter_mobile_no, $license_no, $final_name1,
    	$status, $session_id, $vehicle_no, $vehicle_type, $ids));

    
        if($result){
            
            $pdo->commit();
            $return_arr['message']='Drivers Details Updated Successfully...';
            $return_arr['status']=200;

        }else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
            
            
             
     }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}

?>