<?php $docs = $docs ?? []; $warehouses = $warehouses ?? []; ?>
<div class="row g-3">
  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-header"><strong>إنشاء مستند افتتاحي جديد</strong></div>
      <div class="card-body">
        <form method="post" action="<?= e(url('/inventory/opening-stock')) ?>" class="vstack gap-2">
          <?= csrf_input() ?>
          <input type="hidden" name="action" value="create">
          <div>
            <label class="form-label">المخزن</label>
            <select class="form-select" name="warehouse_id" required>
              <option value="">اختر...</option>
              <?php foreach ($warehouses as $w): ?>
                <option value="<?= (int)$w['id'] ?>"><?= e($w['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="row g-2">
            <div class="col-6">
              <label class="form-label">تاريخ المستند</label>
              <input class="form-control" type="date" name="doc_date" value="<?= e(date('Y-m-d')) ?>" required>
            </div>
            <div class="col-6">
              <label class="form-label">ملاحظات (اختياري)</label>
              <input class="form-control" name="notes" placeholder="مثال: افتتاحي 2026">
            </div>
          </div>
          <button class="btn btn-primary"><i class="fa-solid fa-plus me-2"></i>إنشاء</button>
          <div class="form-text">
            بعد الإنشاء هتدخل الأسطر (الأصناف + الكميات) ثم تعمل ترحيل.
          </div>
        </form>
      </div>
    </div>
  </div>

  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-header"><strong>مستندات الافتتاحي</strong></div>
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-striped table-bordered align-middle mb-0">
            <thead class="table-light">
              <tr>
                <th>رقم المستند</th>
                <th>التاريخ</th>
                <th>المخزن</th>
                <th class="text-center" style="width:110px;">الحالة</th>
                <th class="text-center" style="width:110px;">فتح</th>
                <th class="text-center" style="width:110px;">حذف</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($docs as $d): ?>
                <tr>
                  <td class="fw-semibold"><?= e($d['doc_no']) ?></td>
                  <td><?= e($d['doc_date']) ?></td>
                  <td><?= e($d['warehouse_name']) ?></td>
                  <td class="text-center">
                    <?php if ($d['status']==='POSTED'): ?>
                      <span class="badge bg-success">POSTED</span>
                    <?php else: ?>
                      <span class="badge bg-secondary"><?= e($d['status']) ?></span>
                    <?php endif; ?>
                  </td>
                  <td class="text-center">
                    <a class="btn btn-sm btn-outline-primary" href="<?= e(url('/inventory/opening-stock')) ?>?action=edit&id=<?= (int)$d['id'] ?>">
                      <i class="fa-solid fa-pen-to-square"></i>
                    </a>
                  </td>
                  <td class="text-center">
                    <?php if (strtoupper((string)$d['status'])==='DRAFT'): ?>
                      <form method="post" action="<?= e(url('/inventory/opening-stock')) ?>" onsubmit="return confirm('تأكيد حذف المستند؟');" style="display:inline;">
                        <?= csrf_input() ?>
                        <input type="hidden" name="action" value="delete">
                        <input type="hidden" name="doc_id" value="<?= (int)$d['id'] ?>">
                        <button class="btn btn-sm btn-outline-danger" type="submit" title="حذف">
                          <i class="fa-solid fa-trash"></i>
                        </button>
                      </form>
                    <?php else: ?>
                      —
                    <?php endif; ?>
                  </td>
                </tr>
              <?php endforeach; ?>
              <?php if (!$docs): ?>
                <tr><td colspan="6" class="text-center text-muted py-5">لا يوجد مستندات</td></tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>
