<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<?php
$tabs = [
    'all'         => 'All',
    'open'        => 'Open',
    'assigned'    => 'Assigned',
    'resolved'    => 'Resolved',
    'followed_up' => 'Followed Up',
];
?>

<div class="d-flex justify-content-end mb-3">
    <a href="<?= site_url('complaints/new') ?>" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i>Log Complaint</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Open</div>
            <div class="fs-3 fw-bold text-danger"><?= $stats['open'] ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Assigned</div>
            <div class="fs-3 fw-bold text-warning"><?= $stats['assigned'] ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Resolved</div>
            <div class="fs-3 fw-bold text-success"><?= $stats['resolved'] ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Followed Up</div>
            <div class="fs-3 fw-bold"><?= $stats['followedUp'] ?></div>
        </div></div>
    </div>
</div>

<div class="card">
    <div class="card-header p-0">
        <ul class="nav nav-tabs border-0 px-2 pt-2">
            <?php foreach ($tabs as $key => $label): ?>
                <li class="nav-item"><a class="nav-link <?= $tab === $key ? 'active' : '' ?>" href="<?= site_url('complaints?tab=' . $key) ?>"><?= esc($label) ?></a></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <div class="table-responsive">
        <table class="table mb-0 align-middle small">
            <thead><tr><th>#</th><th>Guest</th><th>Category</th><th>Priority</th><th>Status</th><th>Assigned To</th><th>Logged</th><th></th></tr></thead>
            <tbody>
            <?php if (empty($complaints)): ?>
                <tr><td colspan="8" class="text-center text-muted py-4">No complaints in this view.</td></tr>
            <?php endif; ?>
            <?php foreach ($complaints as $c):
                $prio   = $lookups->taskPriorities[$c->priority] ?? ['label' => ucfirst($c->priority), 'badge' => 'secondary'];
                $status = $lookups->complaintStatuses[$c->status] ?? ['label' => ucfirst($c->status), 'badge' => 'secondary'];
                $cat    = $lookups->complaintCategories[$c->category]['label'] ?? $c->category;
            ?>
                <tr>
                    <td class="fw-semibold">CR-<?= sprintf('%04d', $c->id) ?></td>
                    <td><?= esc($c->guest_name) ?></td>
                    <td><?= esc($cat) ?></td>
                    <td><span class="badge bg-<?= $prio['badge'] ?>"><?= esc($prio['label']) ?></span></td>
                    <td><span class="badge bg-<?= $status['badge'] ?>"><?= esc($status['label']) ?></span></td>
                    <td class="text-muted"><?= esc($c->assignee_name ?: '—') ?></td>
                    <td class="text-muted"><?= esc(date('d M Y', strtotime($c->created_at))) ?></td>
                    <td class="text-end"><a href="<?= site_url('complaints/' . $c->id) ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?= $this->endSection() ?>
