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

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

<?php
$statusPill = static function (string $status): string {
    $label = ucwords(str_replace('_', ' ', $status));
    if ($status === 'posted') {
        $class = 'active';
    } elseif ($status === 'cancelled') {
        $class = 'inactive';
    } elseif ($status === 'draft') {
        $class = 'draft';
    } else {
        $class = 'default';
    }
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc($label) . '</span>';
};
$st = $disposal['status'];
$gainLoss = (float) $disposal['gain_loss'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Asset Disposal',
    'workspaceTitle'        => $disposal['number'],
    'workspaceDesc'         => ($disposal['asset_name'] ?? 'Disposal') . ' · ' . ($disposal['disposal_date'] ?? ''),
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-trash-can"></i>
        <h2 class="enterprise-settings-card__title">Disposal Summary</h2>
    </div>
    <div class="p-4">
        <dl class="row mb-0">
            <dt class="col-sm-3 text-muted">Number</dt>
            <dd class="col-sm-3"><code><?= esc($disposal['number']) ?></code></dd>
            <dt class="col-sm-3 text-muted">Status</dt>
            <dd class="col-sm-3"><?= $statusPill($st) ?></dd>
            <dt class="col-sm-3 text-muted">Asset</dt>
            <dd class="col-sm-3">
                <?php if (! empty($disposal['fixed_asset_id'])): ?>
                    <a href="<?= base_url('accounting/fixed-assets/' . $disposal['fixed_asset_id']) ?>">
                        <code><?= esc($disposal['asset_number'] ?? '') ?></code>
                    </a>
                    <?= esc($disposal['asset_name'] ?? '') ?>
                <?php else: ?>
                    —
                <?php endif; ?>
            </dd>
            <dt class="col-sm-3 text-muted">Disposal Date</dt>
            <dd class="col-sm-3"><?= esc($disposal['disposal_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Cost</dt>
            <dd class="col-sm-3"><?= number_format((float) $disposal['cost'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Accum. Depreciation</dt>
            <dd class="col-sm-3"><?= number_format((float) $disposal['accum_depreciation'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Proceeds</dt>
            <dd class="col-sm-3"><?= number_format((float) $disposal['proceeds'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Gain / Loss</dt>
            <dd class="col-sm-3">
                <?= number_format($gainLoss, 2) ?>
                <?php if ($gainLoss > 0): ?>
                    <span class="text-muted small">(gain)</span>
                <?php elseif ($gainLoss < 0): ?>
                    <span class="text-muted small">(loss)</span>
                <?php endif; ?>
            </dd>
            <dt class="col-sm-3 text-muted">Journal Entry</dt>
            <dd class="col-sm-3">
                <?php if (! empty($disposal['journal_entry_id'])): ?>
                    <a href="<?= base_url('accounting/journal-entries/' . $disposal['journal_entry_id']) ?>">#<?= (int) $disposal['journal_entry_id'] ?></a>
                <?php else: ?>
                    —
                <?php endif; ?>
            </dd>
            <dt class="col-sm-3 text-muted">Posted At</dt>
            <dd class="col-sm-3"><?= esc($disposal['posted_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($disposal['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/asset-disposals') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/asset-disposals/' . $disposal['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/asset-disposals/' . $disposal['id'] . '/post') ?>" method="post" class="d-inline" onsubmit="return confirm('Post this disposal?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Post Disposal</button>
            </form>
        <?php endif; ?>
    </div>
</article>

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