<?php
    include_once 'opendb.php';
    $filter="";
    $CompID=0;
    $issmfilter=1;
      if (isset($_POST["comp_id"]))
    {
        $CompID=$_POST["comp_id"]; 
         $filter.=" and comp_id=" . $CompID;
    }
    if($CompID==0) {
        $response["message"] = "|No Web Co ID|";
         
            // echo no users JSON
            die( json_encode($response));
    }
    if (isset($_POST["last_edit"]))
    {
        $last_edit=$_POST["last_edit"];
        if ($last_edit=='') $last_edit="0" ;        
        $filter.=" and edit_time >= " . $last_edit  ;
        
    }
      $sql = "SELECT set_value FROM appsettings WHERE set_category='GENERAL' and set_name like 'CUST_SM_FILTER' and comp_id=".$CompID;
        $sresult = $conn->query($sql);
        if ($sresult->num_rows > 0 )
        { 
               if($srow = $sresult->fetch_assoc())
                 {
                   if( $srow["set_value"]=="Yes"){
                       $issmfilter=1;
                   }
                   else {
                       $issmfilter=0;
                   }
                 }
        }
        $sql = "SELECT set_value FROM appsettings WHERE set_category='GENERAL' and set_name like 'CUST_ADD_MOB' and comp_id=".$CompID;
        $sresult = $conn->query($sql);
        if ($sresult->num_rows > 0 )
        { 
               if($srow = $sresult->fetch_assoc())
                 {
                   if( $srow["set_value"]=="Yes"){
                       $issmfilter=1;  //need filtering
                   }
                 }
        }        
    
    if (isset($_POST["smancode"]) )
    {
        $sql = "SELECT client_type FROM mobile_clients WHERE  counter_code like '".$_POST["smancode"] ."' and comp_id=".$CompID;
        $sresult = $conn->query($sql);
        if ($sresult->num_rows > 0 )
        { 
             if($srow = $sresult->fetch_assoc())
             {
                   if( $srow["client_type"]==0){   //salesman
                       if ($issmfilter==1){
                           // $filter.=" and SMAN_CODE like'" . $_POST["smancode"]."' ";
                       }
                   }
                   elseif( $srow["client_type"]==1) {  //customer
                        $filter.=" and  sub_code like '" . $_POST["smancode"]."' "; 
                   }
             }
        }
    }
    $Qry="SELECT sub_code, SUBSTRING(sub_name, 1, 40) as sub_name,phone,mobile,email, SUBSTRING(address, 1, 40) as address,place,edit_time,reg_no1,place,area_name,area_code,inactive,sman_code,price_level,latitude,longitude from subsidiary where sub_type like 'C'";
    $Qry.=$filter . " order by edit_time " ;
    //die($Qry);
    $result = $conn->query($Qry); // or die(mysql_error());
    if ($result->num_rows > 0)
    {
        $rows = array();
        while ($r = mysqli_fetch_assoc($result)) {   
             $rows[] = $r;
        }
        echo json_encode($rows);
     } else
     {   
            $response["message"] = "|No Records|" . $filter ;
            echo json_encode($response);
     }
 $conn->close();
?>