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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => 'Pickup Points',
    'workspaceTitle'        => 'Employee Pickup Points',
    'workspaceDesc'         => 'Optional legacy geo points. The AI planner no longer uses these — it plans from contract sites, assigned staff, and shift timing.',
    'workspaceAction'       => [
        'href'  => base_url('transport/ai/pickups/create'),
        'label' => 'Add Pickup Point',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<?php if ($missing !== []): ?>
    <div class="alert alert-warning">
        <i class="fa-solid fa-triangle-exclamation me-1"></i>
        <strong><?= count($missing) ?><?= count($missing) === 50 ? '+' : '' ?> active employees</strong> have no pickup point yet
        (e.g. <?= esc(implode(', ', array_slice(array_column($missing, 'full_name'), 0, 5))) ?><?= count($missing) > 5 ? ', …' : '' ?>).
        They will be excluded from AI route optimization.
    </div>
<?php endif; ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Pickup Points</h2>
        <p class="enterprise-data-panel__subtitle">One pickup location per employee.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('transport/ai/pickups') ?>" 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="Employee name, number, or address"
                           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>Employee</th>
                        <th>Number</th>
                        <th>Address</th>
                        <th>Coordinates</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['employee_name'] ?? '—') ?></td>
                            <td><code><?= esc($e['employee_number'] ?? '—') ?></code></td>
                            <td><?= esc($e['address'] ?? '—') ?></td>
                            <td class="small text-muted"><?= esc($e['latitude']) ?>, <?= esc($e['longitude']) ?></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/pickups/' . $e['id'] . '/edit') ?>" class="btn btn-sm btn-outline-secondary">
                                    <i class="fa-solid fa-pen"></i>
                                </a>
                                <form action="<?= base_url('transport/ai/pickups/' . $e['id'] . '/delete') ?>" method="post" class="d-inline"
                                      onsubmit="return confirm('Delete this pickup point?');">
                                    <?= 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="6" class="text-center text-muted py-5">No pickup points yet.</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() ?>
