<?php

require_once __DIR__ . '/db_config.php';

$r = array();
$r["brand"]= array();
$r["model"]= array();
$r["product"]= array();
$r["serial"]= array();
$r["subcat"]= array();
$r["cat"]= array();
$r["type"]= array();
$r["tickets"] = array();
$r["facility"] = array();

if (isset($_POST["mail"]) ) {
 
$cname=$_POST["cname"];
$email=$_POST["mail"];
date_default_timezone_set('Asia/Calcutta');
$date=date('Y-m-d');

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

$assignQuery="SELECT source,ticketnumber,customername,customertype,atmid,questionorcallreason,answer,latitude,longitude,location,ticketstatus,ticketstage,createdby,createddateandtime,Assignedto,assigneddateandtime,contactperson,contactpersonmailid,phonenumber,priority,ticket_type,ticket_subject,extranotes,product,brand,model,serialnumber,service_category,service_sub_category,cservice_type,ticket_method,acceptedby,accepteddateandtime,rejectedby,rejecteddateandtime,reasonforrejection FROM tickets WHERE Assignedto='$email' AND companyname='$cname'";
$facilitys=array();
$combs=array();
$brand=array();
$model=array();
$product=array();
$serial=array();
$cat=array();
$subcat=array();
$type=array();
$result=mysqli_query($con,$assignQuery);

if(!empty($result) && mysqli_num_rows($result) > 0){
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        array_push($r["tickets"], $row);
        array_push($facilitys,$row["atmid"]);
        array_push($brand, $row["brand"]);
        array_push($model, $row["model"]);
        array_push($product, $row["product"]);
        array_push($serial, $row["serialnumber"]);
        array_push($cat, $row["service_category"]);
        array_push($subcat, $row["service_subcategory"]);
        array_push($type,$row["cservice_type"]);
    }
}

if(count($facilitys)!=0){
    $facilitys=array_unique($facilitys);
    $facilityQuery="SELECT Id,customername,atmaddress,atmlandmark,atmpincode,state,country,latitude,longitude,location,atmphonenumber,managername,customermode,facilitytype FROM atmdetails WHERE (Id='".implode("' OR  Id='",$facilitys)."')";
    
    $result=mysqli_query($con,$facilityQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["facility"], $row);
        }
    }
}

if(count($brand)!=0){
    $brand=array_unique($brand);
    $brandQuery="SELECT id,brand,details FROM brand WHERE (id='".implode("' OR  id='",$brand)."')";
    $result=mysqli_query($con,$brandQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["brand"], $row);
        }
    }
}

if(count($model)!=0){
    $model=array_unique($model);
    $modelQuery="SELECT id,brand,product,model,details FROM model WHERE (id='".implode("' OR  id='",$model)."'))";
    $result=mysqli_query($con,$modelQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["model"], $row);
        }
    }
}

if(count($product)!=0){
    $product=array_unique($product);
    $productQuery="SELECT id,product,brand,details FROM product  WHERE (id='".implode("' OR  id='",$product)."')";
    $result=mysqli_query($con,$productQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["product"], $row);
        }
    }
}

if(count($serial)!=0){
    $serial=array_unique($serial);
    $serialQuery="SELECT id,serialnumber,brand,model,product,details FROM serialnumber WHERE  (id='".implode("' OR  id='",$product)."')";
    $result=mysqli_query($con,$serialQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["serial"], $row);
        }
    }
}

if(count($cat)!=0){
    $cat=array_unique($cat);
    $catQuery="SELECT id,category_name,category_description FROM service_category WHERE   (id='".implode("' OR  id='",$cat)."')";
    $result=mysqli_query($con,$catQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["cat"], $row);
        }
    }
}

if(count($subcat)!=0){
    $subcat=array_unique($subcat);
   $subcatQuery="SELECT id,category_name,sub_category_name,description FROM service_sub_category WHERE (id='".implode("' OR  id='",$subcat)."')";
    $result=mysqli_query($con,$subcatQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["subcat"], $row);
        }
    }
}

if(count($type)!=0){
    $type=array_unique($type);
   $typeQuery="SELECT id,category,sub_category,service_type,details FROM service_type WHERE   (id='".implode("' OR  id='",$type)."')";
    $result=mysqli_query($con,$typeQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["type"], $row);
        }
    }
}
}

echo str_replace("no data available","",json_encode($r));
//echo json_encode($r);

?>