<?php
include('usersession.php');
include_once('complaint_hierarchy.php');

function safe($value) {
    return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
}

function ratingText($rating) {
    if($rating == '5') return 'Excellent';
    if($rating == '4') return 'Very Good';
    if($rating == '3') return 'Good';
    if($rating == '2') return 'Satisfactory';
    if($rating == '1') return 'Poor';
    if($rating == '0') return 'Very Poor';
    return 'N/A';
}

$uid = $_SESSION['ecimsuser'];

// $list = $conn->query("
//     SELECT uc.*, cl.details AS complaint_name
//     FROM users_complaint uc
//     LEFT JOIN complaint_list cl ON cl.cid = uc.c_list
//     WHERE uc.uid='$uid'
//     AND uc.c_status='2'
//     ORDER BY uc.ucid DESC
// ");
$list = $conn->query("
    SELECT 
        uc.*,
        cl.details AS complaint_name,
        cl.node_id,
        cc.name AS category_name,
        cs.name AS subcategory_name
    FROM users_complaint uc
    LEFT JOIN complaint_list cl ON cl.cid = uc.c_list
    LEFT JOIN complaint_category cc ON cc.cc_id = cl.cc_id
    LEFT JOIN complaint_subcategory cs ON cs.cs_id = cl.cs_id
    WHERE uc.uid='$uid' AND uc.c_status='2'
    ORDER BY uc.ucid DESC
");
?>
<!DOCTYPE html>
<html lang="en">
  <head>
	<title>ECIMS USER | NEW COMPLAINT</title>
    <?php include('includes/head.php'); ?>
    <style>
        .alert-success {
            color: #000;
        }
    </style>
  </head>
  <body>
    <div class="container-scroller">
      <div class="horizontal-menu">
        <?php $activePage = basename($_SERVER['PHP_SELF'], ".php"); ?>
<nav class="navbar top-navbar col-lg-12 col-12 p-0">
  <div class="container-fluid">
	<div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-start">
	  <a class="navbar-brand brand-logo" href="index.php"><img style="width: 115px;height:auto;" src="assets/images/logo.png" alt="logo" /></a>
	  <a class="navbar-brand brand-logo-mini" href="index.php"><img style="width: 115px;height:auto;" src="assets/images/logo.png" alt="logo" /></a>
	</div>
	<div class="text-center navbar-menu-wrapper d-none d-lg-block d-flex align-items-center justify-content-center">
	  <span style="text-transform:uppercase;font-size: 30px;font-weight: bold;color: #000;">military engineer services <br> HISAR/ALWAR</span>
	</div>
	<div class="navbar-brand-wrapper d-flex align-items-center justify-content-end">
	  <a class="navbar-brand brand-logo d-none d-lg-block" href="index.php"><img style="width: 115px;height:auto;" src="assets/images/logo-right.png" alt="logo" /></a>
	  <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="horizontal-menu-toggle">
		<i class="fa fa-bars text-white"></i>
	  </button>
	</div>
  </div>
</nav>
<p class="text-center p-0 m-1 fw-bolder"> Login Date : <?php echo date('d/m/Y'); ?> </p>
<nav class="bottom-navbar">
  <div class="container-fluid">
	<?php include('includes/nav.php');  ?>
  </div>
</nav>
      </div>

<div class="container-fluid page-body-wrapper">
<div class="main-panel">
<div class="content-wrapper">

<h3 class="mb-4">Previous Completed Complaints</h3>
<a href="user-dashboard.php" class="btn btn-secondary btn-sm mb-3">Back</a>

<div class="card">
<div class="card-body">
<div class="table-responsive">

<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Sr No.</th>
<th>Complaint No.</th>
<th>Date</th>
<th>Details of Complaint</th>

<th>Completion On</th>
<th> Status</th>
<th>Rating</th>
<th>user Feedback</th>
</tr>
</thead>
<tbody>
<?php
if($list && $list->num_rows > 0) {
$i=1;
while($row = $list->fetch_assoc()) {
?>
<tr>
<td><?= $i++; ?></td>
<td><?= safe($row['docketNumber']); ?></td>
<td><?= safe($row['date']); ?><br><?= safe($row['time']); ?></td>
<td>
    <?= complaint_html(complaint_full_path($conn, ['node_id' => $row['node_id'], 'complaint_name' => $row['complaint_name'], 'category_name' => $row['category_name'], 'subcategory_name' => $row['subcategory_name']])); ?>
</td>
<td><?= safe($row['completiondate']); ?></td>
<td><?= safe($row['attended_status']); ?></td>
<td><?= ratingText($row['user_ratings']); ?></td>
<td><?= nl2br(safe($row['user_feedback'])); ?></td>
</tr>
<?php } } else { ?>
<tr><td colspan="9" class="text-center">No Previous Complaint Found</td></tr>
<?php } ?>
</tbody>
</table>

</div>
</div>
</div>

</div>
<?php include('includes/footer.php'); ?>
</div>
</div>
</div>
<?php include('includes/foot.php'); ?>
</body>
</html>
