<?php
$isEdit = $item !== null;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?><?= $isEdit ? 'تعديل الصنف' : 'إضافة صنف جديد' ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($isEdit): ?>
<a href="/inventory/items/<?= (int) $item->id ?>" class="btn btn-outline"><i data-lucide="eye" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> عرض الصنف</a>
<?php endif; ?>
<a href="/inventory/items" 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="<?= $isEdit ? '/inventory/items/' . (int) $item->id : '/inventory/items' ?>" id="itemForm">
    <?= csrf_field() ?>

    <!-- Card 1: Basic Information -->
    <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="file-text" 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 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">كود الصنف (SKU) <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="sku" value="<?= e(old('sku', $item->sku ?? '')) ?>" class="form-input" required placeholder="مثال: ITM-001" style="direction:ltr;text-align:left;text-transform:uppercase;">
                </div>
                <div class="form-group">
                    <label class="form-label">الباركود</label>
                    <input type="text" name="barcode" value="<?= e(old('barcode', $item->barcode ?? '')) ?>" class="form-input" placeholder="باركود الصنف" style="direction:ltr;text-align:left;">
                </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', $item->name_ar ?? '')) ?>" class="form-input" required placeholder="اسم الصنف بالعربي">
                </div>
                <div class="form-group">
                    <label class="form-label">الاسم بالإنجليزي</label>
                    <input type="text" name="name_en" value="<?= e(old('name_en', $item->name_en ?? '')) ?>" class="form-input" placeholder="Item name in English" style="direction:ltr;text-align:left;">
                </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">التصنيف</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', $item->category_id ?? '') === (int) $cat['id'] && old('category_id', $item->category_id ?? '') !== '' ? 'selected' : '' ?>><?= e($cat['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">وحدة القياس <span style="color:#DC2626;">*</span></label>
                    <select name="unit_of_measure" class="form-select" required>
                        <?php foreach ($units as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= old('unit_of_measure', $item->unit_of_measure ?? 'piece') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">نوع التتبع <span style="color:#DC2626;">*</span></label>
                    <select name="tracking_type" class="form-select" required>
                        <?php foreach ($trackingTypes as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= old('tracking_type', $item->tracking_type ?? 'standard') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="form-group" style="margin-top:15px;">
                <label class="form-label">وصف الصنف</label>
                <textarea name="description_ar" class="form-input" rows="3" placeholder="وصف تفصيلي للصنف..."><?= e(old('description_ar', $item->description_ar ?? '')) ?></textarea>
            </div>
        </div>
    </div>

    <!-- Card 2: Pricing -->
    <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="banknote" style="width:18px;height:18px;color:#059669;"></i>
            <h3 style="margin:0;color:#059669;font-size:15px;">الأسعار</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">سعر التكلفة</label>
                    <input type="number" name="cost_price" value="<?= e(old('cost_price', $item->cost_price ?? '0.00')) ?>" 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">سعر البيع (عضو)</label>
                    <input type="number" name="sale_price_member" value="<?= e(old('sale_price_member', $item->sale_price_member ?? '0.00')) ?>" 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">سعر البيع (غير عضو)</label>
                    <input type="number" name="sale_price_nonmember" value="<?= e(old('sale_price_nonmember', $item->sale_price_nonmember ?? '0.00')) ?>" 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">سعر البيع (لاعب)</label>
                    <input type="number" name="sale_price_player" value="<?= e(old('sale_price_player', $item->sale_price_player ?? '')) ?>" class="form-input" step="0.01" min="0" placeholder="0.00" style="direction:ltr;text-align:left;">
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 3fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">نسبة الضريبة (%)</label>
                    <input type="number" name="tax_rate" value="<?= e(old('tax_rate', $item->tax_rate ?? '14.00')) ?>" class="form-input" step="0.01" min="0" max="100" placeholder="14.00" style="direction:ltr;text-align:left;">
                </div>
            </div>
        </div>
    </div>

    <!-- Card 3: Settings -->
    <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:#D97706;"></i>
            <h3 style="margin:0;color:#D97706;font-size:15px;">إعدادات</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:flex;gap:30px;margin-bottom:20px;">
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding:12px 16px;border:1px solid #E5E7EB;border-radius:8px;">
                    <input type="hidden" name="is_sellable" value="0">
                    <input type="checkbox" name="is_sellable" value="1" <?= (int) old('is_sellable', $item->is_sellable ?? 1) ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#1e3a5f;">
                    <div>
                        <div style="font-size:13px;font-weight:600;color:#1A1A2E;">متاح للبيع</div>
                        <div style="font-size:11px;color:#9CA3AF;">يظهر في شاشة البيع ونقاط البيع</div>
                    </div>
                </label>
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding:12px 16px;border:1px solid #E5E7EB;border-radius:8px;">
                    <input type="hidden" name="is_active" value="0">
                    <input type="checkbox" name="is_active" value="1" <?= (int) old('is_active', $item->is_active ?? 1) ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#1e3a5f;">
                    <div>
                        <div style="font-size:13px;font-weight:600;color:#1A1A2E;">فعّال</div>
                        <div style="font-size:11px;color:#9CA3AF;">الصنف نشط ويمكن استخدامه</div>
                    </div>
                </label>
            </div>
            <div class="form-group">
                <label class="form-label">ملاحظات</label>
                <textarea name="notes" class="form-input" rows="3" placeholder="ملاحظات إضافية..."><?= e(old('notes', $item->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>
            <?= $isEdit ? 'حفظ التعديلات' : 'إضافة الصنف' ?>
        </button>
        <a href="/inventory/items" 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();
    }
});
</script>
<?php $__template->endSection(); ?>
