<?php
// view: pages/warehouse/index
$rows = $rows ?? [];
$edit_wh = $edit_wh ?? null;
$has_allow_negative = $has_allow_negative ?? false;
?>
<div class="row g-3">
  <div class="col-lg-7">
    <div class="card shadow-sm">
      <div class="card-header d-flex align-items-center justify-content-between">
        <strong>قائمة المخازن</strong>
        <span class="text-muted small"><?= count($rows) ?> مخزن</span>
      </div>
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-hover table-striped table-bordered align-middle mb-0">
            <thead class="table-light">
              <tr>
                <th style="width:90px;">#</th>
                <th style="width:140px;">كود</th>
                <th>اسم المخزن</th>
                <?php if ($has_allow_negative): ?>
                  <th class="text-center" style="width:140px;">فتح بالسالب</th>
                <?php endif; ?>
                <th class="text-center" style="width:120px;">رئيسي</th>
                <th class="text-center" style="width:120px;">حالة</th>
                <th class="text-center" style="width:110px;">تعديل</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($rows as $r): ?>
                <tr>
                  <td><?= (int)$r['id'] ?></td>
                  <td><?= e($r['code']) ?></td>
                  <td class="fw-semibold"><?= e($r['name']) ?></td>
                  <?php if ($has_allow_negative): ?>
                    <td class="text-center">
                      <?= ((int)($r['allow_negative'] ?? 0)===1)
                        ? '<span class="badge text-bg-warning">مسموح</span>'
                        : '<span class="badge text-bg-light text-dark">ممنوع</span>' ?>
                    </td>
                  <?php endif; ?>
                  <td class="text-center"><?= ((int)$r['is_main']===1) ? '<span class="badge text-bg-primary">نعم</span>' : '' ?></td>
                  <td class="text-center"><?= ((int)$r['is_active']===1) ? '<span class="badge text-bg-success">فعال</span>' : '<span class="badge text-bg-secondary">متوقف</span>' ?></td>
                  <td class="text-center">
                    <a class="btn btn-sm btn-outline-primary" href="<?= e(url('/inventory/warehouses')) ?>?action=edit&id=<?= (int)$r['id'] ?>">
                      <i class="fa-solid fa-pen"></i>
                    </a>
                  </td>
                </tr>
              <?php endforeach; ?>
              <?php if (!$rows): ?>
                <tr><td colspan="<?= $has_allow_negative ? 7 : 6 ?>" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <div class="col-lg-5">
    <div class="card shadow-sm">
      <div class="card-header">
        <strong><?= $edit_wh ? 'تعديل مخزن' : 'إضافة مخزن' ?></strong>
      </div>
      <div class="card-body">
        <form method="post" class="vstack gap-3">
          <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
          <input type="hidden" name="action" value="save">
          <input type="hidden" name="id" value="<?= (int)($edit_wh['id'] ?? 0) ?>">

          <div>
            <label class="form-label">الكود</label>
            <input class="form-control" name="code" value="<?= e($edit_wh['code'] ?? '') ?>" placeholder="اختياري (مثال: MAIN)">
          </div>

          <div>
            <label class="form-label">اسم المخزن</label>
            <input class="form-control" name="name" value="<?= e($edit_wh['name'] ?? '') ?>" required>
          </div>

          <div class="row g-2">
            <div class="col-6">
              <div class="form-check mt-4">
                <input class="form-check-input" type="checkbox" name="is_main" value="1" id="is_main" <?= !empty($edit_wh) && (int)$edit_wh['is_main']===1 ? 'checked' : '' ?>>
                <label class="form-check-label" for="is_main">مخزن رئيسي</label>
              </div>
            </div>
            <div class="col-6">
              <div class="form-check mt-4">
                <input class="form-check-input" type="checkbox" name="is_active" value="1" id="is_active" <?= (empty($edit_wh) || (int)($edit_wh['is_active'] ?? 1)===1) ? 'checked' : '' ?>>
                <label class="form-check-label" for="is_active">فعال</label>
              </div>
            </div>
          </div>

          <?php if ($has_allow_negative): ?>
          <div class="form-check">
            <input class="form-check-input" type="checkbox" name="allow_negative" value="1" id="allow_negative"
              <?= !empty($edit_wh) && (int)($edit_wh['allow_negative'] ?? 0)===1 ? 'checked' : '' ?>>
            <label class="form-check-label" for="allow_negative">السماح بفتح الرصيد بالسالب</label>
          </div>
          <?php else: ?>
            <div class="alert alert-info small mb-0">
              لإظهار خيار "فتح الرصيد بالسالب" على مستوى المخزن، نفّذ ملف التحديث:
              <code>sql/migrate_v1_18_allow_negative.sql</code>
            </div>
          <?php endif; ?>

          <div class="d-flex gap-2">
            <button class="btn btn-primary flex-fill"><i class="fa-solid fa-save me-2"></i>حفظ</button>
            <a class="btn btn-outline-secondary" href="<?= e(url('/inventory/warehouses')) ?>">جديد</a>
          </div>
        </form>
      </div>
      <div class="card-footer text-muted small">
        تنبيه: اختيار "مخزن رئيسي" سيجعل باقي المخازن غير رئيسية تلقائيًا.
      </div>
    </div>
  </div>
</div>
