<?php
include_once('racss.php');
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}

$flt=racss(1);
$con=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

$q = intval($_GET['q']);

if(isset($_REQUEST['starts'])){
echo "starts ok";	
$starts=$_REQUEST['starts'];}
else{
$starts=date('Y-m-01');}

if(isset($_REQUEST['ends'])){
$ends=$_REQUEST['ends'];}
else{
$ends=date('Y-m-t');}

if(isset($_REQUEST['agents'])){
$agents=$_REQUEST['agents'];}
else{
$agents='all';}

if(isset($_REQUEST['terrys'])){
$terrys=$_REQUEST['terrys'];}
else{
$terrys='all';}

if(isset($_REQUEST['executive'])){
$exce=$_REQUEST['executive'];}
else{
$exce='all';}

if(isset($_REQUEST['rsmname'])){
$rsm=$_REQUEST['rsmname'];}
else{
$rsm='all';}

if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($terrys)!='all'){
$flt=$flt." and territory_names='".$terrys."' ";}
if(strtolower($exce)!='all'){
$flt=$flt."and executive_names='".$exce."'";}
if(strtolower($rsm)!='all'){
$flt=$flt."and rsm_names='".$rsm."'";}


$sql = "SELECT distinct agent_names,territory_names FROM custnew ".$flt." and agent_names LIKE '%".$_GET['q']."%' and cusstatus<>'DEACTIVE' "; 
$result = mysqli_query($con,$sql);

$json = [];
while($row=mysqli_fetch_array($result)){
     $json[] = ['id'=>$row['agent_names'], 'text'=>$row['agent_names'].'--'.$row['territory_names']];
}
echo json_encode($json);
?>
