<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= $warehouse ? 'تعديل المخزن: ' . e($warehouse->name_ar) : 'إنشاء مخزن جديد' ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/warehouses" 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="<?= $warehouse ? '/inventory/warehouses/' . (int) $warehouse->id : '/inventory/warehouses' ?>">
    <?= 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="warehouse" 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>
                    <input type="text" name="code" value="<?= e(old('code', $warehouse->code ?? '')) ?>" class="form-input" style="direction:ltr;text-align:left;text-transform:uppercase;" required>
                </div>
                <div class="form-group">
                    <label class="form-label">الاسم بالعربي <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="name_ar" value="<?= e(old('name_ar', $warehouse->name_ar ?? '')) ?>" class="form-input" required>
                </div>
                <div class="form-group">
                    <label class="form-label">الاسم بالإنجليزي</label>
                    <input type="text" name="name_en" value="<?= e(old('name_en', $warehouse->name_en ?? '')) ?>" class="form-input" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">نوع المخزن <span style="color:#DC2626;">*</span></label>
                    <select name="warehouse_type" class="form-select" required>
                        <option value="">-- اختر النوع --</option>
                        <?php foreach ($warehouseTypes as $typeKey => $typeLabel): ?>
                        <option value="<?= e($typeKey) ?>" <?= old('warehouse_type', $warehouse->warehouse_type ?? '') === $typeKey ? 'selected' : '' ?>><?= e($typeLabel) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group" style="grid-column:1/-1;">
                    <label class="form-label">الموقع</label>
                    <input type="text" name="location_ar" value="<?= e(old('location_ar', $warehouse->location_ar ?? '')) ?>" class="form-input" placeholder="مثال: المبنى الرئيسي - الدور الأرضي">
                </div>
            </div>
        </div>
    </div>

    <!-- بيانات إضافية -->
    <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="settings" style="width:18px;height:18px;color:#6B7280;"></i>
            <h3 style="margin:0;color:#6B7280;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>
                    <select name="keeper_employee_id" class="form-select">
                        <option value="">-- بدون --</option>
                        <?php foreach ($employees as $emp): ?>
                        <option value="<?= (int) $emp['id'] ?>" <?= old('keeper_employee_id', $warehouse->keeper_employee_id ?? '') == $emp['id'] ? 'selected' : '' ?>><?= e($emp['full_name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">السعة (وحدات)</label>
                    <input type="number" name="capacity_units" value="<?= e(old('capacity_units', $warehouse->capacity_units ?? '')) ?>" class="form-input" min="0">
                </div>
                <div class="form-group">
                    <label class="form-label">الهاتف</label>
                    <input type="text" name="phone" value="<?= e(old('phone', $warehouse->phone ?? '')) ?>" class="form-input" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group" style="display:flex;align-items:center;gap:10px;padding-top:28px;">
                    <label style="display:flex;align-items:center;gap:8px;cursor:pointer;font-size:14px;">
                        <input type="hidden" name="is_active" value="0">
                        <input type="checkbox" name="is_active" value="1" <?= old('is_active', $warehouse->is_active ?? '1') == '1' ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#0D7377;">
                        نشط
                    </label>
                </div>
                <div class="form-group" style="grid-column:1/-1;">
                    <label class="form-label">ملاحظات</label>
                    <textarea name="notes" class="form-textarea" rows="3" placeholder="ملاحظات إضافية..."><?= e(old('notes', $warehouse->notes ?? '')) ?></textarea>
                </div>
            </div>
        </div>
    </div>

    <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary">
            <i data-lucide="<?= $warehouse ? 'check' : 'plus' ?>" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i>
            <?= $warehouse ? 'حفظ التعديلات' : 'إنشاء المخزن' ?>
        </button>
        <a href="/inventory/warehouses" class="btn btn-outline">إلغاء</a>
    </div>
</form>

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