<?php
$err = $_GET['err'] ?? '';
?>
<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

    <div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
        <div><h3 class="mb-1 fw-bold"><i class="bi bi-headset me-2 text-primary"></i><?= __('support.title','Support') ?></h3><p class="text-muted mb-0 small"><?= __('support.subtitle','Get help and manage your support tickets') ?></p></div>
        <a class="btn btn-primary" href="#newTicket">
            <i class="bi bi-plus-circle me-1"></i>
            <?= __('support.new_ticket','New Ticket') ?>
        </a>
    </div>

    <?php if ($error === 'server_unreachable'): ?>
        <div class="alert alert-danger">
            <i class="bi bi-exclamation-triangle me-1"></i>
            <?= __('support.server_unreachable','Support server unreachable.') ?>
        </div>
    <?php endif; ?>

    <?php if ($err === 'missing_fields'): ?>
        <div class="alert alert-warning">
            <i class="bi bi-info-circle me-1"></i>
            <?= __('support.missing_fields','Please fill subject and message.') ?>
        </div>
    <?php elseif ($err === 'failed'): ?>
        <div class="alert alert-danger">
            <i class="bi bi-exclamation-triangle me-1"></i>
            <?= __('support.failed','Failed to send. Please try again.') ?>
        </div>
    <?php endif; ?>

    <div class="card border-0 shadow-sm mb-4">
        <div class="card-header fw-bold">
            <i class="bi bi-inboxes me-1"></i>
            <?= __('support.my_tickets','My Tickets') ?>
        </div>
        <div class="card-body table-responsive">
            <table class="table table-hover align-middle">
                <thead>
                    <tr>
                        <th>#</th>
                        <th><?= __('support.subject','Subject') ?></th>
                        <th><?= __('support.status','Status') ?></th>
                        <th><?= __('support.updated_at','Updated') ?></th>
                        <th><?= __('actions.view','View') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php if (empty($tickets)): ?>
                    <tr>
                        <td colspan="5" class="text-center text-muted">
                            <?= __('support.no_tickets','No tickets yet.') ?>
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($tickets as $t): ?>
                        <tr>
                            <td><?= (int)$t['id'] ?></td>
                            <td><?= htmlspecialchars((string)$t['subject']) ?></td>
                            <td>
                                <?php $st = (string)($t['status'] ?? 'open'); ?>
                                <?php if ($st === 'open'): ?>
                                    <span class="badge bg-danger"><?= __('support.status_open','Open') ?></span>
                                <?php elseif ($st === 'answered'): ?>
                                    <span class="badge bg-success"><?= __('support.status_answered','Answered') ?></span>
                                <?php else: ?>
                                    <span class="badge bg-secondary"><?= __('support.status_closed','Closed') ?></span>
                                <?php endif; ?>
                            </td>
                            <td><?= !empty($t['updated_at']) ? date('Y-m-d H:i', (int)$t['updated_at']) : '—' ?></td>
                            <td>
                                <a class="btn btn-sm btn-info" href="/admin/support/view/<?= (int)$t['id'] ?>">
                                    <?= __('action.view','View') ?>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div id="newTicket" class="card shadow-sm">
        <div class="card-header fw-bold">
            <i class="bi bi-plus-circle me-1"></i>
            <?= __('support.new_ticket','New Ticket') ?>
        </div>
        <div class="card-body">
            <form method="POST" action="/admin/support/create">
                <?= \Core\System\Csrf::field(); ?>

                <div class="row g-3">
                    <div class="col-md-8">
                        <label class="form-label"><?= __('support.subject','Subject') ?></label>
                        <input class="form-control" name="subject" required />
                    </div>
                    <div class="col-md-4">
                        <label class="form-label"><?= __('support.priority','Priority') ?></label>
                        <select class="form-control" name="priority">
                            <option value="low"><?= __('support.priority_low','Low') ?></option>
                            <option value="normal" selected><?= __('support.priority_normal','Normal') ?></option>
                            <option value="high"><?= __('support.priority_high','High') ?></option>
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label"><?= __('support.message','Message') ?></label>
                        <textarea class="form-control" name="message" rows="6" required></textarea>
                    </div>
                </div>

                <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>

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