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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'assigned' => 'active',
        'returned' => 'inactive',
        'lost'     => 'terminated',
    ];
    $class = $map[$status] ?? 'inactive';

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

$isAssigned = $row['status'] === 'assigned';
$canDelete  = in_array($row['status'], ['assigned', 'returned'], true);
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $row['number'],
    'workspaceTitle'        => esc($row['number']),
    'workspaceDesc'         => esc($row['asset_name'] ?? 'Equipment 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-screwdriver-wrench"></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">Asset</dt>
                <dd class="col-sm-8"><?= esc($row['asset_name'] ?? '—') ?> <code><?= esc($row['asset_tag'] ?? '') ?></code></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">Assigned At</dt>
                <dd class="col-sm-8"><?= esc($row['assigned_at'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Returned At</dt>
                <dd class="col-sm-8"><?= esc($row['returned_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 ($isAssigned): ?>
                    <a href="<?= base_url('operations/resources/equipment-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/equipment-allocation/' . $row['id'] . '/return') ?>" method="post"
                          onsubmit="return confirm('Mark this equipment as returned?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="fa-solid fa-rotate-left me-1"></i> Return
                        </button>
                    </form>
                    <form action="<?= base_url('operations/resources/equipment-allocation/' . $row['id'] . '/lost') ?>" method="post"
                          onsubmit="return confirm('Mark this equipment as lost?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-triangle-exclamation me-1"></i> Mark Lost
                        </button>
                    </form>
                <?php endif; ?>
                <?php if ($canDelete): ?>
                    <form action="<?= base_url('operations/resources/equipment-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/equipment-allocation') ?>" class="btn btn-outline-secondary btn-sm">Back to List</a>
            </div>
        </article>
    </div>
</div>

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