<?php $this->start('body'); ?>
<?php $s = $this->data['s']; $bills = $this->data['bills'];
$openBalance = 0.0;
foreach ($bills as $b) { if (in_array($b['status'], ['posted','paid'], true)) $openBalance += (float)$b['balance_due']; }
$bg = ($s['status'] === 'ACTIVE') ? 'success' : 'secondary';
?>

<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-industry me-2 text-muted"></i><?= htmlspecialchars($s['vendor_name']); ?>
      <span class="badge bg-<?= $bg; ?> ms-2" style="font-size:.65em;vertical-align:middle;"><?= htmlspecialchars($s['status']); ?></span>
    </h5>
    <div class="text-muted small"><span class="font-monospace"><?= htmlspecialchars($s['vendor_no']); ?></span>
      <?php if ($s['vendor_tin']): ?> · TIN <?= htmlspecialchars($s['vendor_tin']); ?><?php endif; ?>
      <?php if ($s['vendor_category']): ?> · <?= htmlspecialchars($s['vendor_category']); ?><?php endif; ?>
    </div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>Suppliers" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
    <div class="btn-group btn-group-sm">
      <a href="<?= PROOT; ?>SupplierStatements/pdf/<?= (int)$s['c0d']; ?>?from=<?= date('Y-01-01'); ?>&to=<?= date('Y-m-d'); ?>" target="_blank" class="btn btn-outline-primary" title="YTD statement">
        <i class="fas fa-file-invoice me-1"></i>Statement
      </a>
      <a href="<?= PROOT; ?>SupplierStatements" class="btn btn-outline-primary" title="Custom range"><i class="fas fa-caret-down"></i></a>
    </div>
    <a href="<?= PROOT; ?>Suppliers/edit/<?= (int)$s['c0d']; ?>" class="btn btn-sm btn-primary"><i class="fas fa-pen me-1"></i>Edit</a>
  </div>
</div>

<?= flashHtml(); ?>

<div class="row g-3">
  <div class="col-lg-5">
    <div class="card border-0 shadow-sm h-100"><div class="card-body">
      <h6 class="text-uppercase text-muted small mb-3">Details</h6>
      <dl class="row mb-0 small">
        <?php if ($s['vendor_phone_1']): ?><dt class="col-4 text-muted fw-normal">Phone</dt><dd class="col-8"><?= htmlspecialchars($s['vendor_phone_1']); ?><?php if ($s['vendor_phone_2']): ?> · <?= htmlspecialchars($s['vendor_phone_2']); ?><?php endif; ?></dd><?php endif; ?>
        <?php if ($s['vendor_email']):   ?><dt class="col-4 text-muted fw-normal">Email</dt>  <dd class="col-8"><?= htmlspecialchars($s['vendor_email']); ?></dd><?php endif; ?>
        <?php if ($s['vendor_address']): ?><dt class="col-4 text-muted fw-normal">Address</dt><dd class="col-8"><?= htmlspecialchars(trim($s['vendor_address'] . ', ' . $s['vendor_city'] . ', ' . $s['vendor_country'], ', ')); ?></dd><?php endif; ?>
        <?php if ($s['vendor_vrn']):     ?><dt class="col-4 text-muted fw-normal">VRN</dt><dd class="col-8 font-monospace"><?= htmlspecialchars($s['vendor_vrn']); ?></dd><?php endif; ?>
        <?php if ($s['vendor_poc']):     ?><dt class="col-4 text-muted fw-normal">Contact</dt><dd class="col-8"><?= htmlspecialchars($s['vendor_poc']); ?><?php if ($s['vendor_poc_phone']): ?> · <?= htmlspecialchars($s['vendor_poc_phone']); ?><?php endif; ?></dd><?php endif; ?>
        <?php if ($s['bank_name']):      ?><dt class="col-4 text-muted fw-normal">Bank</dt><dd class="col-8"><?= htmlspecialchars($s['bank_name'] . ' · ' . $s['bank_account_no']); ?></dd><?php endif; ?>
      </dl>
    </div></div>
  </div>
  <div class="col-lg-7">
    <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">Bills · <?= count($bills); ?></h6>
          <div class="small">
            <span class="text-muted me-2">Open balance</span>
            <strong class="<?= $openBalance > 0.005 ? 'text-danger' : 'text-success'; ?>">TZS <?= number_format($openBalance, 2); ?></strong>
          </div>
        </div>
        <table class="table table-sm mb-0">
          <thead class="table-light"><tr><th>Bill</th><th>Date</th><th>Due</th><th class="money">Total</th><th class="money">Balance</th><th>Status</th></tr></thead>
          <tbody>
            <?php if (empty($bills)): ?>
              <tr><td colspan="6" class="text-center text-muted small py-4">No bills yet. <a href="<?= PROOT; ?>SupplierBills/add">Create one</a>.</td></tr>
            <?php else: ?>
              <?php foreach ($bills as $b):
                $b_bg = ['draft'=>'secondary','posted'=>'warning text-dark','paid'=>'success','void'=>'dark'][$b['status']] ?? 'light';
              ?>
                <tr>
                  <td class="font-monospace small"><a href="<?= PROOT; ?>SupplierBills/view/<?= (int)$b['c0d']; ?>" class="text-decoration-none"><?= htmlspecialchars($b['bill_no']); ?></a></td>
                  <td class="small"><?= humanDate($b['bill_date']); ?></td>
                  <td class="small"><?= $b['due_date'] ? humanDate($b['due_date']) : '—'; ?></td>
                  <td class="money font-monospace small"><?= number_format((float)$b['total_amount'], 2); ?></td>
                  <td class="money font-monospace small fw-semibold"><?= number_format((float)$b['balance_due'], 2); ?></td>
                  <td><span class="badge bg-<?= $b_bg; ?>"><?= htmlspecialchars($b['status']); ?></span></td>
                </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<?php $this->end(); ?>
