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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => 'Assign Staff',
    'workspaceTitle'        => 'Assign Staff',
    'workspaceDesc'         => 'Assign employees and crews to open work orders.',
]) ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Select Work Order</h2>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('operations/work-orders/assign-staff') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div style="min-width:280px;flex:1;">
                    <label class="enterprise-field-label">Work Order</label>
                    <select name="work_order_id" class="form-select form-select-sm" onchange="this.form.submit()">
                        <option value="">Select a work order</option>
                        <?php foreach ($workOrders as $wo): ?>
                            <option value="<?= (int) $wo['id'] ?>" <?= (string) ($workOrderId ?? '') === (string) $wo['id'] ? 'selected' : '' ?>>
                                <?= esc($wo['number']) ?> — <?= esc($wo['title']) ?> (<?= esc(ucwords(str_replace('_', ' ', $wo['status']))) ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </form>
    </div>
</div>

<?php if ($order): ?>
    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <div class="row g-4">
        <div class="col-lg-5">
            <article class="enterprise-settings-card">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-user-plus"></i>
                    <h2 class="enterprise-settings-card__title">Add Staff</h2>
                </div>
                <p class="small text-muted mb-3">
                    Assigning to <a href="<?= base_url('operations/work-orders/' . $order['id']) ?>"><?= esc($order['number']) ?></a>
                    — <?= esc($order['title']) ?>
                </p>
                <form action="<?= base_url('operations/work-orders/assign-staff') ?>" method="post" novalidate>
                    <?= csrf_field() ?>
                    <input type="hidden" name="work_order_id" value="<?= (int) $order['id'] ?>">
                    <div class="enterprise-settings-fields">
                        <div class="enterprise-field enterprise-field--full">
                            <label class="enterprise-field-label">Employee <span class="text-danger">*</span></label>
                            <select name="employee_id" class="form-select" required>
                                <option value="">Select employee</option>
                                <?php foreach ($employees as $e): ?>
                                    <option value="<?= (int) $e['id'] ?>" <?= (int) old('employee_id') === (int) $e['id'] ? 'selected' : '' ?>>
                                        <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="enterprise-field enterprise-field--full">
                            <label class="enterprise-field-label">Role on job</label>
                            <input type="text" name="role" class="form-control" maxlength="100"
                                   value="<?= esc(old('role')) ?>" placeholder="e.g. Lead technician">
                        </div>
                        <div class="enterprise-field enterprise-field--full">
                            <label class="enterprise-field-label">Notes</label>
                            <input type="text" name="notes" class="form-control" maxlength="500" value="<?= esc(old('notes')) ?>">
                        </div>
                    </div>
                    <div class="enterprise-settings-card__footer">
                        <button type="submit" class="btn btn-primary">Assign Staff</button>
                    </div>
                </form>
            </article>
        </div>
        <div class="col-lg-7">
            <div class="enterprise-data-panel">
                <div class="enterprise-data-panel__head">
                    <h2 class="enterprise-data-panel__title">Current Assignments</h2>
                </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 class="text-end">Actions</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-end">
                                            <form action="<?= base_url('operations/work-orders/staff/' . $row['id'] . '/remove') ?>" method="post"
                                                  onsubmit="return confirm('Remove this staff assignment?');">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="btn btn-sm btn-outline-danger">Remove</button>
                                            </form>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                                <?php if (count($staff) === 0): ?>
                                    <tr><td colspan="4" class="text-center text-muted py-4">No staff assigned yet.</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php else: ?>
    <div class="enterprise-data-panel">
        <div class="enterprise-data-panel__body text-center py-5">
            <i class="fa-solid fa-user-plus fa-3x text-muted mb-3"></i>
            <p class="text-muted mb-0">Select an open work order to assign staff.</p>
        </div>
    </div>
<?php endif; ?>

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