<?php $this->start('body');
    $from       = $this->v('from');
    $to         = $this->v('to');
    $outVat     = (float)($this->data['output_vat']  ?? 0);
    $inVat      = (float)($this->data['input_vat']   ?? 0);
    $net        = (float)($this->data['net_payable'] ?? 0);
    $outLines   = $this->data['output_lines']  ?? [];
    $inLines    = $this->data['input_lines']   ?? [];
    $efdRcp     = $this->data['efd_receipts']  ?? [];
    $supEfd     = $this->data['sup_efd_bills'] ?? [];
    $efdVatSum  = (float)($this->data['efd_vat_sum'] ?? 0);
    $supVatSum  = (float)($this->data['sup_vat_sum'] ?? 0);
    $warnings   = $this->data['warnings'] ?? [];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-percentage me-2 text-muted"></i>VAT Return</h5>
</div>

<form method="get" action="<?= PROOT; ?>VatReturn" class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label">Period from</label><input type="date" name="from" value="<?= htmlspecialchars($from); ?>" class="form-control form-control-sm"></div>
            <div class="col-md-3"><label class="form-label">Period to</label>  <input type="date" name="to"   value="<?= htmlspecialchars($to); ?>"   class="form-control form-control-sm"></div>
            <div class="col-md-6 text-end">
                <?php $today = date('Y-m-d'); $mStart = date('Y-m-01'); $mEnd = date('Y-m-t'); $qStart = date('Y-' . str_pad((intdiv((int)date('n')-1,3))*3+1, 2, '0', STR_PAD_LEFT) . '-01'); ?>
                <a href="<?= PROOT; ?>VatReturn?from=<?= $mStart; ?>&amp;to=<?= $today; ?>"  class="btn btn-sm btn-outline-secondary">This month</a>
                <a href="<?= PROOT; ?>VatReturn?from=<?= $qStart; ?>&amp;to=<?= $today; ?>"  class="btn btn-sm btn-outline-secondary">QTD</a>
                <a href="<?= PROOT; ?>VatReturn?from=<?= date('Y-01-01'); ?>&amp;to=<?= $today; ?>"  class="btn btn-sm btn-outline-secondary">YTD</a>
                <button type="submit" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;"><i class="fas fa-filter me-1"></i>Apply</button>
            </div>
        </div>
    </div>
</form>

<div class="row g-3 mb-3">
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body p-3">
                <div class="text-muted small">Output VAT <span class="text-muted" style="font-size:.68rem;">(collected from customers)</span></div>
                <div style="font-weight:700;font-size:1.4rem;color:#3b82f6;">TZS <?= number_format($outVat); ?></div>
                <div class="text-muted small mt-2"><?= count($outLines); ?> journal line(s) · 8110 credits</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body p-3">
                <div class="text-muted small">Input VAT <span class="text-muted" style="font-size:.68rem;">(paid to suppliers, claimable)</span></div>
                <div style="font-weight:700;font-size:1.4rem;color:#22c55e;">TZS <?= number_format($inVat); ?></div>
                <div class="text-muted small mt-2"><?= count($inLines); ?> journal line(s) · 8210 debits</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= $net>=0?'#f59e0b':'#22c55e'; ?> !important;">
            <div class="card-body p-3">
                <div class="text-muted small">Net VAT · <?= $net>=0 ? 'payable to TRA' : 'credit / refundable'; ?></div>
                <div style="font-weight:700;font-size:1.6rem;color:<?= $net>=0?'#f59e0b':'#22c55e'; ?>;">TZS <?= number_format(abs($net)); ?></div>
                <div class="text-muted small mt-2">= Output − Input</div>
            </div>
        </div>
    </div>
</div>

<?php foreach ($warnings as $w): ?>
    <div class="alert alert-warning py-2 mb-3"><i class="fas fa-triangle-exclamation me-1"></i><?= htmlspecialchars($w); ?></div>
<?php endforeach; ?>

<div class="row g-3">
    <!-- ── Output VAT · EFD receipts ─────────────────────────────────── -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white d-flex align-items-center justify-content-between">
                <strong><i class="fas fa-receipt me-2 text-muted"></i>EFD-VAT receipts issued</strong>
                <span class="text-muted small">Σ output VAT: <strong>TZS <?= number_format($efdVatSum); ?></strong></span>
            </div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light">
                        <tr><th>Date</th><th>Receipt #</th><th>EFD #</th><th>Customer</th><th class="text-end">Total</th><th class="text-end">VAT</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($efdRcp as $r): ?>
                            <tr>
                                <td class="text-muted small"><?= htmlspecialchars($r['receipt_date']); ?></td>
                                <td><a href="<?= PROOT; ?>Receipts/view/<?= (int)$r['c0d']; ?>"><strong><?= htmlspecialchars($r['receipt_no']); ?></strong></a></td>
                                <td class="text-muted small"><code class="text-body"><?= htmlspecialchars($r['efd_receipt_number']); ?></code></td>
                                <td><?= htmlspecialchars($r['customer_name']); ?></td>
                                <td class="text-end"><?= number_format((float)$r['amount']); ?></td>
                                <td class="text-end" style="font-weight:600;color:#3b82f6;"><?= number_format((float)$r['efd_output_vat']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($efdRcp)): ?><tr><td colspan="6" class="text-center text-muted py-3">No EFD-VAT receipts in this period.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- ── Input VAT · Supplier EFD-VAT bills ────────────────────────── -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white d-flex align-items-center justify-content-between">
                <strong><i class="fas fa-file-invoice me-2 text-muted"></i>Supplier EFD-VAT tax invoices</strong>
                <span class="text-muted small">Σ captured VAT: <strong>TZS <?= number_format($supVatSum); ?></strong></span>
            </div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light">
                        <tr><th>Date</th><th>LPO</th><th>EFD #</th><th>Supplier</th><th class="text-end">Net</th><th class="text-end">VAT</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($supEfd as $r): ?>
                            <tr>
                                <td class="text-muted small"><?= htmlspecialchars($r['supplier_efd_date'] ?: '—'); ?></td>
                                <td><a href="<?= PROOT; ?>Lpos/view/<?= (int)$r['c0d']; ?>"><strong><?= htmlspecialchars($r['lpo_no']); ?></strong></a></td>
                                <td class="text-muted small"><code class="text-body"><?= htmlspecialchars($r['supplier_efd_number']); ?></code></td>
                                <td><?= htmlspecialchars($r['payee_name']); ?><?php if ($r['supplier_tin']): ?><div class="text-muted" style="font-size:.68rem;">TIN <?= htmlspecialchars($r['supplier_tin']); ?></div><?php endif; ?></td>
                                <td class="text-end"><?= number_format((float)$r['net_amount']); ?></td>
                                <td class="text-end" style="font-weight:600;color:#22c55e;"><?= number_format((float)$r['vat_amount']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($supEfd)): ?><tr><td colspan="6" class="text-center text-muted py-3">No supplier EFD-VAT invoices in this period.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="row g-3 mt-1">
    <div class="col-md-6">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white"><strong>Output VAT · ledger detail (8110 credits)</strong></div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light">
                        <tr><th>Date</th><th>JE #</th><th>Source</th><th>Ref</th><th class="text-end">Credit</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($outLines as $l): ?>
                            <tr>
                                <td class="text-muted small"><?= htmlspecialchars($l['entry_date']); ?></td>
                                <td><a href="<?= PROOT; ?>Journals/view/<?= (int)$l['je_id']; ?>"><strong><?= htmlspecialchars($l['je_no']); ?></strong></a></td>
                                <td><span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($l['source_type'])); ?></span></td>
                                <td class="text-muted small"><?= htmlspecialchars($l['source_ref']); ?></td>
                                <td class="text-end" style="font-weight:600;"><?= number_format((float)$l['vat_credit']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($outLines)): ?><tr><td colspan="5" class="text-center text-muted py-3">No output VAT ledger activity.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white"><strong>Input VAT · ledger detail (8210 debits)</strong></div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light">
                        <tr><th>Date</th><th>JE #</th><th>Source</th><th>Ref</th><th class="text-end">Debit</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($inLines as $l): ?>
                            <tr>
                                <td class="text-muted small"><?= htmlspecialchars($l['entry_date']); ?></td>
                                <td><a href="<?= PROOT; ?>Journals/view/<?= (int)$l['je_id']; ?>"><strong><?= htmlspecialchars($l['je_no']); ?></strong></a></td>
                                <td><span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($l['source_type'])); ?></span></td>
                                <td class="text-muted small"><?= htmlspecialchars($l['source_ref']); ?></td>
                                <td class="text-end" style="font-weight:600;"><?= number_format((float)$l['vat_debit']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($inLines)): ?><tr><td colspan="5" class="text-center text-muted py-3">No input VAT ledger activity.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php $this->end(); ?>
