<?php

require_once __DIR__ . '/db_config.php';

$r = array();
$r["brand"]= array();
$r["model"]= array();
$r["product"]= array();
$r["serial"]= array();
$r["qcat"]= array();
$r["qsubcat"]= array();
$r["ques"]= array();
$r["assign"] = array();
$r["combs"] = array();
$r["facility"] = array();
$r["subcat"]= array();
$r["cat"]= array();
$r["type"]= array();


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

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

//$assignQuery="SELECT scheduler_id,assignschedule_id,customertype,customername,facilityid,combinationtype,combinationid,eventtype,assigndate,empemailid,status,c_datetime,u_datetime,deleted FROM scheduleassigndate WHERE assigndate<='$date' AND status!='completed' AND empemailid='$email' ";

$assignQuery="SELECT scheduler_id,assignschedule_id,customertype,customername,facilityid,combinationtype,combinationid,eventtype,
assigndate,empemailid,status,c_datetime,u_datetime,deleted,brandid,productid,modelid,serialid 
FROM scheduleassigndate 
WHERE status!='completed' AND empemailid='$email' ";

$facilitys=array();
$combs=array();
$brand=array();
$model=array();
$product=array();
$serial=array();
$cat=array();
$subcat=array();
$type=array();
 mysqli_query($con,'SET CHARACTER SET utf8');

$result=mysqli_query($con,$assignQuery);

if(!empty($result) && mysqli_num_rows($result) > 0){
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        array_push($r["assign"], $row);
        array_push($facilitys,$row["facilityid"]);
        array_push($combs,$row["combinationid"]);
        if($row["brandid"]!="no data available"){
            array_push($brand, $row["brandid"]);
        }
        if($row["modelid"]!="no data available"){
            array_push($model, $row["modelid"]);

        }

        if($row["productid"]!="no data available"){
            array_push($product, $row["productid"]);
        }
        if($row["serialid"]!="no data available"){
            array_push($serial, $row["serialid"]);
        }
    }
}

if(count($facilitys)!=0){
    $facilitys=array_unique($facilitys);
    
    $facilityQuery="SELECT Id,atmid,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($combs)!=0){
    $combs=array_unique($combs);
    $comQuery="SELECT ID,type,typename,custonermode,atmid,brand,product,model,serialnumber,deleted,dateandtime,customername,
    service_category,service_subcategory,service_servicetype,u_datetime 
    FROM combination 
    WHERE (ID='".implode("' OR  ID='",$combs)."')";

    $result=mysqli_query($con,$comQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["combs"], $row);
            array_push($cat, $row["service_category"]);
            array_push($subcat, $row["service_subcategory"]);
            array_push($type,$row["service_servicetype"]);

        }
    }
}

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='",$serial)."')";
    $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);
        }
    }
}

$qcat=array();
$qsubcat=array();

if(count($combs)!=0){
    $combs=array_unique($combs);

 $questionQuery="SELECT Id,questionnumber,category,subcategory,question,typeofquestion,count,optionforanswers,correctanswer,
 dateandtime,deleted,questionmode,customertype,combinationname,update_datetime,question_required
 FROM dynamicquestions 
 WHERE deleted='no' AND (combinationname='".implode("' OR  combinationname='",$combs)."')";
 $result=mysqli_query($con,$questionQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["ques"], $row);
            array_push($qcat, $row["category"]);
            array_push($qsubcat, $row["subcategory"]);
        }
    }
}

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

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



}

echo json_encode($r);


?>