<?php
$err = $_GET['err'] ?? '';
$saved = $_GET['saved'] ?? '';
$closed = $_GET['closed'] ?? '';

// ✅ NEW: ticketStatus comes from SupportController::view()
$ticketStatus = isset($ticketStatus) ? strtolower(trim((string)$ticketStatus)) : '';

// ✅ Closed if: status closed OR closed flag OR ticket_closed error
$isClosed = (
    ($ticketStatus === 'closed')
    || ($closed !== '' && $closed !== '0')
    || ($err === 'ticket_closed')
);
?>
<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid py-4">

    <div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
        <h3 class="mb-0">
            <i class="bi bi-chat-left-text me-2"></i>
            <?= __('support.ticket','Ticket') ?> #<?= (int)$ticketId ?>
        </h3>

        <div class="d-flex gap-2">
            <a class="btn btn-secondary" href="/admin/support">
                <i class="bi bi-arrow-left me-1"></i>
                <?= __('actions.back','Back') ?>
            </a>

            <?php if (!$isClosed): ?>
                <form method="POST" action="/admin/support/close/<?= (int)$ticketId ?>" class="d-inline"
                      onsubmit="return confirm('<?= __('support.confirm_close','Are you sure you want to close this ticket?') ?>');">
                    <?= \Core\System\Csrf::field(); ?>
                    <button class="btn btn-outline-warning">
                        <i class="bi bi-lock me-1"></i>
                        <?= __('support.close_ticket','Close ticket') ?>
                    </button>
                </form>
            <?php endif; ?>
        </div>
    </div>

    <?php if ($saved): ?>
        <div class="alert alert-success">
            <i class="bi bi-check-circle me-1"></i>
            <?= __('support.sent','Message sent.') ?>
        </div>
    <?php endif; ?>

    <?php if ($err === 'server_unreachable'): ?>
        <div class="alert alert-danger">
            <i class="bi bi-exclamation-triangle me-1"></i>
            <?= __('support.server_unreachable','Support server unreachable.') ?>
        </div>
    <?php elseif ($err === 'missing_message'): ?>
        <div class="alert alert-warning">
            <i class="bi bi-info-circle me-1"></i>
            <?= __('support.missing_message','Please write a message.') ?>
        </div>
    <?php elseif ($err === 'not_allowed'): ?>
        <div class="alert alert-danger">
            <i class="bi bi-shield-x me-1"></i>
            <?= __('support.not_allowed','You are not allowed to view this ticket.') ?>
        </div>
    <?php elseif ($err === 'ticket_closed'): ?>
        <div class="alert alert-info">
            <i class="bi bi-lock me-1"></i>
            <?= __('support.ticket_closed_no_reply','Ticket is closed. You cannot reply now.') ?>
        </div>
    <?php endif; ?>

    <?php if ($isClosed && $err !== 'ticket_closed'): ?>
        <div class="alert alert-info">
            <i class="bi bi-lock me-1"></i>
            <?= __('support.ticket_closed_no_reply','Ticket is closed. You cannot reply now.') ?>
        </div>
    <?php endif; ?>

    <div class="card shadow-sm mb-3">
        <div class="card-body" style="max-height:480px; overflow:auto; background:#f8f9fa; border-radius:12px;">
            <?php foreach ($messages as $m): ?>
                <?php $sender = (string)($m['sender'] ?? 'client'); ?>
                <div class="mb-3">
                    <div class="d-flex align-items-center gap-2 mb-1">
                        <?php if ($sender === 'admin'): ?>
                            <span class="badge bg-primary"><?= __('support.from_admin','Admin') ?></span>
                        <?php else: ?>
                            <span class="badge bg-dark"><?= __('support.from_client','You') ?></span>
                        <?php endif; ?>
                        <small class="text-muted"><?= !empty($m['created_at']) ? date('Y-m-d H:i', (int)$m['created_at']) : '' ?></small>
                    </div>
                    <div class="p-2 bg-white border rounded-3">
                        <?= nl2br(htmlspecialchars((string)($m['message'] ?? ''))) ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>

    <?php if (!$isClosed): ?>
        <div class="card shadow-sm">
            <div class="card-header fw-bold">
                <i class="bi bi-reply me-1"></i>
                <?= __('support.reply','Reply') ?>
            </div>
            <div class="card-body">
                <form method="POST" action="/admin/support/reply/<?= (int)$ticketId ?>">
                    <?= \Core\System\Csrf::field(); ?>
                    <textarea class="form-control" name="message" rows="5" placeholder="<?= __('support.write_message','Write a message...') ?>" required></textarea>
                    <div class="mt-3">
                        <button class="btn btn-success">
                            <i class="bi bi-send me-1"></i>
                            <?= __('actions.send','Send') ?>
                        </button>
                    </div>
                </form>
            </div>
        </div>
    <?php endif; ?>

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