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

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

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

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

$isPlanned = $row['status'] === 'planned';
$canRelease = in_array($row['status'], ['planned', 'active'], true);
$canDelete  = in_array($row['status'], ['planned', 'released'], true);
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $row['number'],
    'workspaceTitle'        => esc($row['number']),
    'workspaceDesc'         => esc($row['employee_name'] ?? 'Staff allocation'),
]) ?>

<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-users"></i>
                <h2 class="enterprise-settings-card__title">Allocation 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($row['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Employee</dt>
                <dd class="col-sm-8"><?= esc($row['employee_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Role</dt>
                <dd class="col-sm-8"><?= esc($row['role'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($row['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Work Order</dt>
                <dd class="col-sm-8"><?= esc($row['work_order_number'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Service Visit</dt>
                <dd class="col-sm-8"><?= esc($row['visit_number'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">From</dt>
                <dd class="col-sm-8"><?= esc($row['from_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">To</dt>
                <dd class="col-sm-8"><?= esc($row['to_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Assigned At</dt>
                <dd class="col-sm-8"><?= esc($row['assigned_at'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($row['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">
                <?php if ($isPlanned): ?>
                    <a href="<?= base_url('operations/resources/staff-allocation/' . $row['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fa-solid fa-pen me-1"></i> Edit
                    </a>
                    <form action="<?= base_url('operations/resources/staff-allocation/' . $row['id'] . '/activate') ?>" method="post"
                          onsubmit="return confirm('Activate this allocation?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="fa-solid fa-play me-1"></i> Activate
                        </button>
                    </form>
                <?php endif; ?>
                <?php if ($canRelease): ?>
                    <form action="<?= base_url('operations/resources/staff-allocation/' . $row['id'] . '/release') ?>" method="post"
                          onsubmit="return confirm('Release this allocation?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-user-check me-1"></i> Release
                        </button>
                    </form>
                <?php endif; ?>
                <?php if ($canDelete): ?>
                    <form action="<?= base_url('operations/resources/staff-allocation/' . $row['id'] . '/delete') ?>" method="post"
                          onsubmit="return confirm('Delete this allocation 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/resources/staff-allocation') ?>" class="btn btn-outline-secondary btn-sm">Back to List</a>
            </div>
        </article>
    </div>
</div>

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