<?= $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>';
};

$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>';
};

$isOpen = in_array($order['status'], ['draft', 'open', 'in_progress', 'on_hold'], true);
$canEdit = $isOpen || $order['status'] === 'rejected';
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $order['number'],
    'workspaceTitle'        => esc($order['number']),
    'workspaceDesc'         => esc($order['title']),
]) ?>

<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-clipboard-list"></i>
                <h2 class="enterprise-settings-card__title">Work Order 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"><?= esc($order['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Title</dt>
                <dd class="col-sm-8"><?= esc($order['title']) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($order['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Priority</dt>
                <dd class="col-sm-8"><?= $priorityPill($order['priority']) ?></dd>
                <dt class="col-sm-4 text-muted">Scheduled</dt>
                <dd class="col-sm-8"><?= esc($order['scheduled_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Due</dt>
                <dd class="col-sm-8"><?= esc($order['due_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Location</dt>
                <dd class="col-sm-8"><?= esc($order['location'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8"><?= nl2br(esc($order['description'] ?: '—')) ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($order['notes'] ?: '—')) ?></dd>
                <?php if (! empty($order['rejection_reason'])): ?>
                    <dt class="col-sm-4 text-muted">Rejection Reason</dt>
                    <dd class="col-sm-8"><?= nl2br(esc($order['rejection_reason'])) ?></dd>
                <?php endif; ?>
                <?php if ($order['status'] === 'completed'): ?>
                    <dt class="col-sm-4 text-muted">Completed</dt>
                    <dd class="col-sm-8"><?= esc($order['completed_at'] ?: '—') ?></dd>
                    <dt class="col-sm-4 text-muted">Completion Notes</dt>
                    <dd class="col-sm-8"><?= nl2br(esc($order['completion_notes'] ?: '—')) ?></dd>
                <?php endif; ?>
            </dl>
        </article>

        <div class="enterprise-data-panel mt-4">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Assigned Staff</h2>
                <?php if ($isOpen): ?>
                    <a href="<?= base_url('operations/work-orders/assign-staff?work_order_id=' . $order['id']) ?>" class="btn btn-sm btn-outline-primary">Manage</a>
                <?php endif; ?>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Employee</th>
                                <th>Role</th>
                                <th>Assigned</th>
                                <th>Notes</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($staff as $row): ?>
                                <tr>
                                    <td>
                                        <span class="enterprise-data-table__primary"><?= esc($row['full_name'] ?? '—') ?></span>
                                        <div class="text-muted small"><?= esc($row['employee_number'] ?? '') ?></div>
                                    </td>
                                    <td><?= esc($row['role'] ?: '—') ?></td>
                                    <td><?= esc($row['assigned_at'] ?: '—') ?></td>
                                    <td class="text-muted small"><?= esc($row['notes'] ?: '—') ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($staff) === 0): ?>
                                <tr><td colspan="4" class="text-center text-muted py-4">No staff assigned.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="enterprise-data-panel mt-4">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Assigned Equipment</h2>
                <?php if ($isOpen): ?>
                    <a href="<?= base_url('operations/work-orders/assign-equipment?work_order_id=' . $order['id']) ?>" class="btn btn-sm btn-outline-primary">Manage</a>
                <?php endif; ?>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Asset</th>
                                <th>Tag</th>
                                <th>Assigned</th>
                                <th>Returned</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($equipment as $row): ?>
                                <tr>
                                    <td>
                                        <span class="enterprise-data-table__primary"><?= esc($row['asset_name'] ?? '—') ?></span>
                                        <div class="text-muted small"><?= esc($row['category'] ?? '') ?></div>
                                    </td>
                                    <td><code><?= esc($row['asset_tag'] ?? '—') ?></code></td>
                                    <td><?= esc($row['assigned_at'] ?: '—') ?></td>
                                    <td><?= esc($row['returned_at'] ?: 'Active') ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($equipment) === 0): ?>
                                <tr><td colspan="4" class="text-center text-muted py-4">No equipment assigned.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </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">
                <?php if (in_array($order['status'], ['draft', 'open', 'rejected'], true)): ?>
                    <form action="<?= base_url('operations/work-orders/' . $order['id'] . '/submit') ?>" method="post"
                          onsubmit="return confirm('Submit this work order for approval?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-primary btn-sm w-100">
                            <i class="fa-solid fa-paper-plane me-1"></i> Submit for Approval
                        </button>
                    </form>
                <?php endif; ?>
                <?php if ($canEdit): ?>
                    <a href="<?= base_url('operations/work-orders/' . $order['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fa-solid fa-pen me-1"></i> Edit
                    </a>
                <?php endif; ?>
                <?php if ($isOpen): ?>
                    <a href="<?= base_url('operations/work-orders/assign-staff?work_order_id=' . $order['id']) ?>" class="btn btn-outline-secondary btn-sm">
                        <i class="fa-solid fa-user-plus me-1"></i> Assign Staff
                    </a>
                    <a href="<?= base_url('operations/work-orders/assign-equipment?work_order_id=' . $order['id']) ?>" class="btn btn-outline-secondary btn-sm">
                        <i class="fa-solid fa-screwdriver-wrench me-1"></i> Assign Equipment
                    </a>
                    <form action="<?= base_url('operations/work-orders/' . $order['id'] . '/complete') ?>" method="post"
                          onsubmit="return confirm('Mark this work order as completed?');">
                        <?= csrf_field() ?>
                        <input type="text" name="completion_notes" class="form-control form-control-sm mb-2" placeholder="Completion notes (optional)">
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="fa-solid fa-check me-1"></i> Complete
                        </button>
                    </form>
                    <form action="<?= base_url('operations/work-orders/' . $order['id'] . '/cancel') ?>" method="post"
                          onsubmit="return confirm('Cancel this work order?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-ban me-1"></i> Cancel
                        </button>
                    </form>
                <?php endif; ?>
                <?php if (in_array($order['status'], ['draft', 'cancelled', 'rejected'], true)): ?>
                    <form action="<?= base_url('operations/work-orders/' . $order['id'] . '/delete') ?>" method="post"
                          onsubmit="return confirm('Delete this work order 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>
                <?php endif; ?>
                <a href="<?= base_url('operations/work-orders/status-tracking') ?>" class="btn btn-outline-secondary btn-sm">Back to Tracking</a>
            </div>
        </article>
    </div>
</div>

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