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

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_location'){
     try{
         
        $pdo->beginTransaction(); 
        
         
         
        //  <!--age , email, experience,salary, education, mother, father,   -->
         
            $name = $_POST['name'];
            $mobile = $_POST['mobile'];
            $address = $_POST['address'];
            $status = $_POST['status'];
            
            $age = $_POST['age'];
            $email = $_POST['mail'];
            $experience = $_POST['experience'];
            $salary = $_POST['salary'];
            $education = $_POST['education'];
            $mother = $_POST['mother'];
            $father = $_POST['father'];
            
            $state = $_POST['state'];
            $pincode = $_POST['pincode'];
            $state = $_POST['state'];
            $country = $_POST['country'];
            $city = $_POST['city'];
            $branch_id = $_POST['branch_id'];
            
           
            $delivery_man_proof = $_FILES['delivery_man_proof']['name'];
            
            error_log('stephen delivery boy starts....');
            
            
            
             if(!empty($delivery_man_proof)){
        		    $path_tmp = $_FILES['delivery_man_proof']['tmp_name'];  
                    $ext = pathinfo( $delivery_man_proof, PATHINFO_EXTENSION );
        		    $file_name = basename( $delivery_man_proof, '.' . $ext );
        		    $final_name = $today_date.'-'.$image_today_date.'-1'.'.'.$ext;
        		    move_uploaded_file( $path_tmp, '../img/delivery-man-proof/'.$final_name );
            }else{
                    $final_name='';
            }
            
            
        // if (isset($name) && $name!=""){
        //     $statementsss = $pdo->prepare("SELECT * FROM `tbl_delivery_boy` WHERE name=?");
        //     $resultsss = $statementsss->execute(array($name));
        //     $totalsss = $statementsss->rowCount();    
        //     if($totalsss>0){
        //         $return_arr['status']=400;
        //         $return_arr['error'] = true;
        //         $return_arr['message'] = "This Branch Name is Already Existed!";
        //         print_r(json_encode($return_arr));
        //         return false;
        //         exit(); 
        //     }
        // }
        
        // if (isset($mobile) && $mobile!=""){
        //     $statementsss = $pdo->prepare("SELECT * FROM `tbl_delivery_boy` WHERE mobile_no=?");
        //     $resultsss = $statementsss->execute(array($mobile));
        //     $totalsss = $statementsss->rowCount();    
        //     if($totalsss>0){
        //         $return_arr['status']=400;
        //         $return_arr['error'] = true;
        //         $return_arr['message'] = "This Mobile Number is Already Existed!";
        //         print_r(json_encode($return_arr));
        //         return false;
        //         exit(); 
        //     }
        // }


        $statement = $pdo->prepare("INSERT INTO `tbl_delivery_boy`(`age`,`email`,`experience`,`salary`,`education`,`mother`,`father`,`name`, `mobile_no`, `address`, `city`, `pincode`, 
        `country`, `state`, `created_by`, `updated_by`, `branch_id`, `delivery_man_proof`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    	$result = $statement->execute(array($age,$email,$experience,$salary,$education,$mother,$father,$name, $mobile, $address, $city, $pincode,
    	$country, $state, $session_id, $session_id, $branch_id, $final_name));

        if($result){
            $last_inserted_id = $pdo->lastInsertId();
            $pdo->commit();
            $return_arr['message']='Pickup Points 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_location'){
     try{
         
        $pdo->beginTransaction(); 
            $ids = $_POST['id'];
            $name = $_POST['name'];
            $mobile = $_POST['mobile'];
            $address = $_POST['address'];
            $status = $_POST['status'];
            $age = $_POST['age'];
            $email = $_POST['mail'];
            $experience = $_POST['experience'];
            $salary = $_POST['salary'];
            $education = $_POST['education'];
            $mother = $_POST['mother'];
            $father = $_POST['father'];
            $state = $_POST['state'];
            $pincode = $_POST['pincode'];
            $state = $_POST['state'];
            $country = $_POST['country'];
            $city = $_POST['city'];
            $branch_id = $_POST['branch_id'];
            
            error_log($ids);
            
            $delivery_man_proof = $_FILES['delivery_man_proof']['name'];
            
            
             if($delivery_man_proof!=''){
    		    $path_tmp = $_FILES['delivery_man_proof']['tmp_name'];  
                $ext = pathinfo( $delivery_man_proof, PATHINFO_EXTENSION );
    		    $file_name = basename( $delivery_man_proof, '.' . $ext );
    		    $final_name = $today_date.'-'.$image_today_date.'-1'.'.'.$ext;
    		    
    		    if (file_exists('../img/delivery-man-proof/'.$_POST['old_delivery_man_proof'])) {
                    unlink('../img/delivery-man-proof/'.$_POST['old_delivery_man_proof']);
                }
    		    
    		    
    		    move_uploaded_file( $path_tmp, '../img/delivery-man-proof/'.$final_name );
            }else{
                $final_name=$_POST['old_delivery_man_proof'];
            }
            
        // if (isset($name) && $name!=""){
        //     $statementsss = $pdo->prepare("SELECT * FROM `tbl_delivery_boy` WHERE name=? and id!=?");
        //     $resultsss = $statementsss->execute(array($name, $ids));
        //     $totalsss = $statementsss->rowCount();    
        //     if($totalsss>0){
        //         $return_arr['status']=400;
        //         $return_arr['error'] = true;
        //         $return_arr['message'] = "This Name is Already Existed!";
        //         print_r(json_encode($return_arr));
        //         return false;
        //         exit(); 
        //     }
        // }
        

        
        // if (isset($mobile) && $mobile!=""){
        //     $statementsss = $pdo->prepare("SELECT * FROM `tbl_delivery_boy` WHERE mobile_no=? and id!=?");
        //     $resultsss = $statementsss->execute(array($mobile, $ids));
        //     $totalsss = $statementsss->rowCount();    
        //     if($totalsss>0){
        //         $return_arr['status']=400;
        //         $return_arr['error'] = true;
        //         $return_arr['message'] = "This Mobile Number is Already Existed!";
        //         print_r(json_encode($return_arr));
        //         return false;
        //         exit(); 
        //     }
        // }
        
        
               
        $statement = $pdo->prepare("UPDATE `tbl_delivery_boy` SET `age` = ?, `email` = ?, `experience` = ?, `salary` = ?, `education` = ?, `mother` = ?, `father` = ?, `name` = ?, `mobile_no` = ?, `address` = ?, `city` = ?, `pincode` = ?, `country` = ?, `state` = ?, `updated_by` = ?, `branch_id` = ?, `delivery_man_proof`=? WHERE id = ?");
        $result = $statement->execute(array($age, $email, $experience, $salary, $education, $mother, $father, $name, $mobile, $address, $city, $pincode, $country, $state, $session_id, $branch_id, $final_name, $ids));
	
    	
    	

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

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

?>