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

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

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

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

$licenseExpiring = ! empty($driver['license_expiry'])
    && strtotime($driver['license_expiry']) <= strtotime('+30 days');
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => $driver['employee_name'] ?? $driver['number'],
    'workspaceTitle'        => esc($driver['employee_name'] ?? $driver['number']),
    'workspaceDesc'         => 'Driver profile and license information.',
]) ?>

<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-id-card"></i>
                <h2 class="enterprise-settings-card__title">Driver 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"><code><?= esc($driver['number']) ?></code></dd>
                <dt class="col-sm-4 text-muted">Employee</dt>
                <dd class="col-sm-8"><?= esc($driver['employee_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Employee #</dt>
                <dd class="col-sm-8"><?= esc($driver['employee_number'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">License Number</dt>
                <dd class="col-sm-8"><?= esc($driver['license_number']) ?></dd>
                <dt class="col-sm-4 text-muted">License Class</dt>
                <dd class="col-sm-8"><?= esc($driver['license_class'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">License Expiry</dt>
                <dd class="col-sm-8">
                    <?= esc($driver['license_expiry'] ?: '—') ?>
                    <?php if ($licenseExpiring): ?>
                        <span class="enterprise-status-pill enterprise-status-pill--on_leave ms-1">Expiring soon</span>
                    <?php endif; ?>
                </dd>
                <dt class="col-sm-4 text-muted">Phone</dt>
                <dd class="col-sm-8"><?= esc($driver['phone'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($driver['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($driver['notes'] ?: '—')) ?></dd>
            </dl>
        </article>
    </div>

    <div class="col-lg-4">
        <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('transport/drivers/' . $driver['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                    <i class="fa-solid fa-pen me-1"></i> Edit
                </a>
                <form action="<?= base_url('transport/drivers/' . $driver['id'] . '/delete') ?>" method="post"
                      onsubmit="return confirm('Delete this driver 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('transport/drivers') ?>" class="btn btn-outline-secondary btn-sm">Back to Drivers</a>
            </div>
        </article>
    </div>
</div>

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