<?php 
ob_start();
session_start();
include "../inc/config.php";
$today_date = date('Y-m-d');
//$session_id = $_SESSION['users']['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;
}



if(isset($_POST['type']) && $_POST['type']=='create_zone'){
     try{
         
        $pdo->beginTransaction(); 
        
            $name = $_POST['name'];
            $description = $_POST['description'];
            
        if (isset($name) && $name!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_zone` WHERE zone_name=?");
            $resultsss = $statementsss->execute(array($name));
            $totalsss = $statementsss->rowCount();    
            if($totalsss>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Zone Name is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
        
        $statement = $pdo->prepare("INSERT INTO `tbl_zone`(`zone_name`,description) VALUES (?,?)");
    	$result = $statement->execute(array($name,$description));

        if($result){
            $last_inserted_id = $pdo->lastInsertId();
            $pdo->commit();
            $return_arr['message']='Zone 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_zone'){
     try{
         
        $pdo->beginTransaction(); 
        
        
            $ids = $_POST['id'];
            $name = $_POST['name'];
            $description =$_POST['description'];
            
        if (isset($name) && $name!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_zone` WHERE zone_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 Zone Name is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
       
        $statement = $pdo->prepare("UPDATE `tbl_zone` SET  `zone_name`=?,description=? WHERE id=?");
       	$result = $statement->execute(array($name,$description,$ids));

        if($result){
            $pdo->commit();
            $return_arr['message']='Zone 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);
}

?>