<?php
session_start();
include('include/config.php');

if(strlen($_SESSION['alogin'])==0){    
    header('location:index.php');
    exit();
} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin | Complaint Details</title>

<!-- Bootstrap 5 & Icons -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">

<script>
var popUpWin = 0;
function popUpWindow(URLStr, left, top, width, height) {
    if(popUpWin && !popUpWin.closed) popUpWin.close();
    popUpWin = window.open(URLStr,'popUpWin',
        'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+width+',height='+height+',left='+left+',top='+top);
}
</script>

<style>
body { font-family: 'Poppins', sans-serif; background:#f7f8fa; margin:0; }

/* Header */
.navbar {
    position: fixed;
    top:0; left:0;
    width:100%;
    z-index:1000;
    background: linear-gradient(90deg, #ff7a00, #ffb366);
    color:#fff;
    padding:10px 30px;
    display:flex;
    align-items:center;
}

/* Layout */
.wrapper { display:flex; margin-top:60px; min-height:calc(100vh - 60px); }

/* Sidebar */
.sidebar {
    width:250px;
    background: linear-gradient(180deg, #ff7a00, #ffb366);
    color:#fff;
    flex-shrink:0;
    position:sticky;
    top:0;
    height:100vh;
    overflow-y:auto;
    padding-top:20px;
    z-index: 999;
}
.sidebar a { display:block; color:#fff; padding:12px 20px; text-decoration:none; transition:0.3s; }
.sidebar a:hover { background:rgba(255,255,255,0.15); padding-left:25px; }

/* Content */
.content { flex:1; padding:40px; z-index: 998; }

/* Card & Table */
.card { border-radius:12px; box-shadow:0 5px 20px rgba(0,0,0,0.08); }
.table th { background:#ff7a00; color:#fff; }
.table tbody tr:hover { background-color: rgba(255, 123, 0, 0.1); }
.table thead th { position: sticky; top:0; z-index:1; }

/* Footer */
footer { padding:10px; text-align:center; border-top:1px solid #ddd; background:#fff; margin-top:20px; }

/* Buttons */
.btn { margin-right: 5px; margin-bottom:5px; }

/* Scrollbar for Sidebar */
.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.3); border-radius: 3px; }

/* Responsive */
@media(max-width:992px){
    .wrapper { flex-direction:column; }
    .sidebar { width:100%; height:auto; position:relative; }
    .content { margin-left:0; padding:20px; }
}
</style>
</head>
<body>

<!-- Header -->
<?php include('include/header.php'); ?>

<div class="wrapper">
    <!-- Sidebar -->
    <div class="sidebar">
        <?php include('include/sidebar.php'); ?>
    </div>

    <!-- Main Content -->
    <div class="content">
        <div class="card p-4">
            <h4 class="mb-3"><i class="bi bi-clipboard-fill me-2"></i>Complaint Details</h4>
            <div class="table-responsive">
                <table class="table table-bordered table-striped align-middle">
                    <tbody>
<?php
$cid = mysqli_real_escape_string($bd, $_GET['cid']);
$query=mysqli_query($bd, "SELECT tblcomplaints.*, users.fullName as name, category.categoryName as catname 
FROM tblcomplaints 
JOIN users ON users.id=tblcomplaints.userId 
JOIN category ON category.id=tblcomplaints.category 
WHERE tblcomplaints.complaintNumber='$cid'");

while($row=mysqli_fetch_array($query)) {
?>
<tr>
<th>Complaint Number</th><td><?php echo htmlentities($row['complaintNumber']); ?></td>
<th>Complainant Name</th><td><?php echo htmlentities($row['name']); ?></td>
<th>Reg Date</th><td><?php echo htmlentities($row['regDate']); ?></td>
</tr>
<tr>
<th>Category</th><td><?php echo htmlentities($row['catname']); ?></td>
<th>SubCategory</th><td><?php echo htmlentities($row['subcategory']); ?></td>
<th>Complaint Type</th><td><?php echo htmlentities($row['complaintType']); ?></td>
</tr>
<tr>
<th>State</th><td><?php echo htmlentities($row['state']); ?></td>
<th>Nature of Complaint</th><td colspan="3"><?php echo htmlentities($row['noc']); ?></td>
</tr>
<tr>
<th>Complaint Details</th><td colspan="5"><?php echo htmlentities($row['complaintDetails']); ?></td>
</tr>
<tr>
<th>File (if any)</th><td colspan="5">
<?php $cfile=$row['complaintFile'];
if($cfile=="" || $cfile=="NULL") { echo "File NA"; } 
else { ?><a href="../users/complaintdocs/<?php echo htmlentities($row['complaintFile']);?>" target="_blank">View File</a><?php } ?>
</td></tr>
<tr>
<th>Action</th>
<td>
<?php if($row['status']!="closed") { ?>
<button type="button" class="btn btn-primary" onclick="popUpWindow('updatecomplaint.php?cid=<?php echo htmlentities($row['complaintNumber']); ?>',100,100,600,600)">
Take Action
</button>
<?php } ?>
</td>
<td colspan="4">
<button type="button" class="btn btn-success" onclick="popUpWindow('view-user.php?uid=<?php echo htmlentities($row['userId']); ?>',100,100,500,600)">
View User Details
</button>
</td>
</tr>
<?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<footer>&copy; <?php echo date("Y"); ?> Admin Dashboard</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<?php } ?>
