<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-clipboard-list me-2 text-primary"></i>Start: <?= e($template['name']) ?></h5>
    <a href="<?= url('/Checklists') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<?php if (!empty($existingWo)): ?>
    <!-- ── Existing in-progress checklist prompt ────────────────────── -->
    <div class="rs-card p-4 mb-3 text-center">
        <i class="fas fa-clipboard-check text-warning" style="font-size:3rem;"></i>
        <h5 class="fw-bold mt-3">Checklist already in progress</h5>
        <p class="text-muted mb-1">
            You have an open checklist <strong>"<?= e($existingWo['title']) ?>"</strong><br>
            that was started on <strong><?= e(substr((string)($existingWo['created_at'] ?? ''), 0, 16)) ?></strong>
            with status <span class="badge bg-secondary text-uppercase"><?= e($existingWo['status']) ?></span>.
        </p>
        <p class="text-muted small">What would you like to do?</p>
        <div class="d-flex justify-content-center gap-2 mt-3">
            <a href="<?= e($existingWoUrl) ?>" class="btn btn-primary">
                <i class="fas fa-forward me-1"></i>Continue checklist
            </a>
            <form method="post" action="<?= url('/WorkOrders/delete/' . (int) $existingWo['work_order_id']) ?>" class="rs-confirm-submit" data-confirm-title="Discard this checklist?" data-confirm-body="This will permanently delete the in-progress checklist and all its responses. You can then start a fresh one." data-confirm-ok="Discard" data-confirm-danger="1">
                <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                <button type="submit" class="btn btn-outline-danger"><i class="fas fa-trash me-1"></i>Delete & start fresh</button>
            </form>
            <a href="<?= url('/Checklists') ?>" class="btn btn-outline-secondary"><i class="fas fa-times me-1"></i>Cancel</a>
        </div>
    </div>

    <!-- Still show the tasks list so the user knows what's in it -->
    <div class="row g-3">
        <div class="col-12">
            <div class="rs-card p-3">
                <h6 class="fw-bold mb-2">Items in this checklist (<?= count($tasks) ?>)</h6>
                <?php if (empty($tasks)): ?>
                    <p class="text-muted small mb-0">No items defined.</p>
                <?php else: ?>
                    <ol class="small mb-0" style="padding-left: 1.25rem;">
                        <?php foreach ($tasks as $t): ?>
                            <li class="mb-1">
                                <?= e($t['title']) ?>
                                <?php if (!empty($t['is_required'])): ?><span class="text-danger">*</span><?php endif; ?>
                                <span class="badge bg-secondary text-uppercase" style="font-size:.55rem;"><?= e($t['input_type']) ?></span>
                            </li>
                        <?php endforeach; ?>
                    </ol>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php else: ?>
    <!-- ── Normal start flow ────────────────────────────────────────── -->
    <div class="row g-3">
        <div class="col-md-7">
            <div class="rs-card p-3">
                <h6 class="fw-bold mb-2">Details</h6>

                <form method="post" action="<?= url('/Checklists/create') ?>">
                    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                    <input type="hidden" name="template_id" value="<?= (int) $template['template_id'] ?>">

                    <?php if ($template['asset_type_id']): ?>
                        <div class="mb-3">
                            <label class="form-label">Asset (<?= e($assetType['name'] ?? 'required') ?>) *</label>
                            <?php if (empty($assets)): ?>
                                <div class="alert alert-warning py-2 small mb-0">
                                    No <?= e($assetType['name'] ?? 'assets') ?> exist yet.
                                    <a href="<?= url('/Assets/create?type=' . (int) $template['asset_type_id']) ?>" class="alert-link">Create one first</a>.
                                </div>
                            <?php else: ?>
                                <select name="asset_id" class="form-select form-select-sm" required>
                                    <option value="">— pick asset —</option>
                                    <?php foreach ($assets as $a): ?>
                                        <option value="<?= (int) $a['asset_id'] ?>"><?= e($a['name']) ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <div class="form-text small">This checklist is scoped to <strong><?= e($assetType['name']) ?></strong> assets.</div>
                            <?php endif; ?>
                        </div>
                    <?php else: ?>
                        <div class="mb-3">
                            <label class="form-label">Asset (optional)</label>
                            <select name="asset_id" class="form-select form-select-sm">
                                <option value="">— general / no specific asset —</option>
                                <?php foreach ($assets as $a): ?>
                                    <option value="<?= (int) $a['asset_id'] ?>"><?= e($a['name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                            <div class="form-text small">This is a general checklist — attaching an asset is optional.</div>
                        </div>
                    <?php endif; ?>

                    <div class="mb-3">
                        <label class="form-label">Notes (optional)</label>
                        <textarea name="notes" rows="2" class="form-control form-control-sm" placeholder="e.g. Reason for running early / observations"></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Assign to</label>
                        <select name="assigned_to" class="form-select form-select-sm">
                            <?php $currentUserId = (int) (\Core\Session::user()['user_id'] ?? 0); ?>
                            <?php foreach ($users as $u): ?>
                                <option value="<?= (int) $u['user_id'] ?>" <?= (int) ($u['user_id'] ?? 0) === $currentUserId ? 'selected' : '' ?>>
                                    <?= e($u['username']) ?> — <?= e($u['f_name'] ?? '') ?> <?= e($u['l_name'] ?? '') ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-sm btn-primary" <?= ($template['asset_type_id'] && empty($assets)) ? 'disabled' : '' ?>>
                            <i class="fas fa-play me-1"></i>Create Work Order & start filling
                        </button>
                        <a href="<?= url('/Checklists') ?>" class="btn btn-sm btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>

        <div class="col-md-5">
            <div class="rs-card p-3">
                <h6 class="fw-bold mb-2">Items you'll fill in (<?= count($tasks) ?>)</h6>
                <?php if (empty($tasks)): ?>
                    <p class="text-muted small mb-0">No items defined. Add some in <a href="<?= url('/PmTemplates/show/' . (int) $template['template_id']) ?>">the template</a>.</p>
                <?php else: ?>
                    <ol class="small mb-0" style="padding-left: 1.25rem;">
                        <?php foreach ($tasks as $t): ?>
                            <li class="mb-1">
                                <?= e($t['title']) ?>
                                <?php if (!empty($t['is_required'])): ?><span class="text-danger">*</span><?php endif; ?>
                                <span class="badge bg-secondary text-uppercase" style="font-size:.55rem;"><?= e($t['input_type']) ?></span>
                            </li>
                        <?php endforeach; ?>
                    </ol>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php endif; ?>