<?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($purchase_invoices_url ?? url('/purchases/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"><i class="fa-solid fa-magnifying-glass me-1"></i>بحث</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 style="width:70px">#</th>
            <th>رقم المرتجع</th>
            <th>التاريخ</th>
            <th>المورد</th>
            <th>المخزن</th>
            <th>فاتورة المصدر</th>
            <th class="text-end">الإجمالي</th>
            <th style="width:120px">الحالة</th>
            <th style="width:170px"></th>
          </tr>
        </thead>
        <tbody>
        <?php if (!$rows): ?>
          <tr><td colspan="9" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
        <?php else: ?>
          <?php foreach ($rows as $r): ?>
            <tr>
              <td><?= (int)$r['id'] ?></td>
              <td class="fw-bold"><?= e((string)$r['ret_no']) ?></td>
              <td><?= e(substr((string)$r['ret_date'],0,10)) ?></td>
              <td><?= e((string)$r['supplier_name']) ?></td>
              <td><?= e((string)$r['warehouse_name']) ?></td>
              <td><?= e((string)$r['source_inv_no']) ?></td>
              <td class="text-end"><?= e(format_money((float)$r['total'])) ?></td>
              <td>
                <?php
                  $st = strtoupper((string)$r['status']);
                  $cls = $st==='POSTED'?'success':($st==='DRAFT'?'warning':'secondary');
                ?>
                <span class="badge text-bg-<?= $cls ?>"><?= e($st) ?></span>
              </td>
              <td class="text-end">
                <a class="btn btn-sm btn-outline-secondary" href="<?= e(($purchase_return_view_base_url ?? url('/purchases/returns/view')) . '?id='.(int)$r['id']) ?>">
                  <i class="fa-solid fa-eye"></i>
                </a>
                <?php if (can('purchase_returns.print')): ?>
                  <a class="btn btn-sm btn-outline-primary" target="_blank" href="<?= e(url('/prints/purchase-return?id='.(int)$r['id'])) ?>" title="طباعة">
                    <i class="fa-solid fa-print"></i>
                  </a>
                <?php endif; ?>
              </td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
