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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Edit Run',
    'workspaceTitle'        => 'Update Depreciation Run',
    'workspaceDesc'         => 'Change the period and recalculate asset lines.',
]) ?>

<article class="enterprise-settings-card mb-4" style="max-width:720px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-chart-line"></i>
        <h2 class="enterprise-settings-card__title">Run Details</h2>
    </div>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form action="<?= base_url('accounting/depreciation/' . $run['id']) ?>" method="post" novalidate>
        <?= csrf_field() ?>
        <?= $this->include('Modules\Accounting\Views\depreciation\_form') ?>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('accounting/depreciation/' . $run['id']) ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Recalculate &amp; Save</button>
        </div>
    </form>
</article>

<?php if (! empty($lines)): ?>
<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Current Lines</h2>
        <p class="enterprise-data-panel__subtitle">Will be replaced on save.</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><?= esc(($line['asset_number'] ?? '') . ' — ' . ($line['asset_name'] ?? '')) ?></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; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php endif; ?>

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