<?php include app()->basePath('app/Views/partials/header.php'); ?>

<?php
    $baseUrl = $isAdmin ? '/admin/support' : '/support';
    $replyAction = $isAdmin ? '/admin/support/reply' : '/support/reply';
?>

<section class="support-layout">
    <div class="panel support-thread">
        <div class="page-head">
            <div>
                <p class="eyebrow"><?= e($ticket['ticket_number']) ?></p>
                <h1><?= e($ticket['subject']) ?></h1>
                <p><?= e($categories[$ticket['category']] ?? $ticket['category']) ?> - <?= e(format_date($ticket['created_at'] ?? '')) ?></p>
            </div>
            <span class="badge <?= e($ticket['status']) ?>"><?= e($statuses[$ticket['status']] ?? $ticket['status']) ?></span>
        </div>

        <div class="support-message original">
            <div class="support-avatar"><?= e(strtoupper(substr((string) ($ticket['user_name'] ?? 'U'), 0, 1))) ?></div>
            <div>
                <div class="support-message-head">
                    <strong><?= e($ticket['user_name']) ?></strong>
                    <span><?= e(format_date($ticket['created_at'] ?? '')) ?></span>
                </div>
                <p><?= nl2br(e($ticket['message'])) ?></p>
                <?php if (!empty($ticket['attachment'])): ?>
                    <a class="mini-button" target="_blank" href="/support/attachment?ticket_id=<?= e((string) $ticket['id']) ?>">Abrir anexo</a>
                <?php endif; ?>
            </div>
        </div>

        <?php foreach (($ticket['replies'] ?? []) as $index => $reply): ?>
            <div class="support-message <?= ($reply['user_role'] ?? '') === 'admin' ? 'staff' : '' ?>">
                <div class="support-avatar"><?= ($reply['user_role'] ?? '') === 'admin' ? 'S' : e(strtoupper(substr((string) ($reply['user_name'] ?? 'U'), 0, 1))) ?></div>
                <div>
                    <div class="support-message-head">
                        <strong><?= e($reply['user_name']) ?><?= ($reply['user_role'] ?? '') === 'admin' ? ' - Suporte' : '' ?></strong>
                        <span><?= e(format_date($reply['created_at'] ?? '')) ?></span>
                    </div>
                    <p><?= nl2br(e($reply['message'])) ?></p>
                    <?php if (!empty($reply['attachment'])): ?>
                        <a class="mini-button" target="_blank" href="/support/attachment?ticket_id=<?= e((string) $ticket['id']) ?>&reply=<?= e((string) $index) ?>">Abrir anexo</a>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>

        <?php if (($ticket['status'] ?? '') !== 'closed'): ?>
            <form class="form support-reply-form" method="post" action="<?= e($replyAction) ?>" enctype="multipart/form-data">
                <?= csrf_field() ?>
                <input type="hidden" name="ticket_id" value="<?= e((string) $ticket['id']) ?>">
                <label>Responder
                    <textarea name="message" rows="5" placeholder="Escreva a sua resposta..." required></textarea>
                </label>
                <label>Anexo opcional
                    <input name="attachment" type="file" accept=".pdf,.jpg,.jpeg,.png">
                </label>
                <button class="button" type="submit">Enviar resposta</button>
            </form>
        <?php else: ?>
            <div class="alert success">Este ticket esta fechado.</div>
        <?php endif; ?>
    </div>

    <aside class="panel support-side">
        <h2>Resumo</h2>
        <dl class="detail-list">
            <div><dt>Estado</dt><dd><?= e($statuses[$ticket['status']] ?? $ticket['status']) ?></dd></div>
            <div><dt>Categoria</dt><dd><?= e($categories[$ticket['category']] ?? $ticket['category']) ?></dd></div>
            <div><dt>Prioridade</dt><dd><?= e(ucfirst($ticket['priority'])) ?></dd></div>
            <div><dt>Utilizador</dt><dd><?= e($ticket['user_name']) ?></dd></div>
            <div><dt>Email</dt><dd><?= e($ticket['user_email']) ?></dd></div>
        </dl>

        <?php if ($isAdmin): ?>
            <form class="form compact-form" method="post" action="/admin/support/status">
                <?= csrf_field() ?>
                <input type="hidden" name="ticket_id" value="<?= e((string) $ticket['id']) ?>">
                <label>Alterar estado
                    <select name="status">
                        <?php foreach ($statuses as $value => $label): ?>
                            <option value="<?= e($value) ?>" <?= ($ticket['status'] ?? '') === $value ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </label>
                <button class="button" type="submit">Guardar estado</button>
            </form>
        <?php endif; ?>

        <a class="ghost-button" href="<?= e($baseUrl) ?>">Voltar aos tickets</a>
    </aside>
</section>

<?php include app()->basePath('app/Views/partials/footer.php'); ?>
