<?php 
ob_start();
session_start();
include "../inc/config.php";
$today_date = date('Y-m-d');

try{
        $pdo->beginTransaction(); 
        
        
        $client_id= $_POST['client_id'];
        $rate_type= $_POST['rate_type'];
        $mode= $_POST['mode'];
        $pod_type= $_POST['pod_type'];
        $from_rate_type= $_POST['from_rate_type'];
        $from_city= $_POST['from_city'];
        $from_zone= $_POST['from_zone'];
        $to_rate_type= $_POST['to_rate_type'];
        $to_city= $_POST['to_city'];
        $to_zone= $_POST['to_zone'];
        
        $min_rate= $_POST['min_rate'];
        
        if(isset($_POST['origin_city'])){
            for($t=0;$t<count($_POST['origin_city']);$t++){
                $origin_city= $_POST['origin_city'][$t];
                $destination_city= $_POST['destination_city'][$t];
                $min_weight= $_POST['min_weight'][$t];
                $max_weight= $_POST['max_weight'][$t];
                $per_kg_rate= $_POST['per_kg_rate'][$t];
                $ids= $_POST['ids'][$t];
                if($ids==0){ //insert Query
                    $statement = $pdo->prepare("INSERT INTO `tbl_client_rate_master`
                    (`client_id`, `rate_type`, `mode`, `pod_type`, `from_rate_type`,
                    `from_city`, `from_zone`, `to_rate_type`, `to_city`, `to_zone`, 
                    `min_rate`, `origin_city`, `destination_city`, `min_wt`, `max_wt`, `per_kg_rate`) 
                    VALUES (?,?,?,?,?,
                    ?,?,?,?,?,
                    ?,?,?,?,?,?)");
    	            $result = $statement->execute(array($client_id,$rate_type,$mode,$pod_type,$from_rate_type,
    	            $from_city,$from_zone,$to_rate_type,$to_city,$to_zone,
    	            $min_rate,$origin_city,$destination_city,$min_weight,$max_weight,$per_kg_rate));
                }else{//update Query
                    $statement = $pdo->prepare("UPDATE `tbl_client_rate_master` SET  `client_id`=?, `rate_type`=?, `mode`=?, `pod_type`=?, `from_rate_type`=?,
                    `from_city`=?, `from_zone`=?, `to_rate_type`=?, `to_city`=?, `to_zone`=?, 
                    `min_rate`=?, `origin_city`=?, `destination_city`=?, `min_wt`=?, `max_wt`=?, `per_kg_rate`=? where id=?");
       	            $result = $statement->execute(array($client_id,$rate_type,$mode,$pod_type,$from_rate_type,
    	            $from_city,$from_zone,$to_rate_type,$to_city,$to_zone,
    	            $min_rate,$origin_city,$destination_city,$min_weight,$max_weight,$per_kg_rate,$ids));
                }
                
            }
        }
        if($result){
            $pdo->commit();
            $return_arr['message']='Zone Generation Successfully...';
            $return_arr['status']=200;
        }
        else
        {
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
        echo json_encode($return_arr);
}catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        echo json_encode($return_arr);
}   
    
?>