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

<?php $__template->section('page_actions'); ?>
<a href="/inventory/movements" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للحركات</a>
<?php $__template->endSection(); ?>

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

<form method="POST" action="/inventory/movements" id="movementForm">
    <?= csrf_field() ?>

    <!-- Card 1: Movement Data -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="arrow-left-right" style="width:18px;height:18px;color:#0D7377;"></i>
            <h3 style="margin:0;color:#0D7377;font-size:15px;">بيانات الحركة</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">الصنف <span style="color:#DC2626;">*</span></label>
                    <select name="item_id" id="item_id" class="form-select" required>
                        <option value="">-- اختر الصنف --</option>
                        <?php foreach ($items as $item): ?>
                        <option value="<?= (int) $item['id'] ?>" data-tracking="<?= e($item['tracking_type'] ?? 'standard') ?>" <?= old('item_id') == $item['id'] ? 'selected' : '' ?>>
                            <?= e($item['name_ar']) ?> (<?= e($item['sku']) ?>)
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">المخزن <span style="color:#DC2626;">*</span></label>
                    <select name="warehouse_id" class="form-select" required>
                        <option value="">-- اختر المخزن --</option>
                        <?php foreach ($warehouses as $wh): ?>
                        <option value="<?= (int) $wh['id'] ?>" <?= old('warehouse_id') == $wh['id'] ? 'selected' : '' ?>><?= e($wh['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">الاتجاه <span style="color:#DC2626;">*</span></label>
                    <select name="direction" id="direction" class="form-select" required>
                        <option value="">-- اختر الاتجاه --</option>
                        <option value="in" <?= old('direction') === 'in' ? 'selected' : '' ?>>وارد</option>
                        <option value="out" <?= old('direction') === 'out' ? 'selected' : '' ?>>صادر</option>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">نوع الحركة <span style="color:#DC2626;">*</span></label>
                    <select name="movement_type" id="movement_type" class="form-select" required>
                        <option value="">-- اختر الاتجاه أولاً --</option>
                    </select>
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">الكمية <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="quantity" value="<?= e(old('quantity', '')) ?>" class="form-input" step="0.001" min="0.001" required placeholder="0.000" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">تكلفة الوحدة</label>
                    <input type="number" name="unit_cost" value="<?= e(old('unit_cost', '')) ?>" class="form-input" step="0.01" min="0" placeholder="0.00" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">تاريخ الحركة <span style="color:#DC2626;">*</span></label>
                    <input type="date" name="movement_date" value="<?= e(old('movement_date', date('Y-m-d'))) ?>" class="form-input" required>
                </div>
            </div>
        </div>
    </div>

    <!-- Card 2: Batch Data (hidden by default) -->
    <div class="card" style="margin-bottom:20px;display:none;" id="batchCard">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="calendar-clock" style="width:18px;height:18px;color:#D97706;"></i>
            <h3 style="margin:0;color:#D97706;font-size:15px;">بيانات الدفعة (للأصناف ذات تاريخ صلاحية)</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">رقم الدفعة</label>
                    <input type="text" name="batch_number" value="<?= e(old('batch_number', '')) ?>" class="form-input" placeholder="مثال: BATCH-2026-001" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">تاريخ الصلاحية</label>
                    <input type="date" name="expiry_date" value="<?= e(old('expiry_date', '')) ?>" class="form-input">
                </div>
            </div>
        </div>
    </div>

    <!-- Notes -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:20px;">
            <div class="form-group" style="margin:0;">
                <label class="form-label">ملاحظات</label>
                <textarea name="notes" class="form-input" rows="3" placeholder="ملاحظات إضافية عن الحركة..."><?= e(old('notes', '')) ?></textarea>
            </div>
        </div>
    </div>

    <!-- Submit -->
    <div style="display:flex;gap:10px;justify-content:flex-start;">
        <button type="submit" class="btn btn-primary" style="padding:12px 30px;font-size:15px;">
            <i data-lucide="check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i>
            تسجيل الحركة
        </button>
        <a href="/inventory/movements" class="btn btn-outline" style="padding:12px 30px;font-size:15px;">إلغاء</a>
    </div>
</form>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') {
        lucide.createIcons();
    }

    // Items data with tracking types
    var itemsData = {};
    <?php foreach ($items as $item): ?>
    itemsData[<?= (int) $item['id'] ?>] = {
        tracking_type: '<?= e($item['tracking_type'] ?? 'standard') ?>'
    };
    <?php endforeach; ?>

    // Movement types by direction
    var movementTypesByDirection = {
        'in': {
            'purchase': 'شراء',
            'return_in': 'مرتجع وارد',
            'transfer_in': 'تحويل وارد',
            'adjustment_in': 'تسوية وارد',
            'opening_balance': 'رصيد افتتاحي'
        },
        'out': {
            'sale': 'بيع',
            'return_out': 'مرتجع صادر',
            'transfer_out': 'تحويل صادر',
            'adjustment_out': 'تسوية صادر',
            'damage': 'تالف',
            'consumption': 'استهلاك'
        }
    };

    var directionSelect = document.getElementById('direction');
    var movementTypeSelect = document.getElementById('movement_type');
    var itemSelect = document.getElementById('item_id');
    var batchCard = document.getElementById('batchCard');
    var oldMovementType = '<?= e(old('movement_type', '')) ?>';

    // Update movement types based on direction
    directionSelect.addEventListener('change', function() {
        var dir = this.value;
        movementTypeSelect.innerHTML = '';

        if (!dir) {
            movementTypeSelect.innerHTML = '<option value="">-- اختر الاتجاه أولاً --</option>';
            return;
        }

        movementTypeSelect.innerHTML = '<option value="">-- اختر نوع الحركة --</option>';
        var types = movementTypesByDirection[dir] || {};
        for (var key in types) {
            var opt = document.createElement('option');
            opt.value = key;
            opt.textContent = types[key];
            if (key === oldMovementType) {
                opt.selected = true;
                oldMovementType = '';
            }
            movementTypeSelect.appendChild(opt);
        }

        updateBatchVisibility();
    });

    // Show/hide batch card based on item tracking type and direction
    function updateBatchVisibility() {
        var selectedItemId = itemSelect.value;
        var dir = directionSelect.value;
        var item = itemsData[selectedItemId];

        if (item && item.tracking_type === 'expiry' && dir === 'in') {
            batchCard.style.display = '';
        } else {
            batchCard.style.display = 'none';
        }
    }

    itemSelect.addEventListener('change', updateBatchVisibility);

    // Trigger initial state if old values exist
    if (directionSelect.value) {
        directionSelect.dispatchEvent(new Event('change'));
    }
    updateBatchVisibility();
});
</script>
<?php $__template->endSection(); ?>
