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

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

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

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

$isRecorded = $row['status'] === 'recorded';
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $row['number'],
    'workspaceTitle'        => esc($row['number']),
    'workspaceDesc'         => esc($row['product_name'] ?? 'Material usage'),
]) ?>

<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-cubes"></i>
                <h2 class="enterprise-settings-card__title">Usage 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">Product</dt>
                <dd class="col-sm-8"><?= esc($row['product_name'] ?? '—') ?> <span class="text-muted">(<?= esc($row['sku'] ?? '') ?>)</span></dd>
                <dt class="col-sm-4 text-muted">Quantity</dt>
                <dd class="col-sm-8"><?= esc($row['quantity']) ?><?= ! empty($row['unit_label']) ? ' ' . esc($row['unit_label']) : '' ?></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">Used By</dt>
                <dd class="col-sm-8"><?= esc($row['used_by_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Used At</dt>
                <dd class="col-sm-8"><?= esc($row['used_at'] ?: '—') ?></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">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 ($isRecorded): ?>
                    <a href="<?= base_url('operations/resources/material-usage/' . $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/material-usage/' . $row['id'] . '/void') ?>" method="post"
                          onsubmit="return confirm('Void this usage record?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-ban me-1"></i> Void
                        </button>
                    </form>
                <?php endif; ?>
                <form action="<?= base_url('operations/resources/material-usage/' . $row['id'] . '/delete') ?>" method="post"
                      onsubmit="return confirm('Delete this usage record 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>
                <a href="<?= base_url('operations/resources/material-usage') ?>" class="btn btn-outline-secondary btn-sm">Back to List</a>
            </div>
        </article>
    </div>
</div>

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