<?php
require_once __DIR__ . '/../config/config.php';

// Access Check: Only requires the user to be logged in
if (!isset($_SESSION['user_id'])) { 
    header('Location: ../login.php'); exit; 
}

$query = "SELECT st.*, o.item_name 
          FROM support_tickets st 
          LEFT JOIN orders o ON st.order_id = o.id 
          WHERE st.user_id = ? 
          ORDER BY st.created_at DESC";
          
$stmt = $pdo->prepare($query);
$stmt->execute([$_SESSION['user_id']]);
$tickets = $stmt->fetchAll(PDO::FETCH_ASSOC);

require_once __DIR__ . '/../sections/header.php';
?>

<body class="bg-nftDark text-white antialiased overflow-x-hidden">
    <?php include_once __DIR__ . '/client-sidebar.php'; ?>

    <main class="lg:ml-64 min-h-screen pt-24 lg:pt-10 pb-16 px-4 sm:px-6 lg:px-8">
        <div class="max-w-6xl mx-auto space-y-8">
            
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between border-b border-slate-900/60 pb-6 gap-4">
                <div>
                    <span class="text-xs font-bold tracking-widest text-nftAccentCyan uppercase block mb-1">// Communications Core</span>
                    <h1 class="text-3xl font-extrabold tracking-tight text-white">Support Tickets</h1>
                </div>
                <div>
                    <a href="create-ticket.php" class="inline-block py-3 px-6 rounded-xl bg-gradient-to-r from-nftAccentCyan to-nftAccentPurple font-bold text-xs uppercase tracking-widest text-white hover:opacity-90 transition-all shadow-lg">
                        + Create New Ticket
                    </a>
                </div>
            </div>

            <div class="glass-card rounded-2xl border border-slate-800/80 bg-slate-950/20 overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="w-full text-left border-collapse">
                        <thead>
                            <tr class="border-b border-slate-900 bg-slate-950/40 text-xs font-bold uppercase text-nftTextGray tracking-wider">
                                <th class="p-4 sm:p-5">SL</th>
                                <th class="p-4 sm:p-5">Date</th>
                                <th class="p-4 sm:p-5">Associated Item</th>
                                <th class="p-4 sm:p-5">Subject</th>
                                <th class="p-4 sm:p-5">Priority</th>
                                <th class="p-4 sm:p-5">Status</th>
                                <th class="p-4 sm:p-5 text-right">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-900/60 text-sm">
                            <?php if (empty($tickets)): ?>
                                <tr>
                                    <td colspan="7" class="p-8 text-center text-nftTextGray font-mono text-xs">
                                        No structural support records located matching your signature.
                                    </td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($tickets as $index => $t): ?>
                                    <tr class="hover:bg-slate-900/20 transition-all">
                                        <td class="p-4 sm:p-5 font-mono text-xs text-nftTextGray"><?= $index + 1 ?></td>
                                        <td class="p-4 sm:p-5 text-slate-300"><?= date('M d, Y', strtotime($t['created_at'])) ?></td>
                                        <td class="p-4 sm:p-5 font-semibold text-white"><?= htmlspecialchars($t['item_name'] ?? 'N/A') ?></td>
                                        <td class="p-4 sm:p-5 text-slate-300 max-w-xs truncate"><?= htmlspecialchars($t['subject']) ?></td>
                                        <td class="p-4 sm:p-5">
                                            <span class="px-2.5 py-1 rounded-md text-xs font-bold tracking-wider uppercase <?= $t['priority'] === 'High' ? 'bg-rose-500/10 text-rose-400 border border-rose-500/20' : 'bg-slate-800 text-slate-400' ?>">
                                                <?= $t['priority'] ?>
                                            </span>
                                        </td>
                                        <td class="p-4 sm:p-5">
                                            <span class="px-2.5 py-1 rounded-md text-xs font-bold tracking-wider uppercase <?= $t['ticket_status'] === 'Solved' ? 'bg-emerald-500/10 text-emerald-400 border border-emerald-500/20' : 'bg-amber-500/10 text-amber-400 border border-amber-500/20' ?>">
                                                <?= $t['ticket_status'] ?>
                                            </span>
                                        </td>
                                        <td class="p-4 sm:p-5 text-right">
                                            <a href="ticket-details.php?id=<?= $t['id'] ?>" class="text-xs font-bold uppercase tracking-widest text-nftAccentCyan hover:text-white transition-all">
                                                Details &rarr;
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>
</body>
</html>