<?php 
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";
$update_at = date("Y-m-d H:i:s");


if(isset($_POST['type']) && $_POST['type']=='scanning_runsheet_insert_api'){
    try{
        $pdo->beginTransaction(); 
        
        
        $insert_ids = implode(',', $_POST['updated_ids']);
        $count = count($_POST['updated_ids']);
        
        
        $driver_id = $_POST['driver_id'];
        $driver_name = $_POST['driver_name'];
        $driver_mobile_no = $_POST['driver_mobile_no'];
        
        
        $vehicle_no = $_POST['vehicle_no'];
        $delivery_date = $_POST['delivery_date'];
        
        $delivery_boy_id = $_POST['delivery_boy_id'];
        $delivery_boy_name = $_POST['delivery_boy_name'];
        $delivery_boy_mobile_no = $_POST['delivery_boy_mobile_no'];
        
        $branch_id = $_POST['branch_id'];
        
        
$statement_branch = $pdo->prepare("SELECT * FROM `tbl_branch` WHERE id=?");  
$statement_branch->execute(array($branch_id));
$branch_results = $statement_branch->fetchAll(PDO::FETCH_ASSOC);
$totalDatass = $statement_branch->rowCount();
if($totalDatass>0){
    $branch_name = $branch_results[0]['name'];
}
        
        
                $sheet_no="1000";
			    $statementss = $pdo->prepare("SHOW TABLE STATUS WHERE `Name` = 'tbl_delivery_run_sheet'");
                $statementss->execute();
                $memeber_id_result = $statementss->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($memeber_id_result as $resultss) {
			        $sheet_no=$sheet_no+$resultss['Auto_increment'];
                }
                $sheet_no = "VDRS".$sheet_no; 
                


        $statement1 = $pdo->prepare("INSERT INTO tbl_delivery_run_sheet (`driver_id`, `driver_name`, `driver_mobile_no`,
                                        `delivery_boy_id`, `delivery_boy_name`, `delivery_boy_mobile_no`, `vehicle_no`, 
                                            `date`, `shipment_ids`, `status`, `shipment_count`, `drs_no`, `branch_id`) 
                                            VALUES (?,?,?,
                                            ?,?,?,?,
                                            ?,?,?,?,?,?)");
        $result = $statement1->execute(array($driver_id, $driver_name, $driver_mobile_no,
                                        $delivery_boy_id, $delivery_boy_name, $delivery_boy_mobile_no, $vehicle_no,
                                            $delivery_date, $insert_ids, 1, $count, $sheet_no, $branch_id));
        
        
        $run_sheet_id = $pdo->lastInsertId();
        
        // Tracking and Pickup Entry - Update Status Starts Here
        
        foreach($_POST['updated_ids'] as $ids){     
            
            
            $update_status_name = "OUT FOR DELIVERY";
            $pickup_status = 4;
            $user_id = $session_id;
    	    $location = $branch_name;
    	    $location_id = $branch_id;
    	    $status = $update_status_name;
    	    $description = $update_status_name;
    	    $remark = "";
    	    $receiver_name = "";
    	    $receiver_mobile = "";
    	    $relation = "";
    	    $reason = "";
    	    $delivery_date = $_POST['delivery_date'];
        
        
        $statement1 = $pdo->prepare("UPDATE pickup_entry SET status_code=?, status=?, last_updated_status_date=? WHERE id=?");
        $statement1->execute(array($pickup_status, $update_status_name, $delivery_date, $ids));
            
            
        $statement1 = $pdo->prepare("INSERT INTO tbl_tracking (pickup_entry_id, user_id, location, location_id, status, 
    	                        status_code, description, remark, receiver_name, receiver_mobile, 
    	                        relation, reason, delivery_date, created_at) 
                        	                 VALUES (?,?,?,?,?,
                        	                 ?,?,?,?,?,
                        	                 ?,?,?,?)");
        $result= $statement1->execute(array($ids, $user_id, $location, $location_id, $update_status_name,
                                $pickup_status, $description, $remark, $receiver_name, $receiver_mobile,
                                $relation, $reason, $delivery_date, $update_at));

       }
        
        // Tracking and Pickup Entry - Update Status Endss Here
        
        
        
        
        
       
        $pdo->commit();
        $response['error']=false;
        $response['meassge']='Delivery Run Sheet Added Successfully: ';
        $response['run_sheet_id']=$run_sheet_id;
    }catch(exception $e){
        $pdo->rollBack();
        $response['error']=true;
        $response['meassge']="Something Went Wrong";
        error_log($e);
    }
    print_r(json_encode($response));
}
?>