<?= $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',
        'pending_approval' => 'on_leave',
        'rejected'         => 'terminated',
        '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>';
};

$trackStatuses = ['draft', 'open', 'in_progress', 'on_hold', 'pending_approval', 'rejected', 'completed', 'cancelled'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => 'Status Tracking',
    'workspaceTitle'        => 'Status Tracking',
    'workspaceDesc'         => 'Track work order progress from open through completion.',
    'workspaceAction'       => [
        'href'  => base_url('operations/work-orders/create'),
        'label' => 'Create Work Order',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Open Work Orders</h2>
        <p class="enterprise-data-panel__subtitle">Update status and notes for active work orders.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('operations/work-orders/status-tracking') ?>" 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, location"
                           value="<?= esc($filters['search'] ?? '') ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Open only</option>
                        <?php foreach (['draft', 'open', 'in_progress', 'on_hold', 'pending_approval', 'rejected', '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">Priority</label>
                    <select name="priority" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['low', 'normal', 'high', 'urgent'] as $p): ?>
                            <option value="<?= $p ?>" <?= ($filters['priority'] ?? '') === $p ? 'selected' : '' ?>>
                                <?= esc(ucfirst($p)) ?>
                            </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>Priority</th>
                        <th>Due</th>
                        <th>Current</th>
                        <th>Update Status</th>
                        <th class="text-end">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orders as $o): ?>
                        <tr>
                            <td>
                                <a href="<?= base_url('operations/work-orders/' . $o['id']) ?>" class="enterprise-data-table__primary">
                                    <code><?= esc($o['number']) ?></code>
                                </a>
                            </td>
                            <td>
                                <?= esc($o['title']) ?>
                                <?php if (! empty($o['location'])): ?>
                                    <div class="text-muted small"><?= esc($o['location']) ?></div>
                                <?php endif; ?>
                            </td>
                            <td><?= esc(ucfirst($o['priority'])) ?></td>
                            <td><?= esc($o['due_date'] ?: '—') ?></td>
                            <td><?= $statusPill($o['status']) ?></td>
                            <?php if (in_array($o['status'], ['draft', 'open', 'in_progress', 'on_hold'], true)): ?>
                                <td colspan="2" class="p-2">
                                    <form action="<?= base_url('operations/work-orders/' . $o['id'] . '/status') ?>" method="post"
                                          class="d-flex flex-wrap gap-2 align-items-center justify-content-end">
                                        <?= csrf_field() ?>
                                        <select name="status" class="form-select form-select-sm" style="min-width:140px;" required>
                                            <?php foreach ($trackStatuses as $s): ?>
                                                <option value="<?= $s ?>" <?= $o['status'] === $s ? 'selected' : '' ?>>
                                                    <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                        <input type="text" name="notes" class="form-control form-control-sm" style="min-width:160px;flex:1;"
                                               placeholder="Notes" value="<?= esc($o['notes'] ?? '') ?>">
                                        <button type="submit" class="btn btn-sm btn-primary">Update</button>
                                    </form>
                                </td>
                            <?php elseif ($o['status'] === 'pending_approval'): ?>
                                <td colspan="2" class="text-muted small text-end">Awaiting approval</td>
                            <?php elseif ($o['status'] === 'rejected'): ?>
                                <td colspan="2" class="text-muted small text-end">Rejected</td>
                            <?php else: ?>
                                <td colspan="2" class="text-muted small text-end">Closed</td>
                            <?php endif; ?>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($orders) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No work orders found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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