<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";



if(isset($_POST['type']) && $_POST['type']=='create_business_locations'){
     try{
         
         $pdo->beginTransaction(); 
         
        $business_name = $_POST['business_name'];
        $mobile_no = $_POST['mobile_no'];
        $alter_mobile_no = $_POST['alter_mobile_no'];
        $business_email = $_POST['business_email'];
        $business_gst = $_POST['business_gst'];
        $business_address = $_POST['business_address'];
        $business_logo = $_POST['business_logo'] ?? "";
        $status = $_POST['status'];
        $location_name = $_POST['location_name'];
       
        
     
         
        $statement = $pdo->prepare("INSERT INTO `tbl_business_branch`(`business_name`, `mobile_no`, `alter_mobile_no`, `business_email`, `location_name`, 
                                    `business_address`, `business_gst`, `business_logo`, `status`, 
                                    `created_by`, `updated_by`, `created_at`, `updated_at`) VALUES (?,?,?,?,?,
                                    ?,?,?,?,
                                    ?,?,?,?)");
        $result = $statement->execute(array($business_name, $mobile_no, $alter_mobile_no, $business_email, $location_name,
                                    $business_address, $business_gst, $business_logo, $status,
                                    $session_id, 0, $updated_at, $updated_at));
         
         
    
        if($result){    
            $pdo->commit();
            $return_arr['message'] = 'Business Locations 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_business_locations'){
     try{
         
        $pdo->beginTransaction(); 
        
        $ids = $_POST['ids']; 
        $business_name = $_POST['business_name'];
        $mobile_no = $_POST['mobile_no'];
        $alter_mobile_no = $_POST['alter_mobile_no'];
        $business_email = $_POST['business_email'];
        $business_gst = $_POST['business_gst'];
        $business_address = $_POST['business_address'];
        $business_logo = $_POST['business_logo'] ?? "";
        $status = $_POST['status'];
        $location_name = $_POST['location_name'];
       
        
     
         
        $statement = $pdo->prepare("UPDATE `tbl_business_branch` SET 
                                    `business_name`=?, `mobile_no`=?, `alter_mobile_no`=?, `business_email`=?, `location_name`=?, 
                                    `business_address`=?, `business_gst`=?, `business_logo`=?, `status`=?, 
                                    `updated_by`=?, `updated_at`=? 
                                    WHERE id=?");
        $result = $statement->execute(array($business_name, $mobile_no, $alter_mobile_no, $business_email, $location_name,
                                    $business_address, $business_gst, $business_logo, $status,
                                    $session_id, $updated_at, $ids));
         
         
    
        if($result){    
            $pdo->commit();
            $return_arr['message'] = 'Business Locations 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);
}

?>