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

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

<?php
$statusPill = static function (string $status): string {
    $map = ['active' => 'active', 'inactive' => 'inactive'];
    $class = $map[$status] ?? 'inactive';

    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">'
        . esc(ucfirst($status)) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $checklist['number'],
    'workspaceTitle'        => esc($checklist['number']),
    'workspaceDesc'         => esc($checklist['name']),
]) ?>

<?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-8">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-list-check"></i>
                <h2 class="enterprise-settings-card__title">Checklist Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Number</dt>
                <dd class="col-sm-8 fw-semibold"><?= esc($checklist['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Name</dt>
                <dd class="col-sm-8"><?= esc($checklist['name']) ?></dd>
                <dt class="col-sm-4 text-muted">Code</dt>
                <dd class="col-sm-8"><?= esc($checklist['code'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($checklist['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8"><?= nl2br(esc($checklist['description'] ?: '—')) ?></dd>
            </dl>
        </article>

        <div class="enterprise-data-panel mt-4">
            <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>Required</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($items as $row): ?>
                                <tr>
                                    <td><span class="enterprise-data-table__primary"><?= esc($row['title']) ?></span></td>
                                    <td><?= ! empty($row['is_required']) ? 'Yes' : 'Optional' ?></td>
                                    <td class="text-end">
                                        <form action="<?= base_url('operations/quality-control/audit-checklists/items/' . $row['id'] . '/toggle') ?>" method="post" class="d-inline">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-sm btn-outline-primary">
                                                <?= ! empty($row['is_required']) ? 'Make Optional' : 'Make Required' ?>
                                            </button>
                                        </form>
                                        <form action="<?= base_url('operations/quality-control/audit-checklists/items/' . $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 class="col-lg-4">
        <article class="enterprise-settings-card mb-4">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-square-check"></i>
                <h2 class="enterprise-settings-card__title">Add Item</h2>
            </div>
            <form action="<?= base_url('operations/quality-control/audit-checklists/items') ?>" method="post" novalidate>
                <?= csrf_field() ?>
                <input type="hidden" name="checklist_id" value="<?= (int) $checklist['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 PPE on site">
                    </div>
                    <div class="enterprise-field enterprise-field--full">
                        <div class="form-check">
                            <input type="checkbox" name="is_required" value="1" class="form-check-input" id="is_required" checked>
                            <label class="form-check-label" for="is_required">Required</label>
                        </div>
                    </div>
                </div>
                <div class="enterprise-settings-card__footer">
                    <button type="submit" class="btn btn-primary">Add Item</button>
                </div>
            </form>
        </article>

        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-bolt"></i>
                <h2 class="enterprise-settings-card__title">Actions</h2>
            </div>
            <div class="d-grid gap-2">
                <a href="<?= base_url('operations/quality-control/audit-checklists/' . $checklist['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                    <i class="fa-solid fa-pen me-1"></i> Edit
                </a>
                <form action="<?= base_url('operations/quality-control/audit-checklists/' . $checklist['id'] . '/delete') ?>" method="post"
                      onsubmit="return confirm('Delete this checklist and its items permanently?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                        <i class="fa-solid fa-trash me-1"></i> Delete
                    </button>
                </form>
                <a href="<?= base_url('operations/quality-control/audit-checklists') ?>" class="btn btn-outline-secondary btn-sm">Back to Checklists</a>
            </div>
        </article>
    </div>
</div>

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