<?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_location'){
     try{
         
        $pdo->beginTransaction(); 
        
            $name = $_POST['name'];
            $mobile = $_POST['mobile'];
            $gst_no = $_POST['gst_no'];
            $address = $_POST['address'];
            $status = $_POST['status'];
            $company_id = $_POST['company_id'];
            $state = $_POST['state'];
            $email = $_POST['email'];
            $prefix_for_lr = $_POST['prefix_for_lr'];
            $prefix_for_manifest = $_POST['prefix_for_manifest'];
            $prefix_for_drs = $_POST['prefix_for_drs'];
            $starting_series = $_POST['starting_series'];
            $current_lr_no = $_POST['current_lr_no'];
            
            
            
            
            
        if (isset($name) && $name!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_branch` 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_branch` 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_branch`(`name`, `mobile_no`, `address`, `gst_no`, `status`, 
                                `state`, `email`, `company_id`,
                                `prefix_for_lr`, `prefix_for_manifest`, `prefix_for_drs`, `starting_series`, `current_lr_no`) VALUES (?,?,?,?,?,
                                ?,?,?,
                                ?,?,?,?,?)");
    	$result = $statement->execute(array($name, $mobile, $address, $gst_no, $status, 
    	                    $state, $email,$company_id,
    	                    $prefix_for_lr, $prefix_for_manifest, $prefix_for_drs, $starting_series, $current_lr_no));

        if($result){
            $last_inserted_id = $pdo->lastInsertId();
            $pdo->commit();
            $return_arr['message']='Branch 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'];
            $gst_no = $_POST['gst_no'];
            $address = $_POST['address'];
            $status = $_POST['status'];
            $company_id = $_POST['company_id'];
            $state = $_POST['state'];
            $email = $_POST['email'];
            $prefix_for_lr = $_POST['prefix_for_lr'];
            $prefix_for_manifest = $_POST['prefix_for_manifest'];
            $prefix_for_drs = $_POST['prefix_for_drs'];
            $starting_series = $_POST['starting_series'];
            $current_lr_no = $_POST['current_lr_no'];
            
            
            
            
        if (isset($name) && $name!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_branch` 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_branch` 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_branch` SET  `name`=?, `mobile_no`=?, `address`=?, `gst_no`=?, `status`=?, 
                            `state`=?, `email`=?, `company_id`=?, 
                            `prefix_for_lr`=?, `prefix_for_manifest`=?, `prefix_for_drs`=?, `starting_series`=?, `current_lr_no`=? WHERE id=?");
       	$result = $statement->execute(array($name, $mobile, $address, $gst_no, $status, 
       	                $state, $email, $company_id,
       	                $prefix_for_lr, $prefix_for_manifest, $prefix_for_drs, $starting_series, $current_lr_no, $ids));

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

?>