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

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-1">Services &amp; Extras</h1>
        <p class="text-muted mb-0"><?= esc($pageSubtitle) ?></p>
    </div>
    <a href="<?= site_url('services/new') ?>" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Add New Service</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 d-flex align-items-center gap-3">
            <div class="rounded-3 bg-primary-subtle text-primary d-flex align-items-center justify-content-center flex-shrink-0" style="width: 44px; height: 44px;"><i class="bi bi-bag-plus-fill fs-5"></i></div>
            <div>
                <div class="text-muted small">Total Services</div>
                <div class="fs-4 fw-bold"><?= $stats['total'] ?></div>
                <div class="text-muted small">All services</div>
            </div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body d-flex align-items-center gap-3">
            <div class="rounded-3 bg-success-subtle text-success d-flex align-items-center justify-content-center flex-shrink-0" style="width: 44px; height: 44px;"><i class="bi bi-check-circle-fill fs-5"></i></div>
            <div>
                <div class="text-muted small">Active Services</div>
                <div class="fs-4 fw-bold"><?= $stats['active'] ?></div>
                <div class="text-muted small">Currently active</div>
            </div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body d-flex align-items-center gap-3">
            <div class="rounded-3 bg-danger-subtle text-danger d-flex align-items-center justify-content-center flex-shrink-0" style="width: 44px; height: 44px;"><i class="bi bi-pause-circle-fill fs-5"></i></div>
            <div>
                <div class="text-muted small">Inactive Services</div>
                <div class="fs-4 fw-bold"><?= $stats['inactive'] ?></div>
                <div class="text-muted small">Currently inactive</div>
            </div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body d-flex align-items-center gap-3">
            <div class="rounded-3 bg-warning-subtle text-warning d-flex align-items-center justify-content-center flex-shrink-0" style="width: 44px; height: 44px;"><i class="bi bi-tags-fill fs-5"></i></div>
            <div>
                <div class="text-muted small">Categories</div>
                <div class="fs-4 fw-bold"><?= $stats['categories'] ?></div>
                <div class="text-muted small">Service categories</div>
            </div>
        </div></div>
    </div>
</div>

<div class="card shadow-sm mb-3">
    <div class="card-body">
        <form action="<?= site_url('services') ?>" method="get" class="row g-2 align-items-center">
            <div class="col-md-5">
                <div class="position-relative">
                    <i class="bi bi-search position-absolute" style="left: 12px; top: 50%; transform: translateY(-50%); color: #9ca3af;"></i>
                    <input type="text" class="form-control ps-4" name="q" placeholder="Search services..." value="<?= esc($filters['q']) ?>">
                </div>
            </div>
            <div class="col-md-3">
                <select class="form-select" name="category">
                    <option value="">All Categories</option>
                    <?php foreach ($lookups->serviceCategories as $key => $meta): ?>
                        <option value="<?= esc($key) ?>" <?= $filters['category'] === $key ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <select class="form-select" name="status">
                    <option value="">All Status</option>
                    <option value="active" <?= $filters['status'] === 'active' ? 'selected' : '' ?>>Active</option>
                    <option value="inactive" <?= $filters['status'] === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-outline-primary w-100"><i class="bi bi-funnel me-1"></i>Filter</button>
            </div>
        </form>
    </div>
</div>

<div class="card shadow-sm">
    <div class="table-scroll">
        <table class="table table-hover mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Service</th>
                    <th>Category</th>
                    <th>Price</th>
                    <th>Unit</th>
                    <th>Status</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($services)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-5">
                        No services match this search/filter — these are the add-ons staff can offer in the New Booking wizard's Add Services step.
                    </td></tr>
                <?php endif; ?>
                <?php foreach ($services as $service):
                    $catMeta = $lookups->serviceCategories[$service->category] ?? ['label' => ucfirst($service->category), 'icon' => 'bi-circle'];
                ?>
                    <tr>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <div class="rounded-circle bg-primary-subtle text-primary d-flex align-items-center justify-content-center flex-shrink-0" style="width: 36px; height: 36px;"><i class="bi <?= $catMeta['icon'] ?>"></i></div>
                                <div>
                                    <div class="fw-semibold"><?= esc($service->name) ?></div>
                                    <div class="text-muted small"><?= esc($service->description ?: ($lookups->serviceUnits[$service->unit_label] ?? $service->unit_label)) ?></div>
                                </div>
                            </div>
                        </td>
                        <td><span class="badge bg-light text-dark border"><?= esc($catMeta['label']) ?></span></td>
                        <td class="fw-semibold"><?= format_money($service->price) ?></td>
                        <td class="text-muted small"><?= esc($lookups->serviceUnits[$service->unit_label] ?? $service->unit_label) ?></td>
                        <td>
                            <span class="badge bg-<?= $service->is_active ? 'success' : 'secondary' ?>"><?= $service->is_active ? 'Active' : 'Inactive' ?></span>
                        </td>
                        <td class="text-end">
                            <div class="d-flex justify-content-end gap-1">
                                <a href="<?= site_url('services/' . $service->id . '/edit') ?>" class="btn btn-sm btn-outline-secondary" title="Edit"><i class="bi bi-pencil"></i></a>
                                <form action="<?= site_url('services/' . $service->id . '/toggle-active') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-secondary" title="<?= $service->is_active ? 'Deactivate' : 'Activate' ?>"><i class="bi bi-toggle<?= $service->is_active ? '-on' : '-off' ?>"></i></button>
                                </form>
                                <form action="<?= site_url('services/' . $service->id . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this service?');">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-danger" title="Delete"><i class="bi bi-trash"></i></button>
                                </form>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php if (! empty($services)): ?>
    <?php
        $current = $pager->getCurrentPage();
        $total   = $pager->getTotal();
        $totalPages = max(1, (int) ceil($total / $perPage));
        $first   = ($current - 1) * $perPage + 1;
        $last    = min($current * $perPage, $total);
        $qs      = array_merge($filters, ['per_page' => $perPage]);
    ?>
    <div class="card-footer bg-white d-flex justify-content-between align-items-center flex-wrap gap-2">
        <span class="text-muted small">Showing <?= $first ?> to <?= $last ?> of <?= $total ?> services</span>
        <div class="d-flex align-items-center gap-2">
            <nav>
                <ul class="pagination pagination-sm mb-0">
                    <li class="page-item <?= $current <= 1 ? 'disabled' : '' ?>">
                        <a class="page-link" href="<?= site_url('services?' . http_build_query(array_merge($qs, ['page' => $current - 1]))) ?>"><i class="bi bi-chevron-left"></i></a>
                    </li>
                    <?php for ($p = 1; $p <= $totalPages; $p++): ?>
                        <li class="page-item <?= $p === $current ? 'active' : '' ?>">
                            <a class="page-link" href="<?= site_url('services?' . http_build_query(array_merge($qs, ['page' => $p]))) ?>"><?= $p ?></a>
                        </li>
                    <?php endfor; ?>
                    <li class="page-item <?= $current >= $totalPages ? 'disabled' : '' ?>">
                        <a class="page-link" href="<?= site_url('services?' . http_build_query(array_merge($qs, ['page' => $current + 1]))) ?>"><i class="bi bi-chevron-right"></i></a>
                    </li>
                </ul>
            </nav>
            <form action="<?= site_url('services') ?>" method="get" id="perPageForm">
                <input type="hidden" name="q" value="<?= esc($filters['q']) ?>">
                <input type="hidden" name="category" value="<?= esc($filters['category']) ?>">
                <input type="hidden" name="status" value="<?= esc($filters['status']) ?>">
                <select class="form-select form-select-sm" name="per_page" onchange="document.getElementById('perPageForm').submit();" style="width: auto;">
                    <option value="10" <?= $perPage === 10 ? 'selected' : '' ?>>10 per page</option>
                    <option value="25" <?= $perPage === 25 ? 'selected' : '' ?>>25 per page</option>
                    <option value="50" <?= $perPage === 50 ? 'selected' : '' ?>>50 per page</option>
                </select>
            </form>
        </div>
    </div>
    <?php endif; ?>
</div>

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