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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'draft'     => 'draft',
        'submitted' => 'active',
        'filed'     => 'active',
        'cancelled' => 'inactive',
    ];
    $class = $map[$status] ?? 'default';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
$st = $return['status'];
$breakdown = $breakdown ?? null;
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'VAT Return',
    'workspaceTitle'        => $return['number'],
    'workspaceDesc'         => $return['period_from'] . ' → ' . $return['period_to'],
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-file-invoice"></i>
        <h2 class="enterprise-settings-card__title">VAT Return 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($return['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 From</dt>
            <dd class="col-sm-3"><?= esc($return['period_from']) ?></dd>
            <dt class="col-sm-3 text-muted">Period To</dt>
            <dd class="col-sm-3"><?= esc($return['period_to']) ?></dd>
            <dt class="col-sm-3 text-muted">Output VAT</dt>
            <dd class="col-sm-3"><?= number_format((float) $return['output_vat'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Input VAT</dt>
            <dd class="col-sm-3"><?= number_format((float) $return['input_vat'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Net VAT</dt>
            <dd class="col-sm-3"><strong><?= number_format((float) $return['net_vat'], 2) ?></strong></dd>
            <dt class="col-sm-3 text-muted">Submitted At</dt>
            <dd class="col-sm-3"><?= esc($return['submitted_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Filed At</dt>
            <dd class="col-sm-3"><?= esc($return['filed_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($return['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/vat-returns') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/vat-returns/' . $return['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/vat-returns/' . $return['id'] . '/calculate') ?>" method="post" class="d-inline">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-primary">Recalculate</button>
            </form>
            <form action="<?= base_url('accounting/vat-returns/' . $return['id'] . '/submit') ?>" method="post" class="d-inline" onsubmit="return confirm('Submit this VAT return? Amounts will be recalculated first.');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
            <form action="<?= base_url('accounting/vat-returns/' . $return['id'] . '/cancel') ?>" method="post" class="d-inline" onsubmit="return confirm('Cancel this draft VAT return?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-warning">Cancel</button>
            </form>
            <form action="<?= base_url('accounting/vat-returns/' . $return['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this draft VAT return?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-danger">Delete</button>
            </form>
        <?php endif; ?>
        <?php if ($st === 'submitted'): ?>
            <form action="<?= base_url('accounting/vat-returns/' . $return['id'] . '/file') ?>" method="post" class="d-inline" onsubmit="return confirm('Mark this VAT return as filed?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">File Return</button>
            </form>
        <?php endif; ?>
    </div>
</article>

<?php if ($breakdown): ?>
    <?= $this->include('Modules\Accounting\Views\tax_reports\_breakdown', ['summary' => $breakdown]) ?>
<?php endif; ?>

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