<?php
// /public_html/admin/tickets.php
session_start();
require '../config/db_connect.php';

// Admin Auth
if (!isset($_SESSION['user_id'])) { header("Location: ../login.php"); exit(); }
$check = $conn->query("SELECT role FROM users WHERE id = {$_SESSION['user_id']}")->fetch_assoc();
if (($check['role'] ?? '') !== 'admin') { die("Access Denied"); }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Support Tickets - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
    <?php include 'includes/navbar.php'; ?>

    <div class="container mt-5">
        <h2 class="mb-4">Support Tickets</h2>

        <div class="card shadow-sm">
            <div class="card-body p-0">
                <table class="table table-hover mb-0">
                    <thead class="table-dark">
                        <tr>
                            <th>ID</th>
                            <th>User ID</th>
                            <th>Subject</th>
                            <th>Status</th>
                            <th>Last Update</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $sql = "SELECT * FROM tickets ORDER BY 
                                CASE WHEN status = 'open' THEN 1 ELSE 2 END, 
                                updated_at DESC";
                        $res = $conn->query($sql);
                        
                        while($row = $res->fetch_assoc()):
                        ?>
                        <tr class="<?php echo ($row['status'] == 'open') ? 'table-warning' : ''; ?>">
                            <td>#<?php echo $row['id']; ?></td>
                            <td>User #<?php echo $row['user_id']; ?></td>
                            <td><?php echo htmlspecialchars($row['subject']); ?></td>
                            <td>
                                <?php if($row['status'] == 'open'): ?>
                                    <span class="badge bg-danger">Needs Reply</span>
                                <?php elseif($row['status'] == 'answered'): ?>
                                    <span class="badge bg-success">Answered</span>
                                <?php else: ?>
                                    <span class="badge bg-secondary">Closed</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo date('M d, H:i', strtotime($row['updated_at'])); ?></td>
                            <td>
                                <a href="ticket_reply.php?id=<?php echo $row['id']; ?>" class="btn btn-sm btn-primary">Open</a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>