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

/*Pincode Details Fetched starts Here*/

if(isset($_POST['type']) && $_POST['type']=='get_pincode_details'){
   $pincode = $_POST['shipper_pincode'];
    
    $condition ='';
    
        $statement = $pdo->prepare("SELECT * FROM `pincode` WHERE 1=1 and pin_code='".$pincode."' $condition ");
		$statement->execute();
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		if($result){
		    
		    
		    $response['data']=$result;
		    $response['error']=false;
		    $response['message']="Pincode Details Fetched Successfully";
		}else{
		    $response['count']=0;
		    $response['error']=true;
		    $response['message']='failed';
		}
		
		echo json_encode($response);
}

/*Pincode Details Fetched starts Here*/

 
/*Get Branch DropDown List*/
if(isset($_POST['type']) && $_POST['type']=='get_city_dropdown'){
    $condi = '1=1';
    $state=$_POST['destination_state'] ?? '';
    $client_id=$_POST['client_id'] ?? '';
    $zone_id=$_POST['zone_id'] ?? '';
    $from_city=$_POST['from_city'] ?? '';
    
    if($state!=''){
        $condi.= " and state='".$state."'";
    }
    $statement = $pdo->prepare("SELECT * FROM `pincode` where $condi GROUP by city ORDER by city ASC");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    if($result){
        $data='';
        foreach($result as $res){
            $f='';
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_generate_zone` WHERE client_id=? and from_city=? and zone_id=? and destination_state=?");
            $resultsss = $statementsss->execute(array($client_id,$from_city,$zone_id,$state));
            $totalsss = $statementsss->rowCount();
            if ($resultsss) {
                $f = '';
                $rows = $statementsss->fetchAll(PDO::FETCH_ASSOC);
                foreach ($rows as $row) {
                    $destination_array = explode(',', $row['destination_cities']);
                    if (in_array($res['city'], $destination_array)) {
                        $f = 'selected';
                        break;
                    }
                }
            }
            $data.='<option value="'.$res['city'].'" '.$f.'>'.$res['city'].'</option>';
        }
    }else{
        $data='';
    }
    echo $data;
}
if(isset($_POST['type']) && $_POST['type']=='get_zone_dropdown'){
    $condi = '';
    $statement = $pdo->prepare("SELECT * FROM `tbl_zone`");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
   $data='';
    if($result){
        foreach($result as $res){
            $data.='<option value="'.$res['id'].'" >'.$res['zone_name'].'</option>';
        }
    }else{
        $data='<option value="">Zone Not Available</option>';
    }
    echo $data;
}


if(isset($_POST['type']) && $_POST['type']=='get_branch_dropdown'){
     $condi = '';
    /* if($_SESSION['user']['type']=='client'){ 
        $condi.= " and id='".$session_branch_ids."'";
     }*/
     if($session['id']!=1 && $session_branch_ids!='0'){
        $condi.= " and id IN ($session_branch_ids)";
    }
     
    $statement = $pdo->prepare("SELECT * FROM `tbl_branch` WHERE status=1 $condi");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
   
   if($session['id']==1 && $session_branch_ids=='0'){
    $data='<option value="all">All</option>';
   }
    /*if($_SESSION['user']['type']=='admin'){ 
        if(isset($_POST['isbooking']) && $_POST['isbooking']==1){
            $data='<option value="">Select Branch</option>'; 
        }else{
            $data='<option value="all">All</option>';
        }
    }*/
    
    if($result){
        foreach($result as $res){
            if(isset($_POST['edit_branch_id']) && $res['id']==$_POST['edit_branch_id']){
                $f ='selected';
            }else{
                $f ='';
            }
            $data.='<option value="'.$res['id'].'" '.$f.'>'.$res['name'].'</option>';
        }
    }else{
        $data='<option value="">Branch Not Available</option>';
    }
    echo $data;
}

if(isset($_POST['type']) && $_POST['type']=='get_pickup_points_dropdown'){
    $statement = $pdo->prepare("SELECT * FROM `tbl_pickup_points` WHERE status=1 ");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    if(isset($_POST['isbooking']) && $_POST['isbooking']==1){
        $data='<option value="">Select Pickup Points</option>'; 
    }else{
        $data='<option value="all">All</option>';
    }
    
    if($result){
        foreach($result as $res){
            $data.='<option value="'.$res['id'].'" 
            data-mobileno="'.$res['mobile_no'].'"
            data-address="'.$res['address'].'"
            data-city="'.$res['city'].'"
            data-pincode="'.$res['pincode'].'"
            data-country="'.$res['country'].'"
            data-state="'.$res['state'].'"
            data-name="'.$res['name'].'"
            data-branchid="'.$res['branch_id'].'">'.$res['name'].'</option>';
        }
    }else{
        $data='<option value="">Pickup Points Not Available</option>';
    }
    echo $data;
}



/*Get Client DropDown List*/
if(isset($_POST['type']) && $_POST['type']=='get_client_dropdown'){
    
    $branch_id=$_POST['branch_id'] ?? 0;
    $conditions='';
    if($branch_id!=0){
        $conditions=" and branch_id='".$branch_id."'";
    }
    
    $new_client = $_POST['new_client'] ?? 0;
    
    $statement = $pdo->prepare("SELECT * FROM `tbl_user` WHERE role='Modicare' and status='Active' $conditions");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    
    if(isset($_POST['isbooking']) && $_POST['isbooking']==1){
        $data='<option value="">Select Consignor</option>'; 
    }else{
        $data='<option value="all" 
        data-air="5500"
        data-surface="4500">All</option>';
    }
    
    
    if($result){
        foreach($result as $res){
            
            if(isset($_POST['edit_client_id']) && $res['id']==$_POST['edit_client_id']){
                $f ='selected';
            }else if($new_client==$res['client_id'] && $res['client_same_consignor']==1){
                $f ='selected';
            }
            else{
                $f ='';
            }
            $data.='<option value="'.$res['id'].'" '.$f.'>'.$res['full_name'].'</option>';
        }
    }else if($branch_id=='all'){
        $data='<option value="all">All</option>';
    }else{
        $data='<option value="">Client Not Available</option>';
    }
    echo $data;
}


/*Get New Client Dropdown Here*/


if(isset($_POST['type']) && $_POST['type']=='get_new_client_dropdown'){
    
    $branch_id = $_POST['branch_id'] ?? 0;
    $data="";
    
    $conditions='';
    
    if($branch_id!=0 && $branch_id!='all'){
        $conditions.=" and branch_id='".$branch_id."'";
    }
    
    if($session_client_ids==0){
        $conditions.="";
    }else{
        $conditions.=" and id IN ($session_client_ids)";
    }
    
    $statement = $pdo->prepare("SELECT * FROM `tbl_client_new` WHERE 1=1 and status=1 $conditions");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    
        $data='<option value="" 
        data-air="5500"
        data-surface="4500">All</option>';
   
    if($result){
        foreach($result as $res){
            
            if(isset($_POST['edit_new_client_id']) && $res['id']==$_POST['edit_new_client_id']){
                $f ='selected';
            }else{
                $f ='';
            }
            $data.='<option value="'.$res['id'].'" '.$f.'>'.$res['name'].'</option>';
        }
    }else if($branch_id=='all'){
        $data='<option value="">All</option>';
    }else{
        $data='<option value="">Client Not Available</option>';
    }
    echo $data;
    // error_log("SELECT * FROM `tbl_client_new` WHERE 1=1 and status=1 $conditions");
}





if(isset($_POST['type']) && $_POST['type']=='get_new_client_dropdown_bulk'){
    
    $branch_id = $_POST['branch_id'] ?? '';
    $data="";
    
    $conditions='';
    
    if(isset($branch_id)){
        $branch_id = implode(',', $branch_id);
        $conditions.=" and branch_id IN ($branch_id)";
    }
    
    if($session_client_ids==0){
        $conditions.="";
    }else{
        $conditions.=" and id IN ($session_client_ids)";
    }
    
    $statement = $pdo->prepare("SELECT * FROM `tbl_client_new` WHERE 1=1 and status=1 $conditions");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    
        $data='<option value="" 
        data-air="5500"
        data-surface="4500">All</option>';
   
    if($result){
        foreach($result as $res){
            
            if(isset($_POST['edit_new_client_id']) && $res['id']==$_POST['edit_new_client_id']){
                $f ='selected';
            }else{
                $f ='';
            }
            $data.='<option value="'.$res['id'].'" '.$f.'>'.$res['name'].'</option>';
        }
    }else if($branch_id=='all'){
        $data='<option value="">All</option>';
    }else{
        $data='<option value="">Client Not Available</option>';
    }
    echo $data;
}





/*Get Courier DropDown List*/
if(isset($_POST['type']) && $_POST['type']=='get_courier_dropdown'){
    $statement = $pdo->prepare("SELECT * FROM `courier_company` WHERE 1=1 ");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    if(isset($_POST['isbooking']) && $_POST['isbooking']==1){
        $data='<option value="">Select Courier</option>'; 
    }else{
        $data='<option value="all">All</option>';
    }
    if($result){
        foreach($result as $res){
            $data.='<option value="'.$res['id'].'" >'.$res['company_name'].'</option>';
        }
    }
    else{$data.='<option value="">Courier Not Available</option>';}
    echo $data;
}

if(isset($_POST['type']) && $_POST['type']=='get_client_dropdown_rate_chart'){
    
    $branch_id=$_POST['branch_id'] ?? 0;
    $conditions='';
    if($branch_id!=0){
        $conditions=" and branch_id='".$branch_id."'";
    }
    
    $statement = $pdo->prepare("SELECT * FROM `tbl_client_new` WHERE 1=1 $conditions");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    if(isset($_POST['isbooking']) && $_POST['isbooking']==1){
        $data='<option value="">Select Consignor</option>'; 
    }else{
        $data='<option value="0" 
        data-air="5500"
        data-surface="4500">Common Rate Chart</option>';
    }
    if($result){
        foreach($result as $res){
            $data.='<option value="'.$res['id'].'" >'.$res['name'].'</option>';
        }
    }else if($branch_id=='all'){
        $data='<option value="0">Common Rate Chart</option>';
    }else{
        $data='<option value="0">Client Not Available</option>';
    }
    echo $data;
}

/*Get customer DropDown List*/
if(isset($_POST['type']) && $_POST['type']=='get_customer_dropdown'){
    
    
  //  $client_id=$_POST['client_id'];
    $client_id=$_POST['client_id'] ?? 0;
    $conditions='';
   // if($client_id!=0){
        //$conditions.=" and client_id='".$client_id."'";
    //}
    
    $statement = $pdo->prepare("SELECT * FROM `tbl_customer` WHERE 1=1 $conditions");
   // error_log("SELECT * FROM `tbl_customer` WHERE 1=1 $conditions");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    $data ='';
    /*if(isset($_POST['isbooking']) && $_POST['isbooking']==1){
        $data='<option value="">Select Customer</option>'; 
    }else{
        $data='<option value="all">All</option>';
    }*/
     $data='<option value="">Select Consignee</option>';
    if($result){
        foreach($result as $res){
            $data.='<option value="'.$res['id'].'" >'.$res['cust_name'].'</option>';
        }
    }else if($client_id=='all'){
        $data='<option value="all">All</option>';
    }
    else{$data='<option value="">Customer Not Available</option>';}
    echo $data;
}


?>