<?php
$rows = $rows ?? [];
$q = $q ?? '';
$status = $status ?? '';
$from = $from ?? '';
$to = $to ?? '';
?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h4 class="mb-1">مرتجعات البيع</h4>
    <div class="text-muted small">سجل مرتجعات البيع والصلاحيات المستقلة الخاصة بها</div>
  </div>
  <div class="d-flex gap-2">
    <a class="btn btn-outline-secondary" href="<?= e(url($sales_invoices_url ?? '/sales/invoices')) ?>">فواتير المبيعات</a>
  </div>
</div>

<div class="card mb-3">
  <div class="card-body">
    <form class="row g-2 align-items-end" method="get">
      <div class="col-md-4">
        <label class="form-label">بحث</label>
        <input name="q" class="form-control" value="<?= e($q) ?>" placeholder="رقم المرتجع / العميل / رقم الفاتورة">
      </div>
      <div class="col-md-2">
        <label class="form-label">الحالة</label>
        <select name="status" class="form-select">
          <option value="">الكل</option>
          <option value="DRAFT" <?= $status==='DRAFT'?'selected':'' ?>>مسودة</option>
          <option value="POSTED" <?= $status==='POSTED'?'selected':'' ?>>مرحّلة</option>
          <option value="CANCELED" <?= $status==='CANCELED'?'selected':'' ?>>ملغاة</option>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label">من</label>
        <input type="date" name="from" class="form-control" value="<?= e($from) ?>">
      </div>
      <div class="col-md-2">
        <label class="form-label">إلى</label>
        <input type="date" name="to" class="form-control" value="<?= e($to) ?>">
      </div>
      <div class="col-md-2 d-grid">
        <button class="btn btn-outline-primary">بحث</button>
      </div>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-striped table-hover mb-0">
        <thead class="table-light">
          <tr>
            <th>#</th>
            <th>رقم المرتجع</th>
            <th>التاريخ</th>
            <th>العميل</th>
            <th>فاتورة المصدر</th>
            <th>نوع التسوية</th>
            <th class="text-end">الإجمالي</th>
            <th>الحالة</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <?php if (!$rows): ?>
            <tr><td colspan="9" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
          <?php else: foreach ($rows as $r): ?>
            <?php $st = strtoupper((string)$r['status']); $cls = $st==='POSTED'?'success':($st==='DRAFT'?'warning':'secondary'); ?>
            <tr>
              <td><?= (int)$r['id'] ?></td>
              <td class="fw-bold"><?= e((string)$r['return_no']) ?></td>
              <td><?= e(substr((string)$r['return_date'],0,10)) ?></td>
              <td><?= e((string)$r['customer_name']) ?></td>
              <td><?= e((string)$r['source_inv_no']) ?></td>
              <td><?= e(($r['settlement_type'] ?? '')==='cash_refund' ? 'رد نقدي' : 'رصيد للعميل') ?></td>
              <td class="text-end"><?= e(format_money((float)$r['total_amount'])) ?></td>
              <td><span class="badge text-bg-<?= $cls ?>"><?= e($st) ?></span></td>
              <td class="text-end text-nowrap">
                <a class="btn btn-sm btn-outline-secondary" href="<?= e((url($sales_return_view_base_url ?? '/sales/returns/view')) . '?id='.(int)$r['id']) ?>">عرض</a>
                <a class="btn btn-sm btn-outline-dark" target="_blank" href="<?= e(url('/prints/sales-return?id='.(int)$r['id'])) ?>">طباعة</a>
              </td>
            </tr>
          <?php endforeach; endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
