<?php

require_once __DIR__ . '/db_config.php';

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

if (isset($_POST["mail"]) ) {
 
$cname=$_POST["cname"];
$email=$_POST["mail"];
$ticket=$_POST["ticket"];

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,tickets.customername,customertype,tickets.atmid,questionorcallreason,answer,latitude,longitude,location,ticketstatus,ticketstage,createdby,createddateandtime,Assignedto,assigneddateandtime,contactperson,contactpersonmailid,phonenumber,priority,ticket_type,ticket_subject,extranotes,pd.product,pd.brand,pd.modelnumber as model,tickets.serialnumber,service_category,service_sub_category,cservice_type,ticket_method,acceptedby,accepteddateandtime,rejectedby,rejecteddateandtime,reasonforrejection FROM tickets LEFT JOIN productdetais pd ON tickets.serialnumber=pd.serialnumber WHERE Assignedto='$email' AND ticketnumber='$ticket' ";

$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["ticket"], $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_sub_category"]);
        array_push($type,$row["cservice_type"]);
        $r["service_category"]=$row["service_category"];
        $r["ticketstatus"]=$row["ticketstatus"];

    }
}

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)) {
            $r["atmphonenumber"]=$row["atmphonenumber"];
            $r["atmaddress"]=$row["atmaddress"];
            $r["latitude"]=$row["latitude"];
            $r["longitude"]=$row["longitude"];
            $r["location"]=$row["location"];

        }
    }
}

if(count($brand)!=0){
    $brand=array_unique($brand);
    $brandQuery="SELECT brand 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)) {
           $r["brand"]=$row["brand"];
        }
    }
}

if(count($model)!=0){
    $model=array_unique($model);
    $modelQuery="SELECT model 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)) {
            $r["model"]=$row["model"];
        }
    }
}

if(count($product)!=0){
    $product=array_unique($product);
    $productQuery="SELECT product 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)) {
            $r["product"]=$row["product"];

        }
    }
}

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

if(count($cat)!=0){
    $cat=array_unique($cat);
    $catQuery="SELECT category_name 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)) {
                $r["cat"]=$row["category_name"];
        }
    }
}

if(count($subcat)!=0){
    $subcat=array_unique($subcat);
   $subcatQuery="SELECT sub_category_name 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)) {
                $r["subcat"]=$row["sub_category_name"];
        }
    }
}

if(count($type)!=0){
    $type=array_unique($type);
   $typeQuery="SELECT service_type 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)) {
            $r["type"]=$row["service_type"];
        }
    }
}
}

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

?>