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

<?= $this->section('styles') ?>
<style>
.transport-ai-change-dot {
    display: inline-block;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: #dc3545;
    animation: transport-ai-pulse 1.2s ease-in-out infinite;
}
.transport-ai-change-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.transport-ai-change-badge .transport-ai-change-dot {
    background: #fff;
}
.transport-ai-change-row td {
    background: rgba(220, 53, 69, 0.06);
}
@keyframes transport-ai-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.35; }
}
</style>
<?= $this->endSection() ?>

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'planned'     => 'draft',
        'scheduled'   => 'default',
        'in_progress' => 'active',
        'completed'   => 'active',
        'cancelled'   => 'terminated',
    ];
    $class = $map[$status] ?? 'inactive';

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

$priorityPill = static function (string $priority): string {
    $map = ['low' => 'inactive', 'normal' => 'default', 'high' => 'on_leave', 'urgent' => 'terminated'];
    $class = $map[$priority] ?? 'default';

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

$hm = static fn (?string $t): string => $t ? substr($t, 0, 5) : '—';
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => 'Scheduling',
    'workspaceTitle'        => 'Trip Scheduling',
    'workspaceDesc'         => 'Plan, confirm, and track transport trips.',
    'workspaceAction'       => [
        'href'  => base_url('transport/scheduling/create'),
        'label' => 'Schedule Trip',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<?php if (! empty($unseenAiChanges)): ?>
    <div class="alert alert-danger d-flex align-items-center gap-2">
        <span class="transport-ai-change-dot"></span>
        <div>
            <strong><?= (int) $unseenAiChanges ?> driver schedule<?= (int) $unseenAiChanges === 1 ? ' was' : 's were' ?> added or changed</strong>
            by an approved AI transport plan. Changed rows are marked in red below; the marks disappear once you have seen them.
        </div>
    </div>
<?php endif; ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head d-flex flex-wrap align-items-start justify-content-between gap-2">
        <div>
            <h2 class="enterprise-data-panel__title">Scheduled Trips</h2>
            <p class="enterprise-data-panel__subtitle">Filter by date range, status, or search term.</p>
        </div>
        <a href="<?= base_url('transport/scheduling/export') ?>" class="btn btn-sm btn-outline-secondary">
            <i class="fa-solid fa-file-export me-1"></i>Export Driver Schedules
        </a>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('transport/scheduling') ?>" 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, title, route, vehicle"
                           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 (['planned', 'scheduled', 'in_progress', 'completed', 'cancelled'] as $s): ?>
                            <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>>
                                <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">From</label>
                    <input type="date" name="date_from" class="form-control form-control-sm"
                           value="<?= esc($filters['dateFrom'] ?? '') ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">To</label>
                    <input type="date" name="date_to" class="form-control form-control-sm"
                           value="<?= esc($filters['dateTo'] ?? '') ?>">
                </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>Date</th>
                        <th>Depart</th>
                        <th>Arrive</th>
                        <th>Return</th>
                        <th>Back</th>
                        <th>Route</th>
                        <th>Vehicle</th>
                        <th>Driver</th>
                        <th>Priority</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entries as $e): ?>
                        <?php $aiChanged = ! empty($e['ai_flag']) && empty($e['ai_flag_seen_at']); ?>
                        <tr<?= $aiChanged ? ' class="transport-ai-change-row"' : '' ?>>
                            <td><code><?= esc($e['number']) ?></code></td>
                            <td>
                                <a href="<?= base_url('transport/scheduling/' . $e['id']) ?>" class="enterprise-data-table__primary">
                                    <?= esc($e['title']) ?>
                                </a>
                                <?php if ($aiChanged): ?>
                                    <span class="badge bg-danger ms-1 transport-ai-change-badge"
                                          title="This driver schedule was <?= $e['ai_flag'] === 'updated' ? 'changed' : 'added' ?> by an approved AI plan">
                                        <span class="transport-ai-change-dot"></span>
                                        <?= $e['ai_flag'] === 'updated' ? 'Changed' : 'New' ?>
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td><?= esc($e['schedule_date']) ?></td>
                            <td><?= $hm($e['departure_time'] ?? null) ?></td>
                            <td><?= $hm($e['arrival_time'] ?? null) ?></td>
                            <td><?= $hm($e['return_departure_time'] ?? null) ?></td>
                            <td><?= $hm($e['return_arrival_time'] ?? null) ?></td>
                            <td><?= esc($e['route_name'] ?? '—') ?></td>
                            <td><?= esc($e['vehicle_registration'] ?? '—') ?></td>
                            <td><?= esc($e['driver_name'] ?? '—') ?></td>
                            <td><?= $priorityPill($e['priority'] ?? 'normal') ?></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/scheduling/' . $e['id']) ?>"><i class="fa-solid fa-eye me-2"></i>View</a></li>
                                        <?php if (in_array($e['status'], ['planned', 'scheduled'], true)): ?>
                                            <li><a class="dropdown-item" href="<?= base_url('transport/scheduling/' . $e['id'] . '/edit') ?>"><i class="fa-solid fa-pen me-2"></i>Edit</a></li>
                                        <?php endif; ?>
                                        <?php if ($e['status'] === 'planned'): ?>
                                            <li>
                                                <form action="<?= base_url('transport/scheduling/' . $e['id'] . '/confirm') ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item"><i class="fa-solid fa-check me-2"></i>Confirm</button>
                                                </form>
                                            </li>
                                        <?php endif; ?>
                                        <?php if ($e['status'] === 'scheduled'): ?>
                                            <li>
                                                <form action="<?= base_url('transport/scheduling/' . $e['id'] . '/start') ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item"><i class="fa-solid fa-play me-2"></i>Start</button>
                                                </form>
                                            </li>
                                        <?php endif; ?>
                                        <?php if ($e['status'] === 'in_progress'): ?>
                                            <li>
                                                <form action="<?= base_url('transport/scheduling/' . $e['id'] . '/complete') ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item"><i class="fa-solid fa-flag-checkered me-2"></i>Complete</button>
                                                </form>
                                            </li>
                                        <?php endif; ?>
                                        <?php if (in_array($e['status'], ['planned', 'scheduled', 'in_progress'], true)): ?>
                                            <li>
                                                <form action="<?= base_url('transport/scheduling/' . $e['id'] . '/cancel') ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item"><i class="fa-solid fa-ban me-2"></i>Cancel</button>
                                                </form>
                                            </li>
                                        <?php endif; ?>
                                        <?php if (in_array($e['status'], ['planned', 'cancelled'], true)): ?>
                                            <li><hr class="dropdown-divider"></li>
                                            <li>
                                                <form action="<?= base_url('transport/scheduling/' . $e['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this schedule?');">
                                                    <?= 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="10" class="text-center text-muted py-5">No trips scheduled 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() ?>
