<?php
// variables: $rows, $q, $from, $to
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">سندات الصرف</h4>
  <?php if (can('cash.payments.create')): ?>
    <a class="btn btn-primary" href="<?= e(url($cash_payment_form_url ?? '/finance/cash-payments/form')) ?>"><i class="fa-solid fa-circle-plus"></i> إنشاء سند صرف</a>
  <?php endif; ?>
</div>

<form class="card p-3 mb-3" method="get">
  <div class="row g-2 align-items-end">
    <div class="col-md-3">
      <label class="form-label">من</label>
      <input type="date" class="form-control" name="from" value="<?= e((string)$from) ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label">إلى</label>
      <input type="date" class="form-control" name="to" value="<?= e((string)$to) ?>">
    </div>
    <div class="col-md-4">
      <label class="form-label">بحث</label>
      <input type="text" class="form-control" name="q" placeholder="رقم السند/المستفيد/مرجع" value="<?= e((string)$q) ?>">
    </div>
    <div class="col-md-2 d-flex gap-2">
      <button class="btn btn-outline-primary w-100" type="submit"><i class="fa-solid fa-magnifying-glass"></i> بحث</button>
      <a class="btn btn-outline-secondary" href="<?= e(url($cash_payments_url ?? '/finance/cash-payments')) ?>">مسح</a>
    </div>
  </div>
</form>

<div class="card">
  <div class="table-responsive">
    <table class="table table-striped mb-0">
      <thead>
        <tr>
          <th>رقم السند</th>
          <th>التاريخ</th>
          <th>النوع</th>
          <th>الصرف من</th>
          <th>المستفيد/البند</th>
          <th class="text-end">المبلغ</th>
          <th>الطريقة</th>
          <th>الحالة</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach (($rows ?? []) as $r): ?>
          <?php $isExpense = ((string)($r['payment_type'] ?? 'party')) === 'expense'; ?>
          <tr>
            <td><?= e($r['voucher_no']) ?></td>
            <td><?= e(date('Y-m-d', strtotime((string)$r['voucher_date']))) ?></td>
            <td>
              <?php if ($isExpense): ?>
                <span class="badge text-bg-warning">مصروف</span>
              <?php else: ?>
                <span class="badge text-bg-info">سداد طرف</span>
              <?php endif; ?>
            </td>
            <td><?php $src=(string)($r['account_source_type'] ?? 'cashbox'); if($src==='bank_account'){ echo e((string)($r['bank_account_name'] ?? '')); } else { echo e((string)($r['cashbox_name'] ?? '')); } ?></td>
            <td>
              <?php if ($isExpense): ?>
                <div class="fw-semibold"><?= e((string)($r['expense_item_name'] ?? '')) ?></div>
                <?php if (!empty($r['expense_category_name'])): ?><div class="small text-muted"><?= e((string)$r['expense_category_name']) ?></div><?php endif; ?>
              <?php else: ?>
                <?php if (!empty($r['party_name'])): ?>
                  <span class="badge text-bg-info me-1">طرف</span><?= e((string)$r['party_name']) ?>
                <?php else: ?>
                  <?= e((string)$r['payee_name']) ?>
                <?php endif; ?>
              <?php endif; ?>
            </td>
            <td class="text-end"><?= e(number_format((float)$r['amount'], 2)) ?></td>
            <td>
              <?php
                $pm = (string)($r['pay_method'] ?? 'cash');
                echo $pm==='cheque' ? 'شيك' : ($pm==='bank_transfer' ? 'تحويل بنكي' : 'نقدي');
              ?>
            </td>
            <td>
              <?php if (strtoupper((string)($r['status'] ?? 'POSTED')) === 'CANCELED'): ?>
                <span class="badge text-bg-danger">ملغي</span>
              <?php else: ?>
                <span class="badge text-bg-success">مرحّل</span>
              <?php endif; ?>
            </td>
            <td class="text-end">
              <a class="btn btn-sm btn-outline-primary" href="<?= e((url($cash_payment_view_base_url ?? '/finance/cash-payments/view')) . '?id='.(int)$r['id']) ?>">عرض</a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($rows)): ?>
          <tr><td colspan="9" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
