<?php
// vars: $payment
$p = $payment ?? [];
$status = (string)($p['status'] ?? '');
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h3 class="mb-0">سند سداد طرف</h3>
    <div class="text-muted small">رقم: <strong><?= e((string)($p['pay_no'] ?? '')) ?></strong></div>
  </div>
  <div class="d-flex gap-2">
    <a class="btn btn-outline-secondary" href="<?= e($supplier_payments_url ?? url('/purchases/supplier-payments')) ?>">رجوع</a>
    <?php if (can('supplier_payments.print')): ?>
      <a class="btn btn-outline-primary" href="<?= e(url('/prints/supplier-payment?id='.(int)($p['id'] ?? 0))) ?>" target="_blank" rel="noopener">
        <i class="fa-solid fa-print me-1"></i>طباعة
      </a>
    <?php endif; ?>
    <?php if ($status !== 'POSTED' && can('supplier_payments.edit')): ?>
      <a class="btn btn-primary" href="<?= e(($supplier_payment_form_url ?? url('/purchases/supplier-payments/form')) . '?id='.(int)($p['id'] ?? 0)) ?>">
        <i class="fa-solid fa-pen me-1"></i>تعديل
      </a>
    <?php endif; ?>
  </div>
</div>

<div class="row g-3">
  <div class="col-lg-8">
    <div class="card card-body">
      <div class="row g-3">
        <div class="col-md-6">
          <div class="text-muted small">الطرف</div>
          <div class="fw-bold"><?= e((string)($p['party_name'] ?? '')) ?></div>
        </div>
        <div class="col-md-3">
          <div class="text-muted small">التاريخ</div>
          <div class="fw-bold"><?= e((string)($p['pay_date'] ?? '')) ?></div>
        </div>
        <div class="col-md-3">
          <div class="text-muted small">الحالة</div>
          <div>
            <?php if ($status === 'POSTED'): ?>
              <span class="badge bg-success">POSTED</span>
            <?php else: ?>
              <span class="badge bg-secondary">DRAFT</span>
            <?php endif; ?>
          </div>
        </div>

        <div class="col-md-6">
          <div class="text-muted small">طريقة السداد</div>
          <div class="fw-bold"><?= e((string)($p['method'] ?? '')) ?></div>
        </div>
        <div class="col-md-6">
          <div class="text-muted small">رقم مرجعي</div>
          <div class="fw-bold"><?= e((string)($p['ref_no'] ?? '')) ?></div>
        </div>

        <div class="col-12">
          <div class="text-muted small">ملاحظات</div>
          <div class="fw-bold"><?= e((string)($p['notes'] ?? '')) ?></div>
        </div>
      </div>
    </div>
    <?php if (!empty($allocations ?? [])): ?>
      <hr class="my-3">
      <h6 class="mb-2">توزيع السداد على فواتير</h6>
      <div class="table-responsive">
        <table class="table table-sm table-bordered align-middle">
          <thead class="table-light">
            <tr>
              <th style="width:120px">رقم الفاتورة</th>
              <th style="width:120px">التاريخ</th>
              <th class="text-end" style="width:160px">المبلغ</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach (($allocations ?? []) as $a): ?>
              <tr>
                <td><span class="badge bg-secondary"><?= e((string)($a['inv_no'] ?? '')) ?></span></td>
                <td><?= e((string)($a['inv_date'] ?? '')) ?></td>
                <td class="text-end"><?= e(format_money((float)($a['amount'] ?? 0))) ?></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
          <tfoot>
            <tr>
              <th colspan="2" class="text-end">إجمالي التوزيع</th>
              <th class="text-end"><?= e(format_money((float)($alloc_total ?? 0))) ?></th>
            </tr>
            <tr>
              <th colspan="2" class="text-end">المتبقي (دفعة مقدّمة)</th>
              <th class="text-end"><?= e(format_money((float)($unallocated ?? 0))) ?></th>
            </tr>
          </tfoot>
        </table>
      </div>
    <?php endif; ?>

  </div>

  <div class="col-lg-4">
    <div class="card card-body">
      <div class="text-muted">مبلغ السداد</div>
      <div class="display-6 fw-bold mb-2"><?= e(number_format((float)($p['amount'] ?? 0),2)) ?></div>
      <?php if ($status !== 'POSTED' && can('supplier_payments.post')): ?>
        <form method="post" onsubmit="return confirm('تأكيد ترحيل السداد؟');">
          <?= csrf_field() ?>
          <input type="hidden" name="action" value="post">
          <button class="btn btn-success w-100"><i class="fa-solid fa-circle-check me-1"></i>ترحيل</button>
        </form>
      <?php endif; ?>
    </div>
  </div>
</div>
