<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إضافة مجموعة تدريبية جديدة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/groups" 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="/sa/groups">
    <?= 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="users" 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 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')) ?>" class="form-input" required maxlength="30" placeholder="مثال: GRP-SWIM-01" style="direction:ltr;text-align:left;text-transform:uppercase;">
                </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')) ?>" class="form-input" required maxlength="300" placeholder="مثال: مجموعة السباحة أ">
                </div>
                <div class="form-group">
                    <label class="form-label">الاسم بالإنجليزي</label>
                    <input type="text" name="name_en" value="<?= e(old('name_en')) ?>" class="form-input" maxlength="300" placeholder="e.g. Swimming Group A" 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 id="discipline_id" class="form-select">
                        <option value="">-- كل الأنشطة --</option>
                        <?php foreach ($disciplines as $d): ?>
                        <option value="<?= (int) $d['id'] ?>"><?= e($d['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <small style="color:#6B7280;font-size:11px;margin-top:4px;display:block;">اختياري — لتصفية البرامج والمدربين</small>
                </div>
                <div class="form-group">
                    <label class="form-label">البرنامج <span style="color:#DC2626;">*</span></label>
                    <select name="program_id" id="program_id" class="form-select" required>
                        <option value="">-- اختر البرنامج --</option>
                        <?php foreach ($programs as $p): ?>
                        <option value="<?= (int) $p['id'] ?>" <?= old('program_id', $_GET['program_id'] ?? '') == $p['id'] ? 'selected' : '' ?>><?= e($p['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">المدرب <span style="color:#DC2626;">*</span></label>
                    <select name="coach_id" id="coach_id" class="form-select" required>
                        <option value="">-- اختر المدرب --</option>
                        <?php foreach ($coaches as $c): ?>
                        <option value="<?= (int) $c['id'] ?>" <?= old('coach_id') == $c['id'] ? 'selected' : '' ?>><?= e($c['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>
    </div>

    <!-- Capacity & Fees -->
    <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:#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 1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">الحد الأدنى <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="min_capacity" value="<?= e(old('min_capacity', '5')) ?>" class="form-input" required min="1" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">الحد الأقصى <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="max_capacity" value="<?= e(old('max_capacity', '20')) ?>" class="form-input" required min="1" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">رسوم شهرية (أعضاء) <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="monthly_fee_member" value="<?= e(old('monthly_fee_member', '0')) ?>" class="form-input" required min="0" step="0.01" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">رسوم شهرية (غير أعضاء) <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="monthly_fee_nonmember" value="<?= e(old('monthly_fee_nonmember', '0')) ?>" class="form-input" required min="0" step="0.01" style="direction:ltr;text-align:left;">
                </div>
            </div>
        </div>
    </div>

    <!-- Season -->
    <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="calendar" 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">بداية الموسم</label>
                    <input type="date" name="season_start" value="<?= e(old('season_start')) ?>" class="form-input" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">نهاية الموسم</label>
                    <input type="date" name="season_end" value="<?= e(old('season_end')) ?>" class="form-input" style="direction:ltr;text-align:left;">
                </div>
            </div>
        </div>
    </div>

    <!-- Submit -->
    <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="/sa/groups" 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();

    var disciplineEl = document.getElementById('discipline_id');
    var programEl = document.getElementById('program_id');
    var coachEl = document.getElementById('coach_id');
    var allPrograms = <?= json_encode(array_map(fn($p) => ['id' => (int)$p['id'], 'name_ar' => $p['name_ar']], $programs)) ?>;
    var allCoaches = <?= json_encode(array_map(fn($c) => ['id' => (int)$c['id'], 'name_ar' => $c['name_ar']], $coaches)) ?>;

    disciplineEl.addEventListener('change', function() {
        var did = this.value;
        if (!did) {
            rebuildSelect(programEl, allPrograms, '');
            rebuildSelect(coachEl, allCoaches, '');
            return;
        }
        fetch('/api/sa/activities/programs-by-discipline?discipline_id=' + did)
            .then(function(r) { return r.json(); })
            .then(function(data) {
                if (data.success) rebuildSelect(programEl, data.programs, '');
            });
        fetch('/api/sa/activities/coaches-by-discipline?discipline_id=' + did)
            .then(function(r) { return r.json(); })
            .then(function(data) {
                if (data.success) rebuildSelect(coachEl, data.coaches, '');
            });
    });

    function rebuildSelect(el, items, selectedId) {
        var placeholder = el === programEl ? '-- اختر البرنامج --' : '-- اختر المدرب --';
        var html = '<option value="">' + placeholder + '</option>';
        items.forEach(function(item) {
            var sel = String(item.id) === String(selectedId) ? ' selected' : '';
            html += '<option value="' + item.id + '"' + sel + '>' + escHtml(item.name_ar) + '</option>';
        });
        el.innerHTML = html;
    }

    function escHtml(str) {
        var d = document.createElement('div');
        d.textContent = str || '';
        return d.innerHTML;
    }
});
</script>
<?php $__template->endSection(); ?>
