<?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['uid'])){
    echo json_encode(array(["message" => "internal body error" , "status" => "false"]));
    exit;
}


$uid = $params['uid'];


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


$query = "SELECT c.ucid , 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.uid = $uid ORDER BY c.ucid desc ";

$result = mysqli_query($connection , $query);
if($result) {
    $response = array();
    $i = 0;
    while($row = mysqli_fetch_assoc($result)) {
        $response[$i]['ucid'] = $row['ucid'];
        $response[$i]['date'] = $row['date'];
        $response[$i]['details'] = $row['details'];
        $response[$i]['address'] = $row['bNo'] . ' , ' . $row['bName'] ;
        $response[$i]['status'] = $row['status'];
        $response[$i]['time'] = $row['time'];
        $response[$i]['complaint'] = $row['complaint'];
        $response[$i]['docketNumber'] = $row['docketNumber'];
        
        

        $i++;        

    }

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

?>