<?php $this->start('body'); ?>
<?php $c = $this->data['c']; ?>

<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-building me-2 text-muted"></i><?= htmlspecialchars($c['fname']); ?>
      <?php $bg = ['ACTIVE'=>'success','INACTIVE'=>'secondary','ON_HOLD'=>'warning'][$c['status']] ?? 'secondary'; ?>
      <span class="badge bg-<?= $bg; ?> ms-2" style="text-transform:capitalize;font-size:.65em;vertical-align:middle;"><?= htmlspecialchars(strtolower(str_replace('_',' ',$c['status']))); ?></span>
    </h5>
    <div class="text-muted small">
      Customer #<?= htmlspecialchars($c['customer_no']); ?>
      <?php if ($c['lname']): ?> · <em><?= htmlspecialchars($c['lname']); ?></em><?php endif; ?>
      <?php if ($c['customer_sector']): ?> · <?= htmlspecialchars(ucwords(strtolower($c['customer_sector']))); ?><?php endif; ?>
      · <?= htmlspecialchars(ucwords(strtolower($c['customer_tier']))); ?>
    </div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>Customers" 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; ?>CustomerStatements/pdf/<?= (int)$c['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; ?>CustomerStatements" class="btn btn-outline-primary" title="Custom range">
        <i class="fas fa-caret-down"></i>
      </a>
    </div>
    <a href="<?= PROOT; ?>Customers/edit/<?= (int)$c['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-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3">Identity &amp; Address</h6>
        <dl class="row mb-0">
          <dt class="col-5 text-muted fw-normal">Customer #</dt><dd class="col-7 font-monospace small"><?= htmlspecialchars($c['customer_no']); ?></dd>
          <?php if ($c['customer_code']): ?><dt class="col-5 text-muted fw-normal">Code</dt><dd class="col-7 font-monospace small"><?= htmlspecialchars($c['customer_code']); ?></dd><?php endif; ?>
          <dt class="col-5 text-muted fw-normal">Tier / Sector</dt><dd class="col-7"><?= htmlspecialchars(ucwords(strtolower($c['customer_tier']))); ?> · <?= htmlspecialchars(ucwords(strtolower($c['customer_sector'] ?? '—'))); ?></dd>
          <?php if ($c['customer_tin']): ?><dt class="col-5 text-muted fw-normal">TIN</dt><dd class="col-7 font-monospace small"><?= htmlspecialchars($c['customer_tin']); ?></dd><?php endif; ?>
          <?php if ($c['customer_vrn']): ?><dt class="col-5 text-muted fw-normal">VRN</dt><dd class="col-7 font-monospace small"><?= htmlspecialchars($c['customer_vrn']); ?></dd><?php endif; ?>

          <dt class="col-5 text-muted fw-normal">Phone</dt> <dd class="col-7"><?= htmlspecialchars($c['phone_1'] ?: '—'); ?><?php if ($c['phone_2']): ?> · <?= htmlspecialchars($c['phone_2']); ?><?php endif; ?></dd>
          <?php if ($c['email']): ?><dt class="col-5 text-muted fw-normal">Email</dt><dd class="col-7"><?= htmlspecialchars($c['email']); ?></dd><?php endif; ?>

          <dt class="col-5 text-muted fw-normal">Address</dt><dd class="col-7"><?= htmlspecialchars(trim(($c['physical_address'] ?? '') . (($c['city'] ?? '') ? ', ' . $c['city'] : ''))) ?: '—'; ?></dd>
          <?php if ($c['po_box']): ?><dt class="col-5 text-muted fw-normal">PO Box</dt><dd class="col-7"><?= htmlspecialchars($c['po_box']); ?></dd><?php endif; ?>
          <dt class="col-5 text-muted fw-normal">Country</dt><dd class="col-7"><?= htmlspecialchars($c['country'] ?? '—'); ?></dd>
        </dl>
      </div>
    </div>
  </div>

  <div class="col-lg-6">
    <div class="card border-0 shadow-sm mb-3">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3"><i class="fas fa-user me-1"></i>Primary Contact</h6>
        <?php if (empty($c['customer_poc'])): ?>
          <p class="text-muted small mb-0 fst-italic">No contact person recorded — <a href="<?= PROOT; ?>Customers/edit/<?= (int)$c['c0d']; ?>">add one</a>.</p>
        <?php else: ?>
          <dl class="row mb-0">
            <dt class="col-4 text-muted fw-normal">Name</dt><dd class="col-8 fw-semibold"><?= htmlspecialchars($c['customer_poc']); ?></dd>
            <?php if ($c['customer_poc_role']):  ?><dt class="col-4 text-muted fw-normal">Role</dt><dd class="col-8"><?= htmlspecialchars($c['customer_poc_role']); ?></dd><?php endif; ?>
            <?php if ($c['customer_poc_phone']): ?><dt class="col-4 text-muted fw-normal">Phone</dt><dd class="col-8"><?= htmlspecialchars($c['customer_poc_phone']); ?></dd><?php endif; ?>
            <?php if ($c['customer_poc_email']): ?><dt class="col-4 text-muted fw-normal">Email</dt><dd class="col-8"><?= htmlspecialchars($c['customer_poc_email']); ?></dd><?php endif; ?>
          </dl>
        <?php endif; ?>
      </div>
    </div>

    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3"><i class="fas fa-money-bill me-1"></i>Billing &amp; Credit</h6>
        <dl class="row mb-0">
          <dt class="col-5 text-muted fw-normal">Currency</dt><dd class="col-7"><?= htmlspecialchars($c['currency']); ?></dd>
          <dt class="col-5 text-muted fw-normal">Credit limit</dt>
            <dd class="col-7"><?= (float)$c['credit_limit'] > 0
                ? htmlspecialchars($c['currency']) . ' ' . number_format((float)$c['credit_limit'], 2, '.', ',')
                : '<span class="text-muted">Cash only</span>'; ?></dd>
          <dt class="col-5 text-muted fw-normal">Payment terms</dt><dd class="col-7"><?= (int)$c['payment_terms_days'] > 0 ? 'Net ' . (int)$c['payment_terms_days'] . ' days' : 'Cash on delivery'; ?></dd>
          <?php if (!empty($c['billing_email'])): ?><dt class="col-5 text-muted fw-normal">Billing email</dt><dd class="col-7"><?= htmlspecialchars($c['billing_email']); ?></dd><?php endif; ?>
          <dt class="col-5 text-muted fw-normal">Opening balance</dt>
            <dd class="col-7"><?= (float)$c['opening_balance'] != 0
                ? htmlspecialchars($c['currency']) . ' ' . number_format((float)$c['opening_balance'], 2, '.', ',')
                : '<span class="text-muted">0.00</span>'; ?></dd>
        </dl>
      </div>
    </div>
  </div>

  <?php if (!empty($c['notes'])): ?>
  <div class="col-12">
    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-2">Notes</h6>
        <p class="mb-0"><?= nl2br(htmlspecialchars($c['notes'])); ?></p>
      </div>
    </div>
  </div>
  <?php endif; ?>
</div>

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