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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'draft'       => 'draft',
        'open'        => 'default',
        'in_progress' => 'active',
        'on_hold'     => 'on_leave',
        '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>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => 'Dashboard',
    'workspaceTitle'        => 'Operations Overview',
    'workspaceDesc'         => 'Work orders, staff and equipment assignment, status tracking, and completion reports.',
    'workspaceAction'       => [
        'href'  => base_url('operations/work-orders/create'),
        'label' => 'Create Work Order',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<section class="enterprise-kpi-grid mb-4">
    <?php foreach ($kpis as $kpi): ?>
        <article class="enterprise-kpi-card">
            <div class="enterprise-kpi-header">
                <span class="enterprise-kpi-label"><?= esc($kpi['label']) ?></span>
                <span class="enterprise-kpi-icon"><i class="fa-solid <?= esc($kpi['icon']) ?>"></i></span>
            </div>
            <div class="enterprise-kpi-value"><?= esc($kpi['value']) ?></div>
            <div class="enterprise-kpi-footer">
                <span class="enterprise-kpi-badge enterprise-kpi-badge--<?= esc($kpi['badgeType']) ?>">
                    <?= esc($kpi['badge']) ?>
                </span>
                <span class="enterprise-kpi-dept"><?= esc($kpi['dept']) ?></span>
            </div>
        </article>
    <?php endforeach; ?>
</section>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Recent Work Orders</h2>
        <a href="<?= base_url('operations/work-orders/status-tracking') ?>" class="btn btn-sm btn-outline-primary">Status Tracking</a>
    </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>Priority</th>
                        <th>Due</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recent as $row): ?>
                        <tr>
                            <td>
                                <a href="<?= base_url('operations/work-orders/' . $row['id']) ?>" class="enterprise-data-table__primary">
                                    <code><?= esc($row['number']) ?></code>
                                </a>
                            </td>
                            <td><?= esc($row['title']) ?></td>
                            <td><?= esc(ucfirst($row['priority'])) ?></td>
                            <td><?= esc($row['due_date'] ?: '—') ?></td>
                            <td><?= $statusPill($row['status']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($recent) === 0): ?>
                        <tr><td colspan="5" class="text-center text-muted py-5">No work orders yet. Create one to get started.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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