<?php
$party = $party ?? null;
if (!$party) return;
$back_url = url($back_url ?? '/parties');
$edit_url = $edit_url ?? (url('/parties') . '?edit=' . (int)$party['id']);
?>
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
  <div>
    <h4 class="mb-0">تفاصيل الطرف</h4>
    <div class="text-muted small"><?= e((string)($party['code'] ?? '')) ?></div>
  </div>
  <div class="d-flex gap-2">
    <a class="btn btn-outline-secondary" href="<?= e($back_url) ?>">رجوع</a>
    <?php if (can_manage_parties()): ?><a class="btn btn-dark" href="<?= e($edit_url) ?>">تعديل</a><?php endif; ?>
    <?php if ((int)($party['is_customer'] ?? 0) === 1 && (can('sales.statement.view') || can('customers.view'))): ?>
      <a class="btn btn-outline-primary" href="<?= e(url('/reports/parties/customers')) ?>?party_id=<?= (int)$party['id'] ?>">كشف العميل</a>
    <?php endif; ?>
    <?php if ((int)($party['is_supplier'] ?? 0) === 1 && (can('suppliers.view') || can('parties.view'))): ?>
      <a class="btn btn-outline-primary" href="<?= e(url('/reports/parties/suppliers')) ?>?party_id=<?= (int)$party['id'] ?>">كشف المورد</a>
    <?php endif; ?>
  </div>
</div>

<div class="card shadow-sm">
  <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 fs-5"><?= e((string)$party['name']) ?></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"><?php if ((int)($party['is_customer'] ?? 0) === 1): ?><span class="badge text-bg-secondary">عميل</span><?php endif; ?> <?php if ((int)($party['is_supplier'] ?? 0) === 1): ?><span class="badge text-bg-primary">مورد</span><?php endif; ?></div></div></div>
      <div class="col-lg-4"><div class="border rounded-3 p-3 h-100"><div class="text-muted small">الحالة</div><?php if ((int)($party['is_active'] ?? 1) === 1): ?><span class="badge text-bg-success">نشط</span><?php else: ?><span class="badge text-bg-secondary">موقوف</span><?php endif; ?></div></div>
      <div class="col-lg-3"><div class="border rounded-3 p-3 h-100"><div class="text-muted small">هاتف</div><div class="fw-bold"><?= e((string)($party['phone'] ?? '-')) ?></div></div></div>
      <div class="col-lg-3"><div class="border rounded-3 p-3 h-100"><div class="text-muted small">هاتف 2</div><div class="fw-bold"><?= e((string)($party['phone2'] ?? '-')) ?></div></div></div>
      <div class="col-lg-3"><div class="border rounded-3 p-3 h-100"><div class="text-muted small">البريد</div><div class="fw-bold"><?= e((string)($party['email'] ?? '-')) ?></div></div></div>
      <div class="col-lg-3"><div class="border rounded-3 p-3 h-100"><div class="text-muted small">رقم ضريبي</div><div class="fw-bold"><?= e((string)($party['tax_no'] ?? '-')) ?></div></div></div>
      <div class="col-lg-6"><div class="border rounded-3 p-3 h-100"><div class="text-muted small">العنوان</div><div class="fw-bold"><?= e((string)($party['address'] ?? '-')) ?></div></div></div>
      <div class="col-lg-3"><div class="border rounded-3 p-3 h-100"><div class="text-muted small">رصيد افتتاحي</div><div class="fw-bold"><?= format_money($party['opening_balance'] ?? 0) ?></div></div></div>
      <div class="col-lg-3"><div class="border rounded-3 p-3 h-100"><div class="text-muted small">حد ائتماني</div><div class="fw-bold"><?= format_money($party['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)($party['notes'] ?? '-')) ?></div></div></div>
    </div>
  </div>
</div>
