<?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;
}



if(isset($_POST['type']) && $_POST['type']=='create_location'){
     try{
         
        $pdo->beginTransaction(); 
        
            $name = $_POST['name'];
            $mobile = $_POST['mobile'];
            $address = $_POST['address'];
            $status = $_POST['status'];
            
            $state = $_POST['state'];
            $pincode = $_POST['pincode'];
            $state = $_POST['state'];
            $country = $_POST['country'];
            $city = $_POST['city'];
            $branch_id = $_POST['branch_id'];
            
            
            
            
        if (isset($name) && $name!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_pickup_points` 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_pickup_points` 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_pickup_points`(`name`, `mobile_no`, `address`, `city`, `pincode`, 
        `country`, `state`, `created_by`, `updated_by`, `branch_id`) VALUES (?,?,?,?,?,
        ?,?,?,?,?)");
    	$result = $statement->execute(array($name, $mobile, $address, $city, $pincode,
    	$country, $state, $session_id, $session_id, $branch_id));

        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(); 
        
        
        error_log("UPdate");
        
            $ids = $_POST['id'];
            $name = $_POST['name'];
            $mobile = $_POST['mobile'];
            $address = $_POST['address'];
            $status = $_POST['status'];
            
            $state = $_POST['state'];
            $pincode = $_POST['pincode'];
            $state = $_POST['state'];
            $country = $_POST['country'];
            $city = $_POST['city'];
            $branch_id = $_POST['branch_id'];
            
            error_log($ids);
            
            
        if (isset($name) && $name!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_pickup_points` 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_pickup_points` 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(); 
            }
        }
        
        
    error_log("UPDATE `tbl_pickup_points` SET `name`=$name, `mobile_no`=$mobile, `address`=$address, `city`=$city, `pincode`=$pincode, 
        `country`=$country, `state`=$state,  `updated_by`=$session_id, `branch_id`=$branch_id WHERE id=$ids");
       
       
    $statement = $pdo->prepare("UPDATE `tbl_pickup_points` SET `name`=?, `mobile_no`=?, `address`=?, `city`=?, `pincode`=?, 
        `country`=?, `state`=?,  `updated_by`=?, `branch_id`=? WHERE id=?");
        
    $result = $statement->execute(array($name, $mobile, $address, $city, $pincode,
    	$country, $state,  $session_id, $branch_id, $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){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}

?>