<?php
// view: pages/stock_report
?>
<?php
  $rows = $rows ?? [];
  $warehouses = $warehouses ?? [];
  $warehouse_id = (int)($warehouse_id ?? 1);
  $warehouse_name = $warehouse_name ?? 'المخزن';
  $only_nonzero = (int)($only_nonzero ?? 0);
?>

<div class="card border-0 shadow-sm mb-3" style="border-radius: 16px;">
  <div class="card-body py-3 px-3">
    <div class="d-flex flex-wrap justify-content-between align-items-center gap-3">
      <div>
        <h4 class="mb-0">تقرير المخزون</h4>
        <div class="text-muted small">المخزن: <?= e($warehouse_name) ?></div>
      </div>

      <div class="d-flex flex-wrap align-items-center gap-2">
        <form method="get" class="d-flex flex-wrap align-items-center gap-2 mb-0">
          <label class="small text-muted mb-0">اختر المخزن</label>
          <select name="warehouse_id" class="form-select form-select-sm" style="min-width: 240px" onchange="this.form.submit()">
            <?php foreach ($warehouses as $w): ?>
              <option value="<?= (int)$w['id'] ?>" <?= ((int)$w['id'] === $warehouse_id) ? 'selected' : '' ?>><?= e($w['name']) ?></option>
            <?php endforeach; ?>
          </select>

          <label class="form-check form-check-inline mb-0 me-2">
            <input class="form-check-input" type="checkbox" name="only_nonzero" value="1" <?= $only_nonzero === 1 ? 'checked' : '' ?> onchange="this.form.submit()">
            <span class="form-check-label small">إخفاء الأصناف صفر</span>
          </label>
          <noscript><button class="btn btn-sm btn-primary">عرض</button></noscript>
        </form>

        <div class="btn-group" role="group">
          <a class="btn btn-sm btn-primary" target="_blank" href="<?= e(url('/reports/inventory/stock/print?warehouse_id=' . (int)$warehouse_id . '&only_nonzero=' . (int)$only_nonzero . '&format=a4')) ?>">🖨 طباعة</a>
          <a class="btn btn-sm btn-outline-danger" href="<?= e(url('/reports/inventory/stock/pdf?warehouse_id=' . (int)$warehouse_id . '&only_nonzero=' . (int)$only_nonzero)) ?>">PDF</a>
          <a class="btn btn-sm btn-outline-success" href="<?= e(url('/reports/inventory/stock/export?warehouse_id=' . (int)$warehouse_id . '&only_nonzero=' . (int)$only_nonzero)) ?>">Excel</a>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="card p-3">
  <div class="table-responsive">
    <div class="table-responsive"><table class="table table-bordered table-hover table-striped align-middle table-sm align-middle mb-0" style="table-layout: fixed;">
      <thead class="table-light">
        <tr>
          <th style="width:60px">#</th>
          <th>الصنف</th>
          <th style="width:150px">باركود</th>
          <th class="text-end" style="width:95px">رصيد</th>
          <th class="text-end" style="width:95px">تكلفة</th>
          <th class="text-end" style="width:95px">سعر</th>
          <th class="text-end" style="width:120px">قيمة تكلفة</th>
        </tr>
      </thead>
      <tbody>
        <?php 
          $total_cost_value = 0;
          foreach ($rows as $r):
            $cost_value = (float)$r['qty'] * (float)$r['cost'];
            $total_cost_value += $cost_value;
        ?>
          <tr>
            <td><?= (int)$r['id'] ?></td>
            <td><?= e($r['name']) ?></td>
            <td><?= e($r['barcode'] ?? '-') ?></td>
            <td class="text-end"><?= format_qty($r['qty']) ?></td>
            <td class="text-end"><?= format_money($r['cost']) ?></td>
            <td class="text-end"><?= format_money($r['price']) ?></td>
            <td class="text-end"><?= format_money($cost_value) ?></td>
          </tr>
        <?php endforeach; ?>
        <?php if (!$rows): ?>
          <tr><td colspan="7" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
        <?php endif; ?>
      </tbody>
      <tfoot class="table-light">
        <tr>
          <th colspan="6" class="text-end">إجمالي قيمة التكلفة</th>
          <th class="text-end"><?= format_money($total_cost_value) ?></th>
        </tr>
      </tfoot>
    </table></div>
  </div>
</div>
