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

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

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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => 'AI Transport Plans',
    'workspaceTitle'        => 'AI Transport Plans',
    'workspaceDesc'         => 'AI-optimized routes and driver schedules. Plans stay as proposals until a manager approves them.',
    'workspaceAction'       => [
        'href'  => base_url('transport/ai/plans/generate'),
        'label' => 'Generate Plan',
        'icon'  => 'fa-solid fa-wand-magic-sparkles',
    ],
]) ?>

<div class="row g-3 mb-4">
    <?php
    $cards = [
        ['label' => 'Draft', 'key' => 'draft', 'icon' => 'fa-pen-ruler'],
        ['label' => 'Pending Approval', 'key' => 'pending_approval', 'icon' => 'fa-hourglass-half'],
        ['label' => 'Approved', 'key' => 'approved', 'icon' => 'fa-circle-check'],
        ['label' => 'Rejected', 'key' => 'rejected', 'icon' => 'fa-circle-xmark'],
    ];
    ?>
    <?php foreach ($cards as $card): ?>
        <div class="col-md-3">
            <article class="enterprise-kpi-card">
                <div class="enterprise-kpi-header">
                    <span class="enterprise-kpi-label"><?= esc($card['label']) ?></span>
                    <span class="enterprise-kpi-icon"><i class="fa-solid <?= esc($card['icon']) ?>"></i></span>
                </div>
                <div class="enterprise-kpi-value"><?= (int) ($statusCounts[$card['key']] ?? 0) ?></div>
            </article>
        </div>
    <?php endforeach; ?>
</div>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Plans</h2>
        <p class="enterprise-data-panel__subtitle">Every plan requires manager review and approval before its schedules become official.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('transport/ai/plans') ?>" 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="Number or title"
                           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 (['draft', 'pending_approval', 'approved', 'rejected'] as $s): ?>
                            <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>>
                                <?= esc(ucwords(str_replace('_', ' ', $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>Number</th>
                        <th>Title</th>
                        <th>Period</th>
                        <th class="text-end">Routes</th>
                        <th class="text-end">Employees</th>
                        <th class="text-end">Distance (km)</th>
                        <th class="text-end">Est. Cost</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entries as $e): ?>
                        <tr>
                            <td><code><?= esc($e['number']) ?></code></td>
                            <td>
                                <a href="<?= base_url('transport/ai/plans/' . $e['id']) ?>" class="enterprise-data-table__primary">
                                    <?= esc($e['title']) ?>
                                </a>
                            </td>
                            <td><?= esc($e['plan_start_date']) ?> → <?= esc($e['plan_end_date']) ?></td>
                            <td class="text-end"><?= (int) $e['total_routes'] ?></td>
                            <td class="text-end"><?= (int) $e['total_employees'] ?></td>
                            <td class="text-end"><?= number_format((float) $e['total_distance_km'], 1) ?></td>
                            <td class="text-end"><?= number_format((float) $e['total_cost'], 3) ?></td>
                            <td><?= $statusPill($e['status']) ?></td>
                            <td class="text-end enterprise-row-actions">
                                <div class="dropdown">
                                    <button class="btn btn-sm btn-outline-secondary" data-bs-toggle="dropdown"><i class="fa-solid fa-ellipsis"></i></button>
                                    <ul class="dropdown-menu dropdown-menu-end enterprise-nav-menu">
                                        <li><a class="dropdown-item" href="<?= base_url('transport/ai/plans/' . $e['id']) ?>"><i class="fa-solid fa-eye me-2"></i>Review</a></li>
                                        <?php if (in_array($e['status'], ['draft', 'rejected'], true)): ?>
                                            <li><hr class="dropdown-divider"></li>
                                            <li>
                                                <form action="<?= base_url('transport/ai/plans/' . $e['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this plan?');">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item text-danger"><i class="fa-solid fa-trash me-2"></i>Delete</button>
                                                </form>
                                            </li>
                                        <?php endif; ?>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($entries) === 0): ?>
                        <tr><td colspan="9" class="text-center text-muted py-5">No AI plans yet. Generate one to get optimized routes and schedules.</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() ?>
