<?php
// File: /admin/ticket_view.php
include 'header.php'; 

if (!isset($_GET['id'])) {
    header('Location: tickets.php');
    exit();
}

$ticket_id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);
$ticket = null;
$replies = [];

// Fetch main ticket details, joining with users table to get the user's name
$stmt = $conn->prepare("SELECT st.*, u.username FROM support_tickets st JOIN users u ON st.user_id = u.id WHERE st.id = ?");
$stmt->bind_param("i", $ticket_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
    $ticket = $result->fetch_assoc();
} else {
    header('Location: tickets.php?error=not_found');
    exit();
}
$stmt->close();

// Fetch all replies, joining with users to get the replier's name
$stmt_replies = $conn->prepare("SELECT tr.*, u.username, u.role FROM ticket_replies tr JOIN users u ON tr.user_id = u.id WHERE tr.ticket_id = ? ORDER BY tr.created_at ASC");
$stmt_replies->bind_param("i", $ticket_id);
$stmt_replies->execute();
$replies_result = $stmt_replies->get_result();
if ($replies_result) {
    $replies = $replies_result->fetch_all(MYSQLI_ASSOC);
}
$stmt_replies->close();
?>

<div class="p-6 md:p-8">
    <div class="max-w-4xl mx-auto">
        <div class="flex items-center mb-6">
            <a href="tickets.php" class="text-ucf-green hover:text-ucf-green-dark mr-4"><i class="fas fa-arrow-left fa-lg"></i></a>
            <div>
                <h1 class="text-3xl font-bold text-ucf-charcoal">Ticket #<?php echo $ticket['id']; ?></h1>
                <p class="text-gray-500">Subject: <?php echo htmlspecialchars($ticket['subject']); ?> | From: <?php echo htmlspecialchars($ticket['username']); ?></p>
            </div>
        </div>

        <div class="bg-white rounded-lg shadow-md">
            <!-- Original Ticket Message -->
            <div class="p-6 border-b border-gray-200">
                <div class="flex items-start space-x-4">
                    <div class="flex-shrink-0"><i class="fas fa-user-circle fa-2x text-gray-400"></i></div>
                    <div class="flex-1">
                        <p class="font-bold text-ucf-charcoal"><?php echo htmlspecialchars($ticket['username']); ?></p>
                        <p class="text-sm text-gray-500 mb-2">Posted on <?php echo date('M d, Y, h:i A', strtotime($ticket['created_at'])); ?></p>
                        <div class="prose max-w-none"><p><?php echo nl2br(htmlspecialchars($ticket['message'])); ?></p></div>
                    </div>
                </div>
            </div>

            <!-- Replies -->
            <?php foreach ($replies as $reply): ?>
                <div class="p-6 border-b border-gray-200 <?php echo ($reply['role'] === 'admin') ? 'bg-green-50' : ''; ?>">
                    <div class="flex items-start space-x-4">
                        <div class="flex-shrink-0"><i class="fas fa-user-circle fa-2x <?php echo ($reply['role'] === 'admin') ? 'text-ucf-green' : 'text-gray-400'; ?>"></i></div>
                        <div class="flex-1">
                            <p class="font-bold text-ucf-charcoal"><?php echo htmlspecialchars($reply['username']); ?> <?php if($reply['role'] === 'admin') echo '(Staff)'; ?></p>
                            <p class="text-sm text-gray-500 mb-2">Replied on <?php echo date('M d, Y, h:i A', strtotime($reply['created_at'])); ?></p>
                            <div class="prose max-w-none"><p><?php echo nl2br(htmlspecialchars($reply['message'])); ?></p></div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>

            <!-- Add Reply Form -->
            <div class="p-6 bg-gray-50 rounded-b-lg">
                <h3 class="text-xl font-bold text-ucf-charcoal mb-4">Post a Reply</h3>
                <form action="ticket_handler.php" method="POST">
                    <input type="hidden" name="ticket_id" value="<?php echo $ticket['id']; ?>">
                    <div>
                        <textarea name="message" rows="5" required class="w-full rounded-md border-gray-300 shadow-sm" placeholder="Type your reply to the user..."></textarea>
                    </div>
                    <div class="mt-4 flex justify-between items-center">
                        <div class="flex space-x-2">
                             <button type="submit" name="add_reply_and_close" class="bg-gray-600 text-white font-bold py-2 px-4 rounded-lg hover:bg-gray-700">Reply & Close</button>
                             <button type="submit" name="close_ticket" class="bg-red-600 text-white font-bold py-2 px-4 rounded-lg hover:bg-red-700">Close Ticket</button>
                        </div>
                        <button type="submit" name="add_reply" class="bg-ucf-green text-white font-bold py-2 px-6 rounded-lg hover:bg-ucf-green-dark">Submit Reply</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>

