<?php

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

if (isset($_POST["report_id"]) ) {
 
$cname=$_POST["cname"];
$report_id=$_POST["report_id"];
$user_id=$_POST["user_id"];
date_default_timezone_set("Asia/Calcutta");   //India time (GMT+5:30)
$datetime=date('Y-m-d H:i:s');
$status = "completed";
$facility="";
if (isset($_POST["facility"]) ) {
    $facility = $_POST["facility"];
}

if (isset($_POST["amount"]) ) {
    $amount = $_POST["amount"];
}

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

$query="UPDATE defect_report 
    SET completed_status_DT='$datetime',status='$status',completed_user_id='$user_id' 
    WHERE report_id='$report_id'  AND companyname='$cname'";
if(!empty($amount)){
    $query="UPDATE defect_report 
    SET completed_status_DT='$datetime',status='$status',completed_user_id='$user_id',workreport_amount='$amount'
    WHERE report_id='$report_id'  AND companyname='$cname'";
    
    //echo $query;
}

    $result=mysqli_query($con,$query);
    if($result){
        $r["s"] = 1;
        $noity=new notify();
        
        $query = "SELECT mep_facility.mep_id FROM mep_facility 
            INNER JOIN defect_report dc ON dc.facility_id=mep_facility.facility_id AND dc.companyname='$cname' AND dc.report_id='$report_id'
            WHERE mep_facility.delete_status=0";
        $result=mysqli_query($con,$query);
        if(!empty($result) && mysqli_num_rows($result) > 0){
            $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
            $message="Defect Bill Changed";
            $noity->send($con,$row["mep_id"],$report_id,$message);
        }
    }else{
        $r["s"] = 0;
    }

mysqli_close($con);

}

echo json_encode($r,JSON_UNESCAPED_SLASHES);


?>