<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تقرير أرصدة المخزون<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/inventory/reports/stock-balance" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">المخزن</label>
            <select name="warehouse_id" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($warehouses as $wh): ?>
                <option value="<?= (int) $wh['id'] ?>" <?= ($warehouseId ?? '') == $wh['id'] ? 'selected' : '' ?>><?= e($wh['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">التصنيف</label>
            <select name="category_id" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($categories as $cat): ?>
                <option value="<?= (int) $cat['id'] ?>" <?= ($categoryId ?? '') == $cat['id'] ? 'selected' : '' ?>><?= e($cat['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> عرض</button>
        <a href="/inventory/reports/stock-balance" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Total Value Card -->
<div class="card" style="margin-bottom:20px;padding:20px;text-align:center;background:linear-gradient(135deg, #0D737710, #0D737720);">
    <div style="font-size:14px;color:#6B7280;margin-bottom:6px;">إجمالي قيمة المخزون</div>
    <div style="font-size:36px;font-weight:800;color:#0D7377;direction:ltr;"><?= money($totalValue ?? 0) ?></div>
</div>

<!-- Stock Balance Table -->
<?php if (!empty($rows)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>SKU</th>
                    <th>الصنف</th>
                    <th>المخزن</th>
                    <th>الكمية</th>
                    <th>الحد الأدنى</th>
                    <th>الحد الأقصى</th>
                    <th>الوحدة</th>
                    <th>سعر التكلفة</th>
                    <th>قيمة المخزون</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): ?>
                <?php
                $qty = (float) ($row['quantity'] ?? 0);
                $minLvl = (float) ($row['min_level'] ?? 0);
                $maxLvl = (float) ($row['max_level'] ?? 0);
                // Row color based on stock level
                if ($qty <= 0) {
                    $qtyColor = '#DC2626';
                    $rowBg = '#FEF2F2';
                } elseif ($minLvl > 0 && $qty <= $minLvl) {
                    $qtyColor = '#D97706';
                    $rowBg = '#FFFBEB';
                } else {
                    $qtyColor = '#1A1A2E';
                    $rowBg = '';
                }
                ?>
                <tr style="<?= $rowBg ? 'background:' . $rowBg . ';' : '' ?>">
                    <td style="direction:ltr;text-align:left;font-family:monospace;font-weight:600;"><?= e($row['sku'] ?? '') ?></td>
                    <td style="font-weight:600;"><?= e($row['name_ar'] ?? '') ?></td>
                    <td><?= e($row['warehouse_name'] ?? '') ?></td>
                    <td style="font-weight:700;color:<?= $qtyColor ?>;direction:ltr;text-align:left;"><?= number_format($qty, 2) ?></td>
                    <td style="color:#6B7280;direction:ltr;text-align:left;"><?= $minLvl > 0 ? number_format($minLvl, 2) : '—' ?></td>
                    <td style="color:#6B7280;direction:ltr;text-align:left;"><?= $maxLvl > 0 ? number_format($maxLvl, 2) : '—' ?></td>
                    <td><?= e($row['unit_of_measure'] ?? '') ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;"><?= money($row['cost_price'] ?? 0) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:left;color:#0D7377;"><?= money($row['stock_value'] ?? 0) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="background:#F9FAFB;border-top:2px solid #E5E7EB;">
                    <td colspan="8" style="font-weight:700;font-size:15px;padding:12px 15px;">الإجمالي</td>
                    <td style="font-weight:800;font-size:16px;direction:ltr;text-align:left;color:#0D7377;padding:12px 15px;"><?= money($totalValue ?? 0) ?></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="bar-chart-3" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد بيانات</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0;">
        <?php if (!empty($warehouseId) || !empty($categoryId)): ?>
            لا توجد نتائج مطابقة. جرب تغيير معايير التصفية.
        <?php else: ?>
            لا توجد أرصدة مخزون مسجلة حاليا.
        <?php endif; ?>
    </p>
</div>
<?php endif; ?>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') {
        lucide.createIcons();
    }
});
</script>
<?php $__template->endSection(); ?>
