<?php
// view: pages/stock/item_balance
$warehouses = $warehouses ?? [];
$items = $items ?? [];
$warehouse_id = (int)($warehouse_id ?? 0);
$item_id = (int)($item_id ?? 0);
$balance = $balance ?? null;
$moves = $moves ?? [];
?>
<div class="card shadow-sm">
  <div class="card-header">
    <form class="row g-2 align-items-end" method="get">
      <div class="col-md-4">
        <label class="form-label">المخزن</label>
        <select class="form-select" name="warehouse_id">
          <?php foreach ($warehouses as $w): ?>
            <option value="<?= (int)$w['id'] ?>" <?= ((int)$w['id']===$warehouse_id)?'selected':'' ?>>
              <?= e($w['name']) ?><?= ((int)$w['is_main']===1) ? ' (رئيسي)' : '' ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-6">
        <label class="form-label">الصنف</label>
        <select class="form-select" name="item_id" data-searchable-select="1" data-search-placeholder="اكتب أول حرفين من اسم الصنف...">
          <option value="">اختر صنف...</option>
          <?php foreach ($items as $it): ?>
            <option value="<?= (int)$it['id'] ?>" <?= ((int)$it['id']===$item_id)?'selected':'' ?>>
              <?= e($it['name']) ?><?= $it['barcode'] ? ' - ' . e($it['barcode']) : '' ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2 d-grid">
        <button class="btn btn-primary"><i class="fa-solid fa-magnifying-glass me-2"></i>استعلام</button>
      </div>
    </form>
  </div>

  <div class="card-body">
    <?php if ($balance): ?>
      <div class="row g-3">
        <div class="col-lg-6">
          <div class="p-3 border rounded-3 bg-body">
            <div class="text-muted small">الصنف</div>
            <div class="fs-5 fw-bold"><?= e($balance['name']) ?></div>
            <div class="text-muted small"><?= $balance['barcode'] ? ('Barcode: ' . e($balance['barcode'])) : '' ?></div>
          </div>
        </div>
        <div class="col-lg-3">
          <div class="p-3 border rounded-3 bg-body">
            <div class="text-muted small">الرصيد</div>
            <div class="fs-4 fw-bold"><?= format_qty($balance['qty']) ?></div>
          </div>
        </div>
        <div class="col-lg-3">
          <div class="p-3 border rounded-3 bg-body">
            <div class="text-muted small">قيمة المخزون (تقريبية)</div>
            <div class="fs-5 fw-bold"><?= format_money((float)$balance['qty'] * (float)$balance['cost']) ?></div>
            <div class="text-muted small">حسب تكلفة الصنف الحالية</div>
          </div>
        </div>
      </div>

      <hr class="my-4">

      <div class="d-flex justify-content-between align-items-center">
        <strong>آخر 20 حركة</strong>
        <span class="text-muted small">Ledger</span>
      </div>

      <div class="table-responsive mt-2">
        <table class="table table-bordered table-hover table-striped align-middle mb-0">
          <thead class="table-light">
            <tr>
              <th>التاريخ</th>
              <th>النوع</th>
              <th class="text-center">داخل</th>
              <th class="text-center">خارج</th>
              <th class="text-end">التكلفة</th>
              <th class="text-center">مرجع</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($moves as $m): ?>
              <tr>
                <td><?= e($m['created_at']) ?></td>
                <td><?= e($m['ref_type']) ?></td>
                <td class="text-center"><?= format_qty($m['qty_in']) ?></td>
                <td class="text-center"><?= format_qty($m['qty_out']) ?></td>
                <td class="text-end"><?= format_money($m['cost']) ?></td>
                <td class="text-center">#<?= (int)$m['ref_id'] ?></td>
              </tr>
            <?php endforeach; ?>
            <?php if (!$moves): ?><tr><td colspan="6" class="text-center text-muted py-4">لا توجد حركات</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>

    <?php else: ?>
      <div class="text-muted">اختر مخزن وصنف ثم اضغط استعلام.</div>
    <?php endif; ?>
  </div>
</div>
