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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => 'Contract Sites',
    'workspaceTitle'        => 'Contract Transport Sites',
    'workspaceDesc'         => 'Maps each contract to its physical work site (a transport location with coordinates) and a default shift. The AI planner routes employees to these destinations.',
    'workspaceAction'       => [
        'href'  => base_url('transport/ai/contract-sites/create'),
        'label' => 'Map Contract',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Contract Site Mappings</h2>
        <p class="enterprise-data-panel__subtitle">One destination site per contract.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('transport/ai/contract-sites') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm"
                           placeholder="Contract or site name"
                           value="<?= esc($filters['search'] ?? '') ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['active', 'inactive'] as $s): ?>
                            <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>>
                                <?= esc(ucfirst($s)) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Contract</th>
                        <th>Destination Site</th>
                        <th>Default Shift</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entries as $e): ?>
                        <tr>
                            <td class="enterprise-data-table__primary">
                                <?= esc($e['contract_title'] ?? '—') ?>
                                <div class="small text-muted"><code><?= esc($e['contract_number'] ?? '') ?></code></div>
                            </td>
                            <td><?= esc($e['location_name'] ?? '—') ?></td>
                            <td>
                                <?php if ($e['shift_name']): ?>
                                    <?= esc($e['shift_name']) ?>
                                    <span class="text-muted small">(<?= esc(substr((string) $e['shift_start_time'], 0, 5)) ?>–<?= esc(substr((string) $e['shift_end_time'], 0, 5)) ?>)</span>
                                <?php else: ?>
                                    <span class="text-muted">Per-employee shifts</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <span class="enterprise-status-pill enterprise-status-pill--<?= $e['status'] === 'active' ? 'active' : 'inactive' ?>">
                                    <?= esc(ucfirst($e['status'])) ?>
                                </span>
                            </td>
                            <td class="text-end enterprise-row-actions">
                                <a href="<?= base_url('transport/ai/contract-sites/' . $e['id'] . '/edit') ?>" class="btn btn-sm btn-outline-secondary">
                                    <i class="fa-solid fa-pen"></i>
                                </a>
                                <form action="<?= base_url('transport/ai/contract-sites/' . $e['id'] . '/delete') ?>" method="post" class="d-inline"
                                      onsubmit="return confirm('Delete this mapping?');">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fa-solid fa-trash"></i></button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($entries) === 0): ?>
                        <tr><td colspan="5" class="text-center text-muted py-5">No contract site mappings yet. The AI planner needs at least one.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php if ($pager): ?>
            <div class="enterprise-data-panel__footer"><?= $pager->links() ?></div>
        <?php endif; ?>
    </div>
</div>

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