<?= $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 = $run['status'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Depreciation Run',
    'workspaceTitle'        => $run['number'],
    'workspaceDesc'         => $run['period_start'] . ' → ' . $run['period_end'],
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-chart-line"></i>
        <h2 class="enterprise-settings-card__title">Run 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($run['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">Period Start</dt>
            <dd class="col-sm-3"><?= esc($run['period_start']) ?></dd>
            <dt class="col-sm-3 text-muted">Period End</dt>
            <dd class="col-sm-3"><?= esc($run['period_end']) ?></dd>
            <dt class="col-sm-3 text-muted">Run Date</dt>
            <dd class="col-sm-3"><?= esc($run['run_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Total Amount</dt>
            <dd class="col-sm-3"><?= number_format((float) $run['total_amount'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Journal Entry</dt>
            <dd class="col-sm-3">
                <?php if (! empty($run['journal_entry_id'])): ?>
                    <a href="<?= base_url('accounting/journal-entries/' . $run['journal_entry_id']) ?>">#<?= (int) $run['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($run['posted_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($run['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/depreciation') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/depreciation/' . $run['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/depreciation/' . $run['id'] . '/post') ?>" method="post" class="d-inline" onsubmit="return confirm('Post this depreciation run?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Post Run</button>
            </form>
        <?php endif; ?>
    </div>
</article>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Lines</h2>
        <p class="enterprise-data-panel__subtitle">Total <?= number_format((float) $run['total_amount'], 2) ?></p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Asset</th>
                        <th class="text-end">Amount</th>
                        <th class="text-end">Book Value After</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($lines as $line): ?>
                        <tr>
                            <td>
                                <?php if (! empty($line['fixed_asset_id'])): ?>
                                    <a href="<?= base_url('accounting/fixed-assets/' . $line['fixed_asset_id']) ?>">
                                        <code><?= esc($line['asset_number'] ?? '') ?></code>
                                    </a>
                                    <?= esc($line['asset_name'] ?? '') ?>
                                <?php else: ?>
                                    —
                                <?php endif; ?>
                            </td>
                            <td class="text-end"><?= number_format((float) $line['amount'], 2) ?></td>
                            <td class="text-end"><?= number_format((float) $line['book_value_after'], 2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($lines) === 0): ?>
                        <tr><td colspan="3" class="text-center text-muted py-4">No depreciable amounts for this period.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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