<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تعديل المجموعة: <?= e($group->name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/groups/<?= (int) $group->id ?>" 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/<?= (int) $group->id ?>/update">
    <?= 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', $group->code)) ?>" class="form-input" required maxlength="30" 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', $group->name_ar)) ?>" class="form-input" required maxlength="300">
                </div>
                <div class="form-group">
                    <label class="form-label">الاسم بالإنجليزي</label>
                    <input type="text" name="name_en" value="<?= e(old('name_en', $group->name_en ?? '')) ?>" class="form-input" maxlength="300" 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'] ?>" <?= ($disciplineId ?? null) == $d['id'] ? 'selected' : '' ?>><?= 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', $group->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 data-searchable="true" data-placeholder="-- اختر المدرب --">
                        <option value="">-- اختر المدرب --</option>
                        <?php foreach ($coaches as $c): ?>
                        <option value="<?= (int) $c['id'] ?>" <?= (old('coach_id', $group->coach_id)) == $c['id'] ? 'selected' : '' ?>><?= e($c['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>
    </div>

    <!-- Inherited Pricing & Capacity (read-only from program) -->
    <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="info" style="width:18px;height:18px;color:#6B7280;"></i>
            <h3 style="margin:0;color:#6B7280;font-size:15px;">السعة والرسوم (موروثة من البرنامج)</h3>
        </div>
        <div style="padding:20px;background:#F9FAFB;">
            <div id="inherited-pricing" style="font-size:13px;color:#6B7280;">
                جاري التحميل...
            </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', $group->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', $group->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/<?= (int) $group->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();

    var disciplineEl = document.getElementById('discipline_id');
    var programEl = document.getElementById('program_id');
    var coachEl = document.getElementById('coach_id');
    var currentProgramId = '<?= (int) old('program_id', $group->program_id) ?>';
    var currentCoachId = '<?= (int) old('coach_id', $group->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, currentProgramId);
            rebuildSelect(coachEl, allCoaches, currentCoachId);
            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, currentProgramId);
            });
        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, currentCoachId);
            });
    });

    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;
    }

    // Inherited pricing display
    var pricingPanel = document.getElementById('inherited-pricing');
    function loadProgramPricing(pid) {
        if (!pid) {
            pricingPanel.innerHTML = 'اختر البرنامج أولاً لعرض الرسوم والسعة الموروثة';
            return;
        }
        fetch('/api/sa/programs/' + pid + '/pricing')
            .then(function(r) { return r.json(); })
            .then(function(data) {
                if (data.success) {
                    var p = data.pricing;
                    pricingPanel.innerHTML = '<div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:16px;">'
                        + '<div><span style="color:#9CA3AF;">رسوم أعضاء:</span> <strong>' + p.monthly_fee_member + '</strong></div>'
                        + '<div><span style="color:#9CA3AF;">رسوم غير أعضاء:</span> <strong>' + p.monthly_fee_nonmember + '</strong></div>'
                        + '<div><span style="color:#9CA3AF;">أدنى سعة:</span> <strong>' + p.min_capacity + '</strong></div>'
                        + '<div><span style="color:#9CA3AF;">أقصى سعة:</span> <strong>' + p.max_capacity + '</strong></div>'
                        + '</div>';
                }
            });
    }
    programEl.addEventListener('change', function() { loadProgramPricing(this.value); });
    if (currentProgramId) loadProgramPricing(currentProgramId);
});
</script>
<?php $__template->endSection(); ?>
