<?= $this->extend('admin/layouts/main') ?>
<?= $this->section('content') ?>

<?php $badge = (new \Config\Lookups())->invoiceStatuses[$invoice->status]['badge'] ?? 'secondary'; ?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <a href="<?= site_url('admin/invoices') ?>" class="text-decoration-none small text-muted"><i class="bi bi-arrow-left"></i> Invoices</a>
        <h2 class="h4 fw-bold mt-1 mb-0"><?= esc($invoice->invoice_number) ?></h2>
        <div class="text-muted small">
            <a href="<?= site_url('admin/tenants/' . $tenant->id) ?>" class="text-decoration-none"><?= esc($tenant->hotel_name) ?></a>
        </div>
    </div>
    <span class="badge bg-<?= $badge ?> fs-6"><?= esc(ucfirst($invoice->status)) ?></span>
</div>

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card">
            <div class="card-header">Line Items</div>
            <div class="table-responsive">
                <table class="table mb-0 align-middle">
                    <thead><tr><th>Description</th><th class="text-end">Amount</th></tr></thead>
                    <tbody>
                    <?php foreach ($lineItems as $line): ?>
                        <tr><td><?= esc($line->description) ?></td><td class="text-end"><?= format_money($line->amount) ?></td></tr>
                    <?php endforeach; ?>
                    </tbody>
                    <tfoot>
                        <tr class="fw-bold"><td>Total</td><td class="text-end"><?= format_money($invoice->amount) ?></td></tr>
                    </tfoot>
                </table>
            </div>
        </div>

        <?php if (! empty($transactions)): ?>
        <div class="card mt-3">
            <div class="card-header">Payment Attempts</div>
            <div class="table-responsive">
                <table class="table mb-0 align-middle small">
                    <thead><tr><th>Amount</th><th>Method</th><th>Status</th><th>Date</th></tr></thead>
                    <tbody>
                    <?php foreach ($transactions as $tx):
                        $txBadge = (new \Config\Lookups())->transactionStatuses[$tx->status]['badge'] ?? 'secondary'; ?>
                        <tr>
                            <td><?= format_money($tx->amount) ?></td>
                            <td><?= esc($tx->method ?: '—') ?></td>
                            <td><span class="badge bg-<?= $txBadge ?>"><?= esc(ucfirst($tx->status)) ?></span></td>
                            <td class="text-muted"><?= esc(date('d M Y, g:i a', strtotime($tx->created_at))) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-lg-5">
        <div class="card">
            <div class="card-header">Actions</div>
            <div class="card-body d-flex flex-column gap-2">
                <a href="<?= site_url('admin/invoices/' . $invoice->id . '/pdf') ?>" target="_blank" class="btn btn-outline-primary btn-sm"><i class="bi bi-file-earmark-pdf me-1"></i>View / Download PDF</a>

                <?php if (! in_array($invoice->status, ['paid', 'void'], true)): ?>
                    <form action="<?= site_url('admin/invoices/' . $invoice->id . '/hubtel-payment') ?>" method="post">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-primary btn-sm w-100"><i class="bi bi-phone me-1"></i>Generate Hubtel Payment Link</button>
                    </form>

                    <form action="<?= site_url('admin/invoices/' . $invoice->id . '/mark-paid') ?>" method="post" onsubmit="return confirm('Mark this invoice as paid?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-success btn-sm w-100"><i class="bi bi-check-circle me-1"></i>Mark as Paid</button>
                    </form>

                    <form action="<?= site_url('admin/invoices/' . $invoice->id . '/void') ?>" method="post" onsubmit="return confirm('Void this invoice?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-danger btn-sm w-100"><i class="bi bi-x-circle me-1"></i>Void Invoice</button>
                    </form>
                <?php endif; ?>

                <hr>
                <div class="small text-muted">
                    <div>Due: <?= $invoice->due_date ? esc(date('d M Y', strtotime($invoice->due_date))) : '—' ?></div>
                    <?php if ($invoice->paid_at): ?><div>Paid: <?= esc(date('d M Y, g:i a', strtotime($invoice->paid_at))) ?></div><?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

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