<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تعديل: <?= e($discipline->name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/disciplines/<?= (int) $discipline->id ?>" class="btn btn-outline"><i data-lucide="eye" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> عرض</a>
<a href="/disciplines" 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'); ?>

<?php
$requiresMedical = !empty($config['requires_medical_cert']);
$guardianAge = $config['requires_guardian_consent_under'] ?? '';
$maxPlayers = $config['max_players_per_session'] ?? '';
?>

<form method="POST" action="/disciplines/<?= (int) $discipline->id ?>" id="disciplineForm">
    <?= csrf_field() ?>

    <!-- 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:#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="name_ar" value="<?= e(old('name_ar') ?: $discipline->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') ?: ($discipline->name_en ?? '')) ?>" class="form-input" id="nameEn" 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>
                    <input type="text" name="code" value="<?= e(old('code') ?: $discipline->code) ?>" class="form-input" id="codeInput" style="direction:ltr;text-align:left;text-transform:uppercase;">
                </div>
                <div class="form-group">
                    <label class="form-label">الفئة <span style="color:#DC2626;">*</span></label>
                    <select name="category" class="form-select" required>
                        <option value="">-- اختر الفئة --</option>
                        <?php foreach ($categories as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= (old('category') ?: $discipline->category) === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">ترتيب العرض</label>
                    <input type="number" name="sort_order" value="<?= e(old('sort_order') ?: (string)($discipline->sort_order ?? 0)) ?>" class="form-input" min="0" style="direction:ltr;text-align:left;">
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">الأيقونة</label>
                    <select name="icon" class="form-select" id="iconSelect">
                        <?php
                        $icons = [
                            'activity' => 'Activity (نشاط)',
                            'trophy' => 'Trophy (كأس)',
                            'target' => 'Target (هدف)',
                            'zap' => 'Zap (طاقة)',
                            'flame' => 'Flame (شعلة)',
                            'heart' => 'Heart (قلب)',
                            'star' => 'Star (نجمة)',
                            'award' => 'Award (جائزة)',
                            'shield' => 'Shield (درع)',
                            'swords' => 'Swords (سيوف)',
                            'bike' => 'Bike (دراجة)',
                            'dumbbell' => 'Dumbbell (دمبل)',
                            'waves' => 'Waves (أمواج)',
                            'wind' => 'Wind (رياح)',
                            'mountain' => 'Mountain (جبل)',
                            'flag' => 'Flag (علم)',
                            'timer' => 'Timer (مؤقت)',
                            'gauge' => 'Gauge (مقياس)',
                            'volleyball' => 'Volleyball (كرة طائرة)',
                            'dribbble' => 'Ball (كرة)',
                            'circle-dot' => 'Circle Dot (دائرة)',
                            'move' => 'Move (حركة)',
                            'grab' => 'Grab (قبضة)',
                            'footprints' => 'Footprints (خطوات)',
                        ];
                        $selectedIcon = old('icon') ?: ($discipline->icon ?? 'activity');
                        foreach ($icons as $iconKey => $iconLabel):
                        ?>
                        <option value="<?= e($iconKey) ?>" <?= $selectedIcon === $iconKey ? 'selected' : '' ?>><?= e($iconLabel) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group" style="display:flex;align-items:end;gap:10px;">
                    <div id="iconPreview" style="width:48px;height:48px;border-radius:10px;background:#F3F4F6;display:flex;align-items:center;justify-content:center;">
                        <i data-lucide="<?= e($selectedIcon) ?>" style="width:24px;height:24px;color:#0D7377;" id="iconPreviewIcon"></i>
                    </div>
                    <span style="font-size:12px;color:#9CA3AF;">معاينة الأيقونة</span>
                </div>
            </div>
            <div class="form-group" style="margin-top:15px;">
                <label class="form-label">وصف النشاط</label>
                <textarea name="description_ar" class="form-input" rows="3"><?= e(old('description_ar') ?: ($discipline->description_ar ?? '')) ?></textarea>
            </div>
        </div>
    </div>

    <!-- Additional 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:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding:12px;border:1px solid #E5E7EB;border-radius:8px;">
                        <input type="checkbox" name="requires_medical_cert" value="1" <?= $requiresMedical ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#0D7377;">
                        <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>
                    <input type="number" name="requires_guardian_consent_under" value="<?= e((string) $guardianAge) ?>" class="form-input" min="0" max="21" placeholder="مثال: 18" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">الحد الأقصى للاعبين في الحصة</label>
                    <input type="number" name="max_players_per_session" value="<?= e((string) $maxPlayers) ?>" class="form-input" min="1" placeholder="مثال: 20" style="direction:ltr;text-align:left;">
                </div>
            </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="/disciplines/<?= (int) $discipline->id ?>" 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();
    }

    // Icon preview
    var iconSelect = document.getElementById('iconSelect');
    if (iconSelect) {
        iconSelect.addEventListener('change', function() {
            var preview = document.getElementById('iconPreviewIcon');
            if (preview) {
                preview.setAttribute('data-lucide', this.value);
                if (typeof lucide !== 'undefined') {
                    lucide.createIcons();
                }
            }
        });
    }
});


</script>

<?php $__template->endSection(); ?>
