<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إضافة قاعدة جدولة<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="padding:25px;max-width:700px;">
    <form method="POST" action="/scheduling/rules">
        <?= csrf_field() ?>

        <div class="form-group">
            <label class="form-label">اسم القاعدة</label>
            <input type="text" name="rule_name_ar" class="form-input" value="<?= old('rule_name_ar') ?>" required>
        </div>

        <div class="form-group">
            <label class="form-label">نوع القاعدة</label>
            <select name="rule_type" id="ruleType" class="form-input" required onchange="showConfig(this.value)">
                <option value="">— اختر —</option>
                <?php foreach ($ruleTypes as $key => $label): ?>
                <option value="<?= e($key) ?>"><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="form-group">
            <label class="form-label">ينطبق على</label>
            <select name="applies_to" class="form-input">
                <option value="all">الكل</option>
                <option value="coach">مدرب محدد</option>
                <option value="facility">مرفق محدد</option>
                <option value="player">لاعب محدد</option>
            </select>
        </div>

        <div class="form-group">
            <label class="form-label">رقم المعرّف (إن وجد)</label>
            <input type="number" name="applies_to_id" class="form-input" placeholder="اتركه فارغاً للتطبيق على الكل">
        </div>

        <!-- Dynamic Config Fields -->
        <div id="configCoachMaxHours" class="config-section" style="display:none;">
            <div class="form-group">
                <label class="form-label">أقصى ساعات يومياً</label>
                <input type="number" name="max_hours" class="form-input" value="8" min="1" max="16">
            </div>
        </div>

        <div id="configFacilityBuffer" class="config-section" style="display:none;">
            <div class="form-group">
                <label class="form-label">فترة البفر (دقيقة)</label>
                <input type="number" name="buffer_minutes" class="form-input" value="15" min="5" max="120">
            </div>
        </div>

        <div id="configMaxDailySessions" class="config-section" style="display:none;">
            <div class="form-group">
                <label class="form-label">أقصى عدد جلسات يومياً</label>
                <input type="number" name="max_sessions" class="form-input" value="3" min="1" max="10">
            </div>
        </div>

        <div id="configRestBetween" class="config-section" style="display:none;">
            <div class="form-group">
                <label class="form-label">فترة الراحة (دقيقة)</label>
                <input type="number" name="rest_minutes" class="form-input" value="30" min="10" max="120">
            </div>
        </div>

        <div id="configCoachDayOff" class="config-section" style="display:none;">
            <div class="form-group">
                <label class="form-label">يوم الإجازة</label>
                <select name="day_of_week" class="form-input">
                    <option value="0">الأحد</option>
                    <option value="1">الإثنين</option>
                    <option value="2">الثلاثاء</option>
                    <option value="3">الأربعاء</option>
                    <option value="4">الخميس</option>
                    <option value="5" selected>الجمعة</option>
                    <option value="6">السبت</option>
                </select>
            </div>
        </div>

        <div id="configFacilityMaintenance" class="config-section" style="display:none;">
            <div class="form-group">
                <label class="form-label">يوم الصيانة</label>
                <select name="maintenance_day" class="form-input">
                    <option value="0">الأحد</option>
                    <option value="1" selected>الإثنين</option>
                    <option value="2">الثلاثاء</option>
                    <option value="3">الأربعاء</option>
                    <option value="4">الخميس</option>
                    <option value="6">السبت</option>
                </select>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;">
                <div class="form-group"><label class="form-label">من</label><input type="time" name="maintenance_start" class="form-input" value="06:00"></div>
                <div class="form-group"><label class="form-label">إلى</label><input type="time" name="maintenance_end" class="form-input" value="10:00"></div>
            </div>
        </div>

        <div class="form-group">
            <label class="form-label">الأولوية (أقل = أهم)</label>
            <input type="number" name="priority" class="form-input" value="10" min="1" max="100">
        </div>

        <div style="display:flex;gap:10px;margin-top:20px;">
            <button type="submit" class="btn btn-primary">حفظ القاعدة</button>
            <a href="/scheduling/rules" class="btn btn-outline">إلغاء</a>
        </div>
    </form>
</div>

<script>
function showConfig(type) {
    document.querySelectorAll('.config-section').forEach(el => el.style.display = 'none');
    const map = {
        'coach_max_hours': 'configCoachMaxHours',
        'facility_buffer': 'configFacilityBuffer',
        'max_daily_sessions': 'configMaxDailySessions',
        'rest_between_sessions': 'configRestBetween',
        'coach_day_off': 'configCoachDayOff',
        'facility_maintenance': 'configFacilityMaintenance'
    };
    if (map[type]) document.getElementById(map[type]).style.display = 'block';
}
</script>
<?php $__template->endSection(); ?>
