<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>حركات المخزون<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/movements/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حركة جديدة</a>
<?php $__template->endSection(); ?>

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

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/inventory/movements" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:160px;">
            <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'] ?>" <?= ($filters['warehouse_id'] ?? '') == $wh['id'] ? 'selected' : '' ?>><?= e($wh['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">نوع الحركة</label>
            <select name="movement_type" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($movementTypes as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['movement_type'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الاتجاه</label>
            <select name="direction" class="form-select">
                <option value="">الكل</option>
                <option value="in" <?= ($filters['direction'] ?? '') === 'in' ? 'selected' : '' ?>>وارد</option>
                <option value="out" <?= ($filters['direction'] ?? '') === 'out' ? 'selected' : '' ?>>صادر</option>
            </select>
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">من تاريخ</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </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/movements" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Movements Table -->
<?php if (!empty($movements)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>التاريخ</th>
                    <th>الصنف</th>
                    <th>المخزن</th>
                    <th>النوع</th>
                    <th>الاتجاه</th>
                    <th>الكمية</th>
                    <th>التكلفة</th>
                    <th>ملاحظات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($movements as $mv): ?>
                <tr>
                    <td style="white-space:nowrap;font-size:13px;color:#6B7280;"><?= e($mv['movement_date'] ?? '') ?></td>
                    <td>
                        <div style="font-weight:600;"><?= e($mv['item_name'] ?? '') ?></div>
                        <?php if (!empty($mv['sku'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;direction:ltr;text-align:right;">
                            <code style="font-size:11px;background:#F3F4F6;padding:1px 6px;border-radius:4px;"><?= e($mv['sku']) ?></code>
                        </div>
                        <?php endif; ?>
                    </td>
                    <td><?= e($mv['warehouse_name'] ?? '') ?></td>
                    <td>
                        <span style="font-size:12px;font-weight:600;"><?= e($movementTypes[$mv['movement_type'] ?? ''] ?? ($mv['movement_type'] ?? '')) ?></span>
                    </td>
                    <td>
                        <?php if (($mv['direction'] ?? '') === 'in'): ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#ECFDF5;color:#059669;">وارد</span>
                        <?php else: ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#FEE2E2;color:#DC2626;">صادر</span>
                        <?php endif; ?>
                    </td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= e($mv['quantity'] ?? 0) ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;">
                        <?php if (!empty($mv['total_cost'])): ?>
                        <?= e(number_format((float) $mv['total_cost'], 2)) ?>
                        <?php if (!empty($mv['unit_cost'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;">@ <?= e(number_format((float) $mv['unit_cost'], 2)) ?></div>
                        <?php endif; ?>
                        <?php else: ?>
                        —
                        <?php endif; ?>
                    </td>
                    <td style="font-size:13px;color:#6B7280;max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">
                        <?= e($mv['notes'] ?? '—') ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="padding:15px;">
    <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
</div>
<?php endif; ?>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="arrow-left-right" 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;">
        <?php if (!empty($filters['warehouse_id']) || !empty($filters['movement_type']) || !empty($filters['direction']) || !empty($filters['date_from']) || !empty($filters['date_to'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            لم يتم تسجيل أي حركات مخزون بعد.
        <?php endif; ?>
    </p>
    <a href="/inventory/movements/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(); ?>
