<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => 'Checklists',
    'workspaceTitle'        => 'Checklists',
    'workspaceDesc'         => 'Add and track checklist items on open tasks.',
]) ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Select Task</h2>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('operations/tasks/checklists') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div style="min-width:280px;flex:1;">
                    <label class="enterprise-field-label">Task</label>
                    <select name="task_id" class="form-select form-select-sm" onchange="this.form.submit()">
                        <option value="">Select a task</option>
                        <?php foreach ($openTasks as $t): ?>
                            <option value="<?= (int) $t['id'] ?>" <?= (string) ($taskId ?? '') === (string) $t['id'] ? 'selected' : '' ?>>
                                <?= esc($t['number']) ?> — <?= esc($t['title']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </form>
    </div>
</div>

<?php if ($task): ?>
    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <div class="row g-4">
        <div class="col-lg-5">
            <article class="enterprise-settings-card">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-square-check"></i>
                    <h2 class="enterprise-settings-card__title">Add Item</h2>
                </div>
                <p class="small text-muted mb-3">
                    Checklist for <a href="<?= base_url('operations/tasks/' . $task['id']) ?>"><?= esc($task['number']) ?></a>
                    — <?= esc($task['title']) ?>
                </p>
                <form action="<?= base_url('operations/tasks/checklists') ?>" method="post" novalidate>
                    <?= csrf_field() ?>
                    <input type="hidden" name="task_id" value="<?= (int) $task['id'] ?>">
                    <div class="enterprise-settings-fields">
                        <div class="enterprise-field enterprise-field--full">
                            <label class="enterprise-field-label">Item <span class="text-danger">*</span></label>
                            <input type="text" name="title" class="form-control" maxlength="255" required
                                   value="<?= esc(old('title')) ?>" placeholder="e.g. Verify spare parts on site">
                        </div>
                    </div>
                    <div class="enterprise-settings-card__footer">
                        <button type="submit" class="btn btn-primary">Add Item</button>
                    </div>
                </form>
            </article>
        </div>
        <div class="col-lg-7">
            <div class="enterprise-data-panel">
                <div class="enterprise-data-panel__head">
                    <h2 class="enterprise-data-panel__title">Checklist Items</h2>
                </div>
                <div class="enterprise-data-panel__body">
                    <div class="table-responsive">
                        <table class="enterprise-data-table">
                            <thead>
                                <tr>
                                    <th>Item</th>
                                    <th>Done</th>
                                    <th class="text-end">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($items as $row): ?>
                                    <tr>
                                        <td>
                                            <span class="<?= ! empty($row['is_done']) ? 'text-decoration-line-through text-muted' : 'enterprise-data-table__primary' ?>">
                                                <?= esc($row['title']) ?>
                                            </span>
                                        </td>
                                        <td><?= ! empty($row['is_done']) ? 'Yes' : 'No' ?></td>
                                        <td class="text-end">
                                            <form action="<?= base_url('operations/tasks/checklist/' . $row['id'] . '/toggle') ?>" method="post" class="d-inline">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="btn btn-sm btn-outline-primary">
                                                    <?= ! empty($row['is_done']) ? 'Undo' : 'Done' ?>
                                                </button>
                                            </form>
                                            <form action="<?= base_url('operations/tasks/checklist/' . $row['id'] . '/remove') ?>" method="post" class="d-inline"
                                                  onsubmit="return confirm('Remove this checklist item?');">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="btn btn-sm btn-outline-danger">Remove</button>
                                            </form>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                                <?php if (count($items) === 0): ?>
                                    <tr><td colspan="3" class="text-center text-muted py-4">No checklist items yet.</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php else: ?>
    <div class="enterprise-data-panel">
        <div class="enterprise-data-panel__body text-center py-5">
            <i class="fa-solid fa-square-check fa-3x text-muted mb-3"></i>
            <p class="text-muted mb-0">Select an open task to manage its checklist.</p>
        </div>
    </div>
<?php endif; ?>

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