<?php
// variables: $row
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">سند صرف: <?= e($row['voucher_no']) ?></h4>
  <div class="d-flex gap-2">
    <?php if (((string)($row['payment_type'] ?? 'party')) === 'expense' && strtoupper((string)($row['status'] ?? 'POSTED')) === 'POSTED' && can('cash.payments.cancel_expense')): ?>
      <a class="btn btn-outline-danger" href="<?= e(url('/finance/cash-payments/cancel?id='.(int)$row['id'])) ?>"><i class="fa-solid fa-ban"></i> إلغاء السند</a>
    <?php endif; ?>
    <?php if (can('cash.payments.print')): ?>
      <a class="btn btn-outline-primary" href="<?= e(url('/prints/cash-payment?id='.(int)($row['id'] ?? 0))) ?>" target="_blank" rel="noopener">طباعة</a>
    <?php endif; ?>
    <a class="btn btn-outline-secondary" href="<?= e(url($cash_payments_url ?? '/finance/cash-payments')) ?>">رجوع</a>
  </div>
</div>

<div class="card p-3">
  <div class="row g-3">
    <div class="col-md-3">
      <div class="text-muted small">التاريخ</div>
      <div class="fw-semibold"><?= e(date('Y-m-d', strtotime((string)$row['voucher_date']))) ?></div>
    </div>

    <div class="col-md-3">
      <div class="text-muted small">نوع السند</div>
      <div class="fw-semibold"><?= ((string)($row['payment_type'] ?? 'party')) === 'expense' ? 'مصروف' : 'سداد طرف' ?></div>
    </div>

    <div class="col-md-3">
      <div class="text-muted small">الحالة</div>
      <div class="fw-semibold">
        <?php $st = strtoupper((string)($row['status'] ?? 'POSTED')); ?>
        <?php if ($st === 'CANCELED'): ?>
          <span class="badge text-bg-danger">ملغي</span>
        <?php else: ?>
          <span class="badge text-bg-success">مرحّل</span>
        <?php endif; ?>
      </div>
    </div>

    <div class="col-md-3">
      <div class="text-muted small">الصرف من</div>
      <div class="fw-semibold"><?php $src=(string)($row['account_source_type'] ?? 'cashbox'); if($src==='bank_account'){ echo e((string)($row['bank_account_name'] ?? '')); } else { echo e((string)($row['cashbox_name'] ?? '')); } ?></div>
    </div>

    <div class="col-md-3">
      <div class="text-muted small">الطريقة</div>
      <div class="fw-semibold">
        <?php
          $pm = (string)($row['pay_method'] ?? 'cash');
          echo $pm==='cheque' ? 'شيك' : ($pm==='bank_transfer' ? 'تحويل بنكي' : 'نقدي');
        ?>
      </div>
    </div>

    <div class="col-md-3">
      <div class="text-muted small">المبلغ</div>
      <div class="fw-semibold fs-5"><?= e(number_format((float)$row['amount'], 2)) ?></div>
    </div>

    <?php if (((string)($row['payment_type'] ?? 'party')) === 'expense'): ?>
      <div class="col-md-5">
        <div class="text-muted small">بند المصروف</div>
        <div class="fw-semibold"><?= e((string)($row['expense_item_name'] ?? '')) ?></div>
      </div>
      <div class="col-md-4">
        <div class="text-muted small">مجموعة المصروف</div>
        <div class="fw-semibold"><?= e((string)($row['expense_category_name'] ?? '')) ?></div>
      </div>
    <?php else: ?>
      <div class="col-md-9">
        <div class="text-muted small">المستفيد/الطرف</div>
        <div class="fw-semibold"><?= e((string)($row['payee_name'] ?? ($row['party_name'] ?? ''))) ?></div>
      </div>
    <?php endif; ?>

    <div class="col-md-6">
      <div class="text-muted small">البيان</div>
      <div class="fw-semibold"><?= e((string)($row['purpose'] ?? '')) ?></div>
    </div>

    <div class="col-md-6">
      <div class="text-muted small">اسم الجهة المستفيدة</div>
      <div class="fw-semibold"><?= e((string)($row['payee_name'] ?? '')) ?></div>
    </div>

    <div class="col-md-4">
      <div class="text-muted small">رقم مرجعي</div>
      <div class="fw-semibold"><?= e((string)($row['ref_no'] ?? '')) ?></div>
    </div>

    <div class="col-md-8">
      <div class="text-muted small">ملاحظات</div>
      <div class="fw-semibold"><?= e((string)($row['notes'] ?? '')) ?></div>
    </div>
  </div>
</div>

<?php if (strtoupper((string)($row['status'] ?? 'POSTED')) === 'CANCELED'): ?>
<div class="card p-3 mt-3 border-danger">
  <div class="row g-3">
    <div class="col-md-4"><div class="text-muted small">تاريخ الإلغاء</div><div class="fw-semibold"><?= e((string)($row['canceled_at'] ?? '')) ?></div></div>
    <div class="col-md-8"><div class="text-muted small">سبب الإلغاء</div><div class="fw-semibold"><?= e((string)($row['cancel_reason'] ?? '')) ?></div></div>
  </div>
</div>
<?php endif; ?>
