<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>أرصدة افتتاحية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('inventory.manage')): ?>
<a href="/inventory/opening-balances/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> رصيد افتتاحي جديد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<!-- Warehouse Filter -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/inventory/opening-balances" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:200px;">
            <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'] ?>" <?= ($warehouse_id ?? '') == $wh['id'] ? 'selected' : '' ?>><?= e($wh['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>
    </form>
</div>

<!-- Balances Table -->
<?php if (!empty($balances)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>اسم الصنف</th>
                    <th>SKU</th>
                    <th>المخزن</th>
                    <th>الكمية</th>
                    <th>تكلفة الوحدة</th>
                    <th>إجمالي التكلفة</th>
                    <th>تاريخ الرصيد</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($balances as $bal): ?>
                <tr>
                    <td style="font-weight:600;"><?= e($bal['item_name']) ?></td>
                    <td><code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($bal['sku']) ?></code></td>
                    <td><?= e($bal['warehouse_name']) ?></td>
                    <td><?= e($bal['quantity']) ?></td>
                    <td style="direction:ltr;text-align:left;"><?= money($bal['unit_cost'] ?? 0) ?></td>
                    <td style="direction:ltr;text-align:left;font-weight:600;"><?= money($bal['total_cost'] ?? 0) ?></td>
                    <td><?= e($bal['balance_date'] ?? '—') ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="archive" 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 0 20px;">ابدأ بإدخال الأرصدة الافتتاحية للأصناف في المخازن.</p>
    <a href="/inventory/opening-balances/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> رصيد افتتاحي جديد</a>
</div>
<?php endif; ?>

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