<?php

require_once('Conn.php');
header('Content-Type: application/json');

if($_SERVER['REQUEST_METHOD'] != 'POST') {
    header('HTTPS/1.1 405');
    echo json_encode(array(["message" => "method not allowed" , "status" => "false"]));
    exit;
}

$params = json_decode(file_get_contents('php://input') , true);

if($params == null || !isset($params['ucid'])){
    echo json_encode(array(["message" => "internal body error" , "status" => "false"]));
    exit;
}


$ucid = $params['ucid'];


// date_default_timezone_set("Asia/Kolkata"); 
// $today = date("d/m/Y");
// $yesterday = date('d/m/Y', strtotime("-1 days"));


$query = "SELECT c.ucid , c.engineer , c.remarks , c.completiondate , c.user_ratings , c.user_feedback , c.date , c.details , c.c_status as status , bl.bNo , bl.bName  , c.time , cl.details as complaint , cl.priority , c.docketNumber  FROM users_complaint c INNER JOIN building_list bl ON c.b_list = bl.bid INNER JOIN complaint_list cl ON c.c_list = cl.cid WHERE c.ucid = $ucid ";

$result = mysqli_query($connection , $query);
if($result) {
    $response = array();
    while($row = mysqli_fetch_assoc($result)) {
        $respons['ucid'] = $row['ucid'];
        $response['date'] = $row['date'];
        $response['details'] = $row['details'];
        $response['address'] = $row['bNo'] . ' , ' . $row['bName'] ;
        $response['status'] = $row['status'];
        $response['time'] = $row['time'];
        $response['complaint'] = $row['complaint'];
        $response['docketNumber'] = $row['docketNumber'];
        $response['user_ratings'] = $row['user_ratings'];
        $response['user_feedback'] = $row['user_feedback'];
        $response['completiondate'] = $row['completiondate'];
        $engArray = fetchEngineer($connection , $row['engineer']);
        $response['engName'] = $engArray['name'];
        $response['engMobile'] = $engArray['mobile'];
        $response['adminRemarks'] = $row['remarks'];
        $response['images'] = fetchImages($connection , $ucid);

    }

    echo json_encode($response);
}
else {
    echo json_encode(array(["message" => mysqli_error($connection) , "status" => "false"])); 
}


function fetchEngineer($connection , $id) {
    $q1 = "SELECT name , mobile FROM engineers_list WHERE id = $id ";
    $r1 = mysqli_query($connection , $q1);
    if($r1){
        $row = mysqli_fetch_assoc($r1);
        $res = array();
        $res['name'] = $row['name'];
        $res['mobile'] = $row['mobile'];

        return $res;
    }
    else{
        return array(["message" => mysqli_error($connection) , "status" => "false"]);
    }
}

function fetchImages($connection , $ucid){
    $q2 = "SELECT id , img FROM users_complaint_images WHERE ucid = $ucid";
    $r2 = mysqli_query($connection , $q2);
    if($r2){
        $res = array();
        $i = 0;
        while($row = mysqli_fetch_assoc($r2)){
            $res[$i]['id'] = $row['id'];
            $res[$i]['img'] = $row['img'];
            $i++;
        }
        return $res;
    }
    else{
        return array(["message" => mysqli_error($connection) , "status" => "false"]);
    }
} 

?>