<?php

require_once __DIR__ . '/db_config.php';

$r = array();
$r["ticket"] = array();

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

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

$query="SELECT w.ticketnumber,w.latitude,w.longitude,w.workdonelocation,w.workstatus,w.workreportdetails,w.working_hours,w.servicecharge,w.sparesamount,w.amountreceived,w.workdoneby,w.imagepath,w.signaturepath,w.billimagepath,w.nameofsignature,w.dateandtime,f.companyname,f.customername,f.atmaddress,t.assigneddateandtime,t.createddateandtime,f.location,f.atmphonenumber,t.customertype,t.questionorcallreason FROM workreport w INNER JOIN tickets t ON w.ticketnumber=t.ticketnumber AND w.companyname=t.companyname INNER JOIN atmdetails f ON f.Id=t.atmid WHERE w.ticketnumber='$ticket' and w.companyname='$cname'";

//echo $query;

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

    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["ticket"], $row);
        }
    }
    
    $sparesQuery="SELECT item_name as name,amount,quantity as unit,totalamount as total FROM spareparts WHERE companyname='$cname' and ticketnumber='$ticket'";
    
    $result=mysqli_query($con,$sparesQuery);

    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($r["spares"], $row);
        }
    }
    
}

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

?>