<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إنشاء جرد جديد<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/audits" 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/audits" id="auditForm">
    <?= csrf_field() ?>

    <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="clipboard-list" style="width:18px;height:18px;color:#1e3a5f;"></i>
            <h3 style="margin:0;color:#1e3a5f;font-size:15px;">بيانات الجرد</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;">
                <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'] ?>" <?= (int) old('warehouse_id') === (int) $wh['id'] && old('warehouse_id') !== '' ? 'selected' : '' ?>><?= e($wh['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">نوع الجرد <span style="color:#DC2626;">*</span></label>
                    <select name="audit_type" id="auditType" class="form-select" required>
                        <?php foreach ($auditTypes as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= old('audit_type', 'full') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group" id="categoryGroup" style="display:none;">
                    <label class="form-label">التصنيف</label>
                    <select name="category_id" class="form-select">
                        <option value="">-- كل التصنيفات --</option>
                        <?php foreach ($categories as $cat): ?>
                        <option value="<?= (int) $cat['id'] ?>" <?= (int) old('category_id') === (int) $cat['id'] && old('category_id') !== '' ? 'selected' : '' ?>><?= e($cat['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="form-group" style="margin-top:15px;">
                <label class="form-label">ملاحظات</label>
                <textarea name="notes" class="form-input" rows="3" placeholder="ملاحظات إضافية حول عملية الجرد..."><?= e(old('notes')) ?></textarea>
            </div>
        </div>
    </div>

    <!-- Submit Buttons -->
    <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/audits" 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();
    }

    var auditTypeSelect = document.getElementById('auditType');
    var categoryGroup = document.getElementById('categoryGroup');

    function toggleCategory() {
        categoryGroup.style.display = auditTypeSelect.value === 'partial' ? '' : 'none';
    }

    auditTypeSelect.addEventListener('change', toggleCategory);
    toggleCategory();
});
</script>
<?php $__template->endSection(); ?>
