<?php
// view: pages/customers/view
$c = $customer ?? null;
if (!$c) return;

$isActive = ((int)($c['is_active'] ?? 1) === 1);
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h4 class="mb-1">تفاصيل عميل</h4>
    <div class="text-muted small"><?= e((string)($c['code'] ?? '')) ?></div>
  </div>
  <div class="d-flex gap-2">
    <a class="btn btn-outline-secondary" href="<?= e(url('/parties/customers')) ?>"><i class="fa-solid fa-arrow-right me-1"></i>رجوع</a>
    <?php if (can('customers.edit')): ?>
      <a class="btn btn-outline-dark" href="<?= e(url('/parties/customers')) ?>?edit=<?= (int)$c['id'] ?>"><i class="fa-solid fa-pen me-1"></i>تعديل</a>
    <?php endif; ?>
    <a class="btn btn-dark" target="_blank" href="<?= e(url('/prints/customer')) ?>?id=<?= (int)$c['id'] ?>"><i class="fa-solid fa-print me-1"></i>طباعة</a>
  </div>
</div>

<div class="row g-3">
  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-header d-flex justify-content-between align-items-center">
        <strong><?= e((string)$c['name']) ?></strong>
        <?php if ($isActive): ?>
          <span class="badge text-bg-success">نشط</span>
        <?php else: ?>
          <span class="badge text-bg-secondary">موقوف</span>
        <?php endif; ?>
      </div>
      <div class="card-body">

        <div class="row g-3">
          <div class="col-lg-4">
            <div class="border rounded-3 p-3 h-100">
              <div class="text-muted small">كود العميل</div>
              <div class="fw-bold"><?= e((string)($c['code'] ?? '-')) ?></div>
            </div>
          </div>

          <div class="col-lg-4">
            <div class="border rounded-3 p-3 h-100">
              <div class="text-muted small">الهاتف</div>
              <div class="fw-bold"><?= e((string)($c['phone'] ?? '-')) ?></div>
            </div>
          </div>

          <div class="col-lg-4">
            <div class="border rounded-3 p-3 h-100">
              <div class="text-muted small">هاتف 2</div>
              <div class="fw-bold"><?= e((string)($c['phone2'] ?? '-')) ?></div>
            </div>
          </div>

          <div class="col-lg-4">
            <div class="border rounded-3 p-3 h-100">
              <div class="text-muted small">البريد الإلكتروني</div>
              <div class="fw-bold"><?= e((string)($c['email'] ?? '-')) ?></div>
            </div>
          </div>

          <div class="col-lg-4">
            <div class="border rounded-3 p-3 h-100">
              <div class="text-muted small">الرقم الضريبي</div>
              <div class="fw-bold"><?= e((string)($c['tax_no'] ?? '-')) ?></div>
            </div>
          </div>

          <div class="col-lg-4">
            <div class="border rounded-3 p-3 h-100">
              <div class="text-muted small">الرصيد الافتتاحي</div>
              <div class="fw-bold"><?= format_money($c['opening_balance'] ?? 0) ?></div>
            </div>
          </div>

          <div class="col-lg-4">
            <div class="border rounded-3 p-3 h-100">
              <div class="text-muted small">الحد الائتماني</div>
              <div class="fw-bold"><?= format_money($c['credit_limit'] ?? 0) ?></div>
            </div>
          </div>

          <div class="col-12">
            <div class="border rounded-3 p-3">
              <div class="text-muted small">العنوان</div>
              <div class="fw-bold"><?= e((string)($c['address'] ?? '-')) ?></div>
            </div>
          </div>

          <div class="col-12">
            <div class="border rounded-3 p-3">
              <div class="text-muted small">ملاحظات</div>
              <div class="fw-bold"><?= e((string)($c['notes'] ?? '-')) ?></div>
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
</div>
