<div class="d-flex align-items-center mb-3">
    <a href="<?= url('/SuperAdmin/defaultChecklists') ?>" class="btn btn-sm btn-outline-secondary me-2"><i class="fas fa-arrow-left"></i></a>
    <h5 class="fw-bold mb-0"><?= $checklist ? 'Edit' : 'New' ?> Checklist</h5>
</div>

<div class="row">
    <div class="col-md-5">
        <div class="rs-card p-3 mb-3">
            <form method="post" action="<?= url('/SuperAdmin/saveChecklist') ?>">
                <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                <input type="hidden" name="default_checklist_id" value="<?= (int) ($checklist['default_checklist_id'] ?? 0) ?>">
                <div class="mb-3">
                    <label class="form-label small">Name</label>
                    <input type="text" name="name" class="form-control" value="<?= e($checklist['name'] ?? '') ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label small">Description</label>
                    <textarea name="description" class="form-control" rows="3"><?= e($checklist['description'] ?? '') ?></textarea>
                </div>
                <div class="row mb-3">
                    <div class="col"><label class="form-label small">Interval (days)</label><input type="number" name="interval_days" class="form-control" value="<?= (int) ($checklist['interval_days'] ?? 30) ?>"></div>
                    <div class="col"><label class="form-label small">Est. Minutes</label><input type="number" name="estimated_minutes" class="form-control" value="<?= (int) ($checklist['estimated_minutes'] ?? 60) ?>"></div>
                </div>
                <div class="mb-3">
                    <label class="form-check small"><input type="checkbox" name="is_active" class="form-check-input" <?= ($checklist['is_active'] ?? 1) ? 'checked' : '' ?>> Active</label>
                </div>
                <button type="submit" class="btn btn-primary">Save Checklist</button>
            </form>
        </div>
    </div>

    <?php if ($checklist): ?>
    <div class="col-md-7">
        <div class="rs-card p-3 mb-3">
            <h6 class="fw-bold mb-3">Tasks</h6>
            <?php if (!empty($tasks)): ?>
                <table class="table table-sm mb-3"><thead><tr><th>#</th><th>Title</th><th>Type</th><th>Req</th><th></th></tr></thead><tbody>
                    <?php foreach ($tasks as $t): ?>
                        <tr><td><?= (int) $t['sort_order'] ?></td><td><?= e($t['title']) ?></td><td class="small"><?= e($t['input_type']) ?></td><td><?= $t['is_required'] ? '<i class="fas fa-check text-success"></i>' : '' ?></td><td><a href="<?= url('/SuperAdmin/deleteTask/' . (int) $t['task_id']) ?>" class="text-danger small" onclick="return confirm('Remove this task?')"><i class="fas fa-times"></i></a></td></tr>
                    <?php endforeach; ?>
                </tbody></table>
            <?php else: ?>
                <p class="text-muted small">No tasks yet.</p>
            <?php endif; ?>

            <form method="post" action="<?= url('/SuperAdmin/saveTask/' . (int) $checklist['default_checklist_id']) ?>">
                <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                <div class="row g-2">
                    <div class="col-1"><input type="number" name="sort_order" class="form-control form-control-sm" value="<?= count($tasks ?? []) + 1 ?>" title="Sort order"></div>
                    <div class="col-5"><input type="text" name="title" class="form-control form-control-sm" placeholder="Task title" required></div>
                    <div class="col-2"><select name="input_type" class="form-select form-select-sm"><option value="checkbox">Checkbox</option><option value="text">Text</option><option value="number">Number</option><option value="options">Options</option><option value="photo">Photo</option></select></div>
                    <div class="col-2"><textarea name="description" class="form-control form-control-sm" placeholder="Desc" rows="1"></textarea></div>
                    <div class="col-2"><button type="submit" class="btn btn-sm btn-outline-primary w-100">Add</button></div>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>
</div>