<?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(); 
        
        $reason = $_POST['reason'];
        $statement = $pdo->prepare("INSERT INTO `tbl_reason`(`reason`,`created_by`, `updated_by`) VALUES (?,?,?)");
    	$result = $statement->execute(array($reason,$session_id, $session_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){
         error_log($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'];
        $reason = $_POST['reason'];
        
    $statement = $pdo->prepare("UPDATE `tbl_reason` SET `reason`=?,`updated_by`=? WHERE id=?");
    $result = $statement->execute(array($reason,$session_id,$ids));
    
        if($result){
            
            $pdo->commit();
            $return_arr['message']='Reason 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);
}

?>