<?php
declare(strict_types=1);
$__template->layout('Layout.main');
$__template->section('title', 'تقارير اللاعبين');
$__template->section('content');
?>

<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:20px;">
    <h1 style="font-size:22px;font-weight:700;color:#0D7377;">تقارير اللاعبين</h1>
    <a href="/sa" class="btn btn-outline btn-sm">العودة للوحة التحكم</a>
</div>

<!-- Filter Card -->
<div class="card" style="margin-bottom:20px;padding:20px;border-right:4px solid #0D7377;">
    <form method="GET" action="/sa/reports/players">
        <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:12px;">
            <div class="form-group">
                <label class="form-label">اللعبة</label>
                <select name="discipline_id" id="filter_discipline" class="form-select" onchange="filterPrograms()">
                    <option value="">الكل</option>
                    <?php foreach ($disciplines as $d): ?>
                    <option value="<?= (int) $d['id'] ?>" <?= ($filters['discipline_id'] ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">البرنامج</label>
                <select name="program_id" id="filter_program" class="form-select" onchange="filterGroups()">
                    <option value="">الكل</option>
                    <?php foreach ($programs as $p): ?>
                    <option value="<?= (int) $p['id'] ?>" data-discipline="<?= (int) $p['discipline_id'] ?>" <?= ($filters['program_id'] ?? '') == $p['id'] ? 'selected' : '' ?>><?= e($p['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">المجموعة</label>
                <select name="group_id" id="filter_group" class="form-select">
                    <option value="">الكل</option>
                    <?php foreach ($groups as $g): ?>
                    <option value="<?= (int) $g['id'] ?>" data-program="<?= (int) $g['program_id'] ?>" <?= ($filters['group_id'] ?? '') == $g['id'] ? 'selected' : '' ?>><?= e($g['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">نوع اللاعب</label>
                <select name="player_type" class="form-select">
                    <option value="">الكل</option>
                    <option value="member" <?= ($filters['player_type'] ?? '') === 'member' ? 'selected' : '' ?>>عضو</option>
                    <option value="non_member" <?= ($filters['player_type'] ?? '') === 'non_member' ? 'selected' : '' ?>>غير عضو</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">الحالة الطبية</label>
                <select name="medical_status" class="form-select">
                    <option value="">الكل</option>
                    <option value="pending" <?= ($filters['medical_status'] ?? '') === 'pending' ? 'selected' : '' ?>>معلق</option>
                    <option value="fit" <?= ($filters['medical_status'] ?? '') === 'fit' ? 'selected' : '' ?>>لائق</option>
                    <option value="conditional" <?= ($filters['medical_status'] ?? '') === 'conditional' ? 'selected' : '' ?>>مشروط</option>
                    <option value="unfit" <?= ($filters['medical_status'] ?? '') === 'unfit' ? 'selected' : '' ?>>غير لائق</option>
                    <option value="expired" <?= ($filters['medical_status'] ?? '') === 'expired' ? 'selected' : '' ?>>منتهي</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">حالة الاشتراك</label>
                <select name="payment_status" class="form-select">
                    <option value="">الكل</option>
                    <option value="paid" <?= ($filters['payment_status'] ?? '') === 'paid' ? 'selected' : '' ?>>مدفوع</option>
                    <option value="unpaid" <?= ($filters['payment_status'] ?? '') === 'unpaid' ? 'selected' : '' ?>>غير مدفوع</option>
                    <option value="overdue" <?= ($filters['payment_status'] ?? '') === 'overdue' ? 'selected' : '' ?>>متأخر</option>
                    <option value="exempt" <?= ($filters['payment_status'] ?? '') === 'exempt' ? 'selected' : '' ?>>معفي</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">الفرع</label>
                <select name="branch_id" class="form-select">
                    <option value="">الكل</option>
                    <?php foreach ($branches as $b): ?>
                    <option value="<?= (int) $b['id'] ?>" <?= ($filters['branch_id'] ?? '') == $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group" style="display:flex;align-items:end;">
                <button type="submit" class="btn btn-primary" style="width:100%;">
                    <i data-lucide="search" style="width:14px;height:14px;display:inline-block;vertical-align:middle;margin-left:4px;"></i> عرض التقرير
                </button>
            </div>
        </div>
    </form>
</div>

<?php if ($report !== null): ?>
<!-- Results -->
<div class="card" style="padding:20px;">
    <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:16px;">
        <h2 style="font-size:16px;font-weight:600;color:#374151;margin:0;">
            النتائج: <?= (int) $report['total'] ?> لاعب
        </h2>
        <div style="display:flex;gap:8px;">
            <a href="/sa/reports/players/export-csv?<?= http_build_query(array_filter($filters, fn($v) => $v !== '' && $v !== null)) ?>" class="btn btn-sm btn-outline" style="color:#059669;border-color:#059669;">
                <i data-lucide="file-spreadsheet" style="width:14px;height:14px;display:inline-block;vertical-align:middle;margin-left:4px;"></i> تصدير CSV
            </a>
            <a href="/sa/reports/players/export-pdf?<?= http_build_query(array_filter($filters, fn($v) => $v !== '' && $v !== null)) ?>" class="btn btn-sm btn-outline" style="color:#DC2626;border-color:#DC2626;">
                <i data-lucide="file-text" style="width:14px;height:14px;display:inline-block;vertical-align:middle;margin-left:4px;"></i> تصدير PDF
            </a>
        </div>
    </div>

    <?php if (empty($report['rows'])): ?>
    <div style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد نتائج تطابق الفلاتر المحددة.</div>
    <?php else: ?>
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;font-size:12px;">
            <thead>
                <tr style="background:#F3F4F6;">
                    <th style="padding:8px 10px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">#</th>
                    <?php foreach ($report['columns'] as $label): ?>
                    <th style="padding:8px 10px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;"><?= e($label) ?></th>
                    <?php endforeach; ?>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($report['rows'] as $idx => $row): ?>
                <tr style="border-bottom:1px solid #F3F4F6;<?= $idx % 2 ? 'background:#FAFAFA;' : '' ?>">
                    <td style="padding:6px 10px;color:#9CA3AF;"><?= $idx + 1 ?></td>
                    <?php foreach (array_keys($report['columns']) as $key): ?>
                    <td style="padding:6px 10px;white-space:nowrap;"><?= e((string) ($row[$key] ?? '—')) ?></td>
                    <?php endforeach; ?>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
<?php else: ?>
<div class="card" style="padding:40px;text-align:center;color:#9CA3AF;">
    <i data-lucide="filter" style="width:40px;height:40px;display:block;margin:0 auto 12px;opacity:0.5;"></i>
    <p style="font-size:14px;">اختر الفلاتر ثم اضغط "عرض التقرير" لعرض النتائج.</p>
</div>
<?php endif; ?>

<script>
function filterPrograms() {
    var discId = document.getElementById('filter_discipline').value;
    var progSelect = document.getElementById('filter_program');
    var opts = progSelect.querySelectorAll('option[data-discipline]');
    opts.forEach(function(opt) {
        opt.style.display = (!discId || opt.getAttribute('data-discipline') === discId) ? '' : 'none';
    });
    if (progSelect.selectedOptions[0] && progSelect.selectedOptions[0].style.display === 'none') {
        progSelect.value = '';
    }
    filterGroups();
}

function filterGroups() {
    var progId = document.getElementById('filter_program').value;
    var grpSelect = document.getElementById('filter_group');
    var opts = grpSelect.querySelectorAll('option[data-program]');
    opts.forEach(function(opt) {
        opt.style.display = (!progId || opt.getAttribute('data-program') === progId) ? '' : 'none';
    });
    if (grpSelect.selectedOptions[0] && grpSelect.selectedOptions[0].style.display === 'none') {
        grpSelect.value = '';
    }
}

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

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