<?php

$r = array();
require_once __DIR__ . '/db_config.php';


if (isset($_POST["email"])) {
    
$email = $_POST['email'];
$canme = $_POST['cname'];
$role = $_POST['role'];
$facility="";
if(isset($_POST["status"])){ $status = $_POST["status"]; }
if(isset($_POST["id"])){ $id = $_POST["id"]; }
if(isset($_POST["page"])){ $page = $_POST["page"]; }
if(isset($_POST["start"])){ $start = $_POST["start"]; }
if(isset($_POST["end"])){ $end = $_POST["end"]; }

//$canme = "Zkitchen";
//$email = "ankush.chauhan@zomato.com";

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


$query="SELECT d.id,d.report_id,d.datetime,d.companyname,d.facility_id,d.estimated_amount,d.approved_amount,d.emp_emailid,d.status,
    d.defect,d.delete_DT,d.delete_status,d.delete_reason,d.delete_user_id,d.completed_status_DT,d.completed_user_id,d.open_status_DT,
    d.open_user_id,d.approved_status_DT,d.approved_user_id,d.rejected_status_DT,d.rejected_user_id,d.closed_user_id,d.closed_status_DT,
    d.defective_equipment,sf.subfacility_name as kitchen_number,eq.equipment,ou.employeename as 'openedBy',au.employeename as 'approvedBy',
    ru.employeename as 'rejectedBy', cl_u.employeename as 'closedBy',cu.employeename as 'completedBy',du.employeename 'deletedBy',
    emp.employeename as 'createdBy',f.sitename,f.state,f.location,f.atmaddress,
    d.billpay_type,d.bill_approval,d.bill_approval_user_id,d.bill_approval_DT,d.resubmission,d.resubmission_DT,d.reject_reason,
    (CASE WHEN mep.mep_id IS NULL THEN false else true END) AS can_edit,d.workreport_acitivity,d.workreport_amount,d.approved_reason
FROM defect_report d
INNER JOIN equipment eq ON eq.equipment_id=d.defective_equipment
INNER JOIN atmdetails f ON f.atmid=d.facility_id
LEFT JOIN signupdetails emp ON emp.emailid=d.emp_emailid
LEFT JOIN signupdetails ou ON ou.emailid=d.open_user_id
LEFT JOIN signupdetails au ON au.emailid=d.approved_user_id
LEFT JOIN signupdetails ru ON ru.emailid=d.rejected_user_id
LEFT JOIN signupdetails cl_u ON cl_u.emailid=d.closed_user_id
LEFT JOIN signupdetails cu ON cu.emailid=d.completed_user_id
LEFT JOIN signupdetails du ON du.emailid=d.delete_user_id
LEFT JOIN mep_facility mep ON mep.facility_id=d.facility_id
left JOIN subfacility sf ON d.kitchen_number=sf.subfacility_id
WHERE d.companyname='$canme' AND ( emp_emailid='$email' OR ( mep.mep_id='$email' AND mep.delete_status=0 )) AND d.delete_status = '0'";

if(!empty($id)){ $query = $query." AND d.report_id='$id'"; }
else if($status=="open"){ $query = $query." AND d.status='open'"; }
else if($status=="approved"){ $query = $query." AND d.status='approved'"; }
else if($status=="rejected"){ $query = $query." AND d.status='rejected'"; }
else if($status=="completed"){ $query = $query." AND d.status='completed'"; }
else if($status=="closed"){ $query = $query." AND d.status='closed'"; }

if(!empty($start)){
    $query = $query ." AND d.datetime BETWEEN '$start' AND '$end'";
}


$query = $query ." GROUP BY d.id ORDER BY d.id DESC";
$startLimit = $page * 10;
$query = $query ." LIMIT $startLimit,10";

$result = mysqli_query($con,$query);
if(!empty($result) && mysqli_num_rows($result) > 0){
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        array_push($r, $row);
    }
}
mysqli_close($con);               
}
echo json_encode($r);

?>