<?php
$isEdit = isset($category) && $category !== null;
$pageTitle = $isEdit ? 'تعديل التصنيف: ' . e($category->name_ar) : 'إضافة تصنيف جديد';
$formAction = $isEdit ? '/inventory/categories/' . (int) $category->id : '/inventory/categories';
?>
<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= $pageTitle ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/inventory/categories" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;"></i> رجوع</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<form method="POST" action="<?= $formAction ?>">
    <?= csrf_field() ?>
    <div class="card" style="padding:20px;margin-bottom:20px;">
        <h3 style="margin:0 0 20px;font-size:16px;font-weight:700;color:#1F2937;border-bottom:1px solid #E5E7EB;padding-bottom:12px;">
            <i data-lucide="tag" style="width:18px;height:18px;vertical-align:middle;color:#0D7377;"></i>
            بيانات التصنيف
        </h3>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
            <!-- Parent Category -->
            <div class="form-group">
                <label class="form-label">التصنيف الرئيسي</label>
                <select name="parent_id" class="form-select">
                    <option value="">— تصنيف رئيسي —</option>
                    <?php foreach ($roots as $root): ?>
                        <option value="<?= (int) $root['id'] ?>" <?= (string) e(old('parent_id', $category->parent_id ?? '')) === (string) $root['id'] ? 'selected' : '' ?>><?= e($root['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <!-- Code -->
            <div class="form-group">
                <label class="form-label">الكود <span style="color:#DC2626;">*</span></label>
                <input type="text" name="code" value="<?= e(old('code', $category->code ?? '')) ?>" class="form-input" required style="direction:ltr;text-align:right;text-transform:uppercase;">
            </div>

            <!-- Name AR -->
            <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', $category->name_ar ?? '')) ?>" class="form-input" required>
            </div>

            <!-- Name EN -->
            <div class="form-group">
                <label class="form-label">الاسم بالإنجليزي</label>
                <input type="text" name="name_en" value="<?= e(old('name_en', $category->name_en ?? '')) ?>" class="form-input" style="direction:ltr;text-align:right;">
            </div>

            <!-- Description AR -->
            <div class="form-group" style="grid-column:1/-1;">
                <label class="form-label">الوصف</label>
                <textarea name="description_ar" class="form-textarea" rows="3"><?= e(old('description_ar', $category->description_ar ?? '')) ?></textarea>
            </div>

            <!-- Sort Order -->
            <div class="form-group">
                <label class="form-label">ترتيب العرض</label>
                <input type="number" name="sort_order" value="<?= e(old('sort_order', $category->sort_order ?? '0')) ?>" class="form-input" min="0">
            </div>

            <!-- Is Active -->
            <div class="form-group" style="display:flex;align-items:end;">
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding-bottom:8px;">
                    <input type="hidden" name="is_active" value="0">
                    <input type="checkbox" name="is_active" value="1" <?= old('is_active', $category->is_active ?? '1') ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#0D7377;cursor:pointer;">
                    <span class="form-label" style="margin:0;">نشط</span>
                </label>
            </div>
        </div>
    </div>

    <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary">
            <i data-lucide="<?= $isEdit ? 'save' : 'plus-circle' ?>" style="width:16px;height:16px;"></i>
            <?= $isEdit ? 'حفظ التعديلات' : 'إضافة التصنيف' ?>
        </button>
        <a href="/inventory/categories" class="btn btn-outline">إلغاء</a>
    </div>
</form>

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