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

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

<?php
$statusPill = static function (string $status): string {
    $label    = ucwords(str_replace('_', ' ', $status));
    $active   = ['posted'];
    $inactive = ['cancelled'];
    if (in_array($status, $active, true)) {
        $class = 'active';
    } elseif (in_array($status, $inactive, true)) {
        $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 = $note['status'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Debit Note',
    'workspaceTitle'        => $note['number'],
    'workspaceDesc'         => ($note['supplier_name'] ?? 'Debit note') . ' · ' . ($note['debit_date'] ?? ''),
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-file-circle-plus"></i>
        <h2 class="enterprise-settings-card__title">Debit Note 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($note['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">Supplier</dt>
            <dd class="col-sm-3"><?= esc($note['supplier_name'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Bill</dt>
            <dd class="col-sm-3"><?= ! empty($note['bill_number']) ? '<code>' . esc($note['bill_number']) . '</code>' : '—' ?></dd>
            <dt class="col-sm-3 text-muted">Debit Date</dt>
            <dd class="col-sm-3"><?= esc($note['debit_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Posted At</dt>
            <dd class="col-sm-3"><?= esc($note['posted_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Subtotal</dt>
            <dd class="col-sm-3"><?= number_format((float) $note['subtotal'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Tax</dt>
            <dd class="col-sm-3"><?= number_format((float) $note['tax_amount'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Total</dt>
            <dd class="col-sm-3"><?= number_format((float) $note['total'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($note['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/debit-notes') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/debit-notes/' . $note['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/debit-notes/' . $note['id'] . '/post') ?>" method="post" class="d-inline" onsubmit="return confirm('Post this debit note?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Post Debit Note</button>
            </form>
            <form action="<?= base_url('accounting/debit-notes/' . $note['id'] . '/cancel') ?>" method="post" class="d-inline" onsubmit="return confirm('Cancel this debit note?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-secondary">Cancel</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) $note['total'], 2) ?></p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Code</th>
                        <th>Item</th>
                        <th class="text-end">Qty</th>
                        <th>Unit</th>
                        <th class="text-end">Price</th>
                        <th class="text-end">Line Total</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($lines as $line): ?>
                        <tr>
                            <td><?= esc($line['item_code'] ?: '—') ?></td>
                            <td><?= esc($line['item_name']) ?><?= ! empty($line['description']) ? '<br><span class="text-muted small">' . esc($line['description']) . '</span>' : '' ?></td>
                            <td class="text-end"><?= number_format((float) $line['quantity'], 3) ?></td>
                            <td><?= esc($line['unit'] ?: 'pcs') ?></td>
                            <td class="text-end"><?= number_format((float) $line['unit_price'], 2) ?></td>
                            <td class="text-end"><?= number_format((float) $line['line_total'], 2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($lines) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-4">No lines.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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