<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تسعير الأكاديميات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/pricing" class="btn btn-outline"><i data-lucide="calculator" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تسعير المرافق</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(140px, 1fr));gap:12px;margin-bottom:20px;">
    <div class="card" style="padding:14px;text-align:center;">
        <div style="font-size:11px;color:#6B7280;">أكاديميات</div>
        <div style="font-size:22px;font-weight:800;"><?= count($academies) ?></div>
    </div>
    <div class="card" style="padding:14px;text-align:center;">
        <div style="font-size:11px;color:#6B7280;">كروت الحصص</div>
        <div style="font-size:22px;font-weight:800;"><?= count($sessionCards) ?></div>
    </div>
</div>

<!-- Academies List -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;font-weight:600;"><i data-lucide="building-2" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;"></i> قائمة أسعار الأكاديميات — موسم 2025/2026</h3>
    </div>
    <table style="width:100%;border-collapse:collapse;font-size:13px;">
        <thead>
            <tr style="background:#F9FAFB;">
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">الأكاديمية</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">التخصص</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">عدد المستويات</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">أقل سعر عضو</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">أعلى سعر عضو</th>
                <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">تفاصيل</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($academies)): ?>
            <tr><td colspan="6" style="padding:30px;text-align:center;color:#9CA3AF;">لا توجد بيانات — يرجى تشغيل php cli.php seed</td></tr>
            <?php else: ?>
            <?php foreach ($academies as $a): ?>
            <tr style="border-top:1px solid #F3F4F6;">
                <td style="padding:10px 15px;font-weight:600;"><?= e($a['academy_name_ar']) ?></td>
                <td style="padding:10px 15px;font-size:12px;color:#6B7280;"><?= e($a['discipline_code']) ?></td>
                <td style="padding:10px 15px;"><?= (int) $a['pricing_count'] ?></td>
                <td style="padding:10px 15px;direction:ltr;text-align:right;"><?= number_format((float) $a['min_price_member'], 0) ?> ج.م</td>
                <td style="padding:10px 15px;direction:ltr;text-align:right;"><?= number_format((float) $a['max_price_member'], 0) ?> ج.م</td>
                <td style="padding:10px 15px;">
                    <button type="button" class="btn btn-outline" style="padding:4px 10px;font-size:11px;" onclick="loadAcademy('<?= e($a['academy_code']) ?>')">
                        <i data-lucide="eye" style="width:12px;height:12px;vertical-align:middle;"></i>
                    </button>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<!-- Session Cards -->
<?php if (!empty($sessionCards)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:15px;font-weight:600;"><i data-lucide="credit-card" style="width:16px;height:16px;vertical-align:middle;margin-left:6px;"></i> كروت حصص السباحة</h3>
    </div>
    <table style="width:100%;border-collapse:collapse;font-size:13px;">
        <thead>
            <tr style="background:#F9FAFB;">
                <th style="padding:8px 15px;text-align:right;font-weight:600;color:#6B7280;">الكارت</th>
                <th style="padding:8px 15px;text-align:right;font-weight:600;color:#6B7280;">عدد الحصص</th>
                <th style="padding:8px 15px;text-align:right;font-weight:600;color:#6B7280;">السعر</th>
                <th style="padding:8px 15px;text-align:right;font-weight:600;color:#6B7280;">سعر الحصة</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($sessionCards as $card): ?>
            <tr style="border-top:1px solid #F3F4F6;">
                <td style="padding:8px 15px;"><?= e($card['level_name_ar']) ?></td>
                <td style="padding:8px 15px;"><?= $card['total_sessions'] ?></td>
                <td style="padding:8px 15px;direction:ltr;text-align:right;"><?= number_format($card['price'], 0) ?> ج.م</td>
                <td style="padding:8px 15px;direction:ltr;text-align:right;color:#059669;"><?= number_format($card['price_per_session'], 0) ?> ج.م</td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php endif; ?>

<!-- Academy Detail Modal -->
<div id="academyDetail" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,0.5);z-index:1000;display:none;align-items:center;justify-content:center;">
    <div style="background:white;border-radius:12px;padding:25px;max-width:800px;width:90%;max-height:80vh;overflow-y:auto;">
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
            <h3 id="academyDetailTitle" style="margin:0;font-size:18px;font-weight:700;"></h3>
            <button type="button" onclick="closeDetail()" style="background:none;border:none;cursor:pointer;font-size:20px;">✕</button>
        </div>
        <div id="academyDetailBody"></div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});

function loadAcademy(code) {
    fetch('/sa/academy-pricing/academy/' + code, {headers:{'X-Requested-With':'XMLHttpRequest'}})
    .then(function(r){return r.json();})
    .then(function(data) {
        if (data.error) { alert(data.error); return; }
        document.getElementById('academyDetailTitle').textContent = data.academy_name_ar;
        var html = '';
        for (var cat in data.categories) {
            var catLabel = {'subscription':'اشتراكات','team':'فرق','private_training':'تدريب خاص','session_card':'كروت حصص','lane_rental':'ايجار حارات'}[cat] || cat;
            html += '<h4 style="margin:15px 0 8px;font-size:14px;color:#4B5563;border-bottom:1px solid #E5E7EB;padding-bottom:5px;">' + catLabel + '</h4>';
            html += '<table style="width:100%;border-collapse:collapse;font-size:12px;margin-bottom:10px;"><thead><tr style="background:#F9FAFB;"><th style="padding:6px 10px;text-align:right;">المستوى</th><th style="padding:6px 10px;text-align:right;">عضو</th><th style="padding:6px 10px;text-align:right;">غير عضو</th><th style="padding:6px 10px;text-align:right;">تدريبات/اسبوع</th></tr></thead><tbody>';
            data.categories[cat].forEach(function(row) {
                html += '<tr style="border-top:1px solid #F3F4F6;"><td style="padding:6px 10px;">' + (row.level_name_ar||'—') + '</td><td style="padding:6px 10px;direction:ltr;text-align:right;">' + Number(row.price_member).toLocaleString() + '</td><td style="padding:6px 10px;direction:ltr;text-align:right;">' + Number(row.price_nonmember).toLocaleString() + '</td><td style="padding:6px 10px;">' + (row.sessions_per_week||'—') + '</td></tr>';
            });
            html += '</tbody></table>';
        }
        document.getElementById('academyDetailBody').innerHTML = html;
        document.getElementById('academyDetail').style.display = 'flex';
    });
}

function closeDetail() {
    document.getElementById('academyDetail').style.display = 'none';
}
</script>
<?php $__template->endSection(); ?>
