<?php $this->start('body'); ?>
<?php
$r = $this->data['r']; $year = $this->data['year']; $month = $this->data['month'];
$this->data['form_action'] = PROOT . 'TaxReturns/vat';
$dueTs = strtotime($r['due_date']);
$today = strtotime('today');
$overdue = $today > $dueTs;
$dueSoon = !$overdue && ($dueTs - $today) < 7 * 86400;
$refundable = $r['net_vat'] < 0;
?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-receipt me-2 text-muted"></i>VAT Return</h5>
    <div class="text-muted small"><?= date('F Y', strtotime($r['from'])); ?> · due <?= humanDate($r['due_date']); ?>
      <?php if ($overdue): ?><span class="badge bg-danger ms-1">Overdue</span>
      <?php elseif ($dueSoon): ?><span class="badge bg-warning text-dark ms-1">Due soon</span>
      <?php endif; ?>
    </div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>TaxReturns" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Returns</a>
    <button class="btn btn-sm btn-outline-primary" onclick="window.print()"><i class="fas fa-print me-1"></i>Print</button>
  </div>
</div>

<?php include __DIR__ . '/_month_form.php'; ?>

<!-- Headline KPI -->
<div class="row g-2 mb-3">
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Output VAT</div>
    <div class="fs-4 fw-bold text-success">TZS <?= number_format($r['output_vat'], 0); ?></div>
    <div class="small text-muted">from ~<?= number_format($r['sales_base_estimated'], 0); ?> sales base</div>
  </div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Input VAT</div>
    <div class="fs-4 fw-bold text-warning">TZS <?= number_format($r['input_vat'], 0); ?></div>
    <div class="small text-muted">from ~<?= number_format($r['purchase_base_estimated'], 0); ?> purchase base</div>
  </div></div></div>
  <div class="col-md-6"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Net <?= $refundable ? 'refundable' : 'payable'; ?></div>
    <div class="fs-3 fw-bold <?= $refundable ? 'text-info' : 'text-primary'; ?>">
      TZS <?= number_format(abs($r['net_vat']), 2); ?>
    </div>
    <div class="small text-muted">
      <?php if ($refundable): ?>
        Input exceeds Output — carry-forward or file for refund per TRA rules.
      <?php else: ?>
        Remit by <?= humanDate($r['due_date']); ?> (form VAT201).
      <?php endif; ?>
    </div>
  </div></div></div>
</div>

<div class="row g-3">
  <!-- Output side -->
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body p-0">
        <div class="p-3 pb-2 d-flex justify-content-between align-items-center">
          <h6 class="text-uppercase text-muted small mb-0">Output VAT · 4010 VAT Payable</h6>
          <span class="small text-muted"><?= count($r['output_lines']); ?> postings</span>
        </div>
        <table class="table table-sm mb-0 align-middle">
          <thead class="table-light">
            <tr><th style="width:88px;">Date</th><th>JE</th><th class="money">Credit</th><th class="money">Debit</th></tr>
          </thead>
          <tbody>
            <?php if (empty($r['output_lines'])): ?>
              <tr><td colspan="4" class="text-center text-muted py-4">No output VAT this period.</td></tr>
            <?php else: ?>
              <?php foreach ($r['output_lines'] as $l): ?>
                <tr>
                  <td class="small"><?= humanDate($l['entry_date']); ?></td>
                  <td class="small">
                    <span class="font-monospace"><?= htmlspecialchars($l['je_no']); ?></span>
                    <div class="text-muted small"><?= htmlspecialchars($l['source_type']); ?><?php if ($l['line_narrative']): ?> · <?= htmlspecialchars($l['line_narrative']); ?><?php endif; ?></div>
                  </td>
                  <td class="money font-monospace small text-success"><?= (float)$l['credit'] > 0 ? number_format((float)$l['credit'], 2) : '—'; ?></td>
                  <td class="money font-monospace small text-muted"><?= (float)$l['debit'] > 0 ? number_format((float)$l['debit'], 2) : '—'; ?></td>
                </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
          <tfoot class="table-light">
            <tr class="fw-bold">
              <th colspan="2">Net output VAT</th>
              <th colspan="2" class="money font-monospace">TZS <?= number_format($r['output_vat'], 2); ?></th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>

  <!-- Input side -->
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body p-0">
        <div class="p-3 pb-2 d-flex justify-content-between align-items-center">
          <h6 class="text-uppercase text-muted small mb-0">Input VAT · 3008 VAT Recoverable</h6>
          <span class="small text-muted"><?= count($r['input_lines']); ?> postings</span>
        </div>
        <table class="table table-sm mb-0 align-middle">
          <thead class="table-light">
            <tr><th style="width:88px;">Date</th><th>JE</th><th class="money">Debit</th><th class="money">Credit</th></tr>
          </thead>
          <tbody>
            <?php if (empty($r['input_lines'])): ?>
              <tr><td colspan="4" class="text-center text-muted py-4">No input VAT this period.</td></tr>
            <?php else: ?>
              <?php foreach ($r['input_lines'] as $l): ?>
                <tr>
                  <td class="small"><?= humanDate($l['entry_date']); ?></td>
                  <td class="small">
                    <span class="font-monospace"><?= htmlspecialchars($l['je_no']); ?></span>
                    <div class="text-muted small"><?= htmlspecialchars($l['source_type']); ?><?php if ($l['line_narrative']): ?> · <?= htmlspecialchars($l['line_narrative']); ?><?php endif; ?></div>
                  </td>
                  <td class="money font-monospace small text-warning"><?= (float)$l['debit'] > 0 ? number_format((float)$l['debit'], 2) : '—'; ?></td>
                  <td class="money font-monospace small text-muted"><?= (float)$l['credit'] > 0 ? number_format((float)$l['credit'], 2) : '—'; ?></td>
                </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
          <tfoot class="table-light">
            <tr class="fw-bold">
              <th colspan="2">Net input VAT</th>
              <th colspan="2" class="money font-monospace">TZS <?= number_format($r['input_vat'], 2); ?></th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</div>

<!-- Net summary strip -->
<div class="card border-0 shadow-sm mt-3">
  <div class="card-body py-3 d-flex align-items-center justify-content-between">
    <div>
      <div class="text-uppercase small text-muted" style="letter-spacing:.06em;">Net position</div>
      <div class="small text-muted">Output <?= number_format($r['output_vat'], 2); ?> &minus; Input <?= number_format($r['input_vat'], 2); ?></div>
    </div>
    <div class="fs-3 fw-bold <?= $refundable ? 'text-info' : 'text-primary'; ?>">
      TZS <?= number_format(abs($r['net_vat']), 2); ?>
      <span class="fs-6 text-muted"><?= $refundable ? 'refundable' : 'payable'; ?></span>
    </div>
  </div>
</div>
<?php $this->end(); ?>
