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

<div class="row justify-content-center">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-bag-plus" style="color: var(--brand);"></i> <?= $service ? 'Edit Service' : 'New Service' ?>
            </div>
            <div class="card-body">
                <form action="<?= $service ? site_url('services/' . $service->id) : site_url('services') ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Name</label>
                        <input type="text" class="form-control" name="name" value="<?= esc(old('name', $service->name ?? '')) ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Description <span class="text-muted fw-normal">(optional)</span></label>
                        <input type="text" class="form-control" name="description" value="<?= esc(old('description', $service->description ?? '')) ?>">
                    </div>
                    <div class="row g-3 mb-3">
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Category</label>
                            <select class="form-select" name="category">
                                <?php foreach ($lookups->serviceCategories as $key => $meta): ?>
                                    <option value="<?= $key ?>" <?= old('category', $service->category ?? 'other') === $key ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Price (GHS)</label>
                            <input type="number" step="0.01" min="0" class="form-control" name="price" value="<?= esc(old('price', $service->price ?? '')) ?>" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Unit</label>
                            <select class="form-select" name="unit_label">
                                <?php foreach ($lookups->serviceUnits as $key => $label): ?>
                                    <option value="<?= $key ?>" <?= old('unit_label', $service->unit_label ?? 'flat') === $key ? 'selected' : '' ?>><?= esc($label) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-check form-switch mb-4">
                        <input class="form-check-input" type="checkbox" role="switch" id="is_active" name="is_active" value="1" <?= old('is_active', $service->is_active ?? 1) ? 'checked' : '' ?>>
                        <label class="form-check-label small fw-semibold" for="is_active">Active <span class="text-muted fw-normal">— offered in the New Booking wizard</span></label>
                    </div>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="bi bi-check2 me-1"></i>Save Service</button>
                        <a href="<?= site_url('services') ?>" class="btn btn-outline-secondary">Cancel</a>
                        <?php if ($service): ?>
                            <form action="<?= site_url('services/' . $service->id . '/delete') ?>" method="post" class="ms-auto" onsubmit="return confirm('Delete this service?');">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-outline-danger"><i class="bi bi-trash me-1"></i>Delete</button>
                            </form>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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