<?php
// view: pages/stock/wh_docs_list
$docs = $docs ?? [];
$warehouses = $warehouses ?? [];
$from = $from ?? date('Y-m-01');
$to = $to ?? date('Y-m-d');
$type = $type ?? '';
$status = $status ?? '';
$warehouse_id = (int)($warehouse_id ?? 0);
$q = $q ?? '';

$doc_types = [
  '' => 'الكل',
  'RECEIPT' => 'إضافة',
  'ISSUE' => 'صرف',
  'TRANSFER' => 'تحويل',
  'SCRAP' => 'هالك',
  'ADJUST' => 'تسوية',
];
$statuses = [
  '' => 'الكل',
  'POSTED' => 'مرحل',
  'DRAFT' => 'مسودة',
];
?>

<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
  <h4 class="mb-0">استعلام أذونات المخازن</h4>
</div>

<div class="card mb-3">
  <div class="card-header d-flex align-items-center justify-content-between flex-wrap gap-2">
    <span class="fw-semibold">معايير البحث</span>
  </div>
  <div class="card-body">
    <form class="row g-2 align-items-end" method="get">
      <div class="col-12 col-md-3 col-lg-2">
        <label class="form-label mb-1">من</label>
        <input class="form-control" type="date" name="from" value="<?= e($from) ?>">
      </div>
      <div class="col-12 col-md-3 col-lg-2">
        <label class="form-label mb-1">إلى</label>
        <input class="form-control" type="date" name="to" value="<?= e($to) ?>">
      </div>

      <div class="col-12 col-md-3 col-lg-2">
        <label class="form-label mb-1">النوع</label>
        <select class="form-select" name="type">
          <?php foreach ($doc_types as $k=>$label): ?>
            <option value="<?= e($k) ?>" <?= $type===$k?'selected':'' ?>><?= e($label) ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="col-12 col-md-3 col-lg-2">
        <label class="form-label mb-1">الحالة</label>
        <select class="form-select" name="status">
          <?php foreach ($statuses as $k=>$label): ?>
            <option value="<?= e($k) ?>" <?= $status===$k?'selected':'' ?>><?= e($label) ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="col-12 col-md-6 col-lg-2">
        <label class="form-label mb-1">المخزن</label>
        <select class="form-select" name="warehouse_id">
          <option value="0">كل المخازن</option>
          <?php foreach ($warehouses as $wh): ?>
            <option value="<?= (int)$wh['id'] ?>" <?= ((int)$wh['id']===$warehouse_id)?'selected':'' ?>><?= e($wh['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="col-12 col-md-6 col-lg-2">
        <label class="form-label mb-1">بحث</label>
        <input class="form-control" type="text" name="q" placeholder="رقم الإذن / ملاحظات" value="<?= e($q) ?>">
      </div>

      <div class="col-12 col-lg-2">
        <button class="btn btn-outline-dark w-100">عرض</button>
      </div>
    </form>
  </div>
</div>
<div class="card p-3">
  <div class="table-responsive">
    <table class="table table-bordered table-hover table-striped align-middle table-sm mb-0">
      <thead class="table-light">
        <tr>
          <th>#</th>
          <th>رقم الإذن</th>
          <th>النوع</th>
          <th>التاريخ</th>
          <th>المستخدم</th>
          <th>من مخزن</th>
          <th>إلى مخزن</th>
          <th>الحالة</th>
          <th class="text-end">عدد البنود</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($docs as $d): ?>
          <tr>
            <td><?= (int)$d['id'] ?></td>
            <td><span class="fw-semibold"><?= e($d['doc_no']) ?></span></td>
            <td><?= e(doc_type_label($d['doc_type'])) ?></td>
            <td><?= e(substr((string)$d['doc_date'],0,16)) ?></td>
            <td><?= e($d['created_by_name'] ?? '-') ?></td>
            <td><?= e($d['warehouse_name']) ?></td>
            <td><?= e($d['to_warehouse_name'] ?? '-') ?></td>
            <td><?= e(($d['status']==='POSTED')?'مرحل':$d['status']) ?></td>
            <td class="text-end"><?= (int)$d['lines_count'] ?></td>
            <td class="text-end">
              <a class="btn btn-sm btn-outline-dark" href="<?= e(url(($routeContext['view'])((int)$d['id']))) ?>">عرض</a>
              <?php if (($d['status'] ?? '') === 'POSTED'): ?>
                <a class="btn btn-sm btn-outline-success" target="_blank" href="<?= e(url(($routeContext['print'])((int)$d['id']))) ?>">طباعة</a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (!$docs): ?>
          <tr><td colspan="10" class="text-center text-muted py-4">لا توجد أذون</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
