<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تعيين: <?= e($player['full_name_ar']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/assignments" 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'); ?>

<?php
    $isMember = ($player['player_type'] ?? '') === 'member';
    $playerAge = null;
    if (!empty($player['date_of_birth'])) {
        $playerAge = (int) date_diff(date_create($player['date_of_birth']), date_create())->y;
    }
    $selectedDiscipline = $filters['discipline_id'] ?? '';
    $selectedProgram = $filters['program_id'] ?? '';
    $programSearch = $filters['program_search'] ?? '';
?>

<!-- Player Info -->
<div class="card" style="padding:20px;margin-bottom:20px;">
    <div style="display:flex;gap:20px;align-items:center;">
        <div style="width:60px;height:60px;border-radius:50%;background:#F3F4F6;display:flex;align-items:center;justify-content:center;overflow:hidden;flex-shrink:0;">
            <?php if (!empty($player['photo_path'])): ?>
            <img src="/<?= e($player['photo_path']) ?>" style="width:100%;height:100%;object-fit:cover;">
            <?php else: ?>
            <i data-lucide="user" style="width:28px;height:28px;color:#9CA3AF;"></i>
            <?php endif; ?>
        </div>
        <div>
            <div style="font-size:16px;font-weight:700;"><?= e($player['full_name_ar']) ?></div>
            <div style="display:flex;gap:12px;margin-top:4px;font-size:12px;color:#6B7280;">
                <span><?= $isMember ? 'عضو' : 'غير عضو' ?></span>
                <?php if ($playerAge !== null): ?>
                <span>العمر: <?= $playerAge ?> سنة</span>
                <?php endif; ?>
                <?php if ($player['phone']): ?>
                <span><?= e($player['phone']) ?></span>
                <?php endif; ?>
                <?php
                    $medColors = ['fit' => '#059669', 'conditional' => '#D97706', 'pending' => '#6B7280', 'expired' => '#DC2626'];
                    $medLabels = ['fit' => 'لائق', 'conditional' => 'مشروط', 'pending' => 'معلق', 'expired' => 'منتهي'];
                    $ms = $player['medical_status'] ?? 'pending';
                ?>
                <span style="padding:2px 8px;border-radius:8px;background:<?= $medColors[$ms] ?? '#6B7280' ?>15;color:<?= $medColors[$ms] ?? '#6B7280' ?>;font-weight:600;"><?= $medLabels[$ms] ?? $ms ?></span>
            </div>
        </div>
    </div>
</div>

<!-- Current Enrollments -->
<?php if (!empty($currentEnrollments)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:12px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:14px;color:#374151;">التسجيلات الحالية</h3>
    </div>
    <div style="padding:15px 20px;">
        <div style="display:flex;flex-wrap:wrap;gap:10px;">
            <?php foreach ($currentEnrollments as $en): ?>
            <div style="padding:8px 14px;background:#ECFDF5;border:1px solid #A7F3D0;border-radius:8px;font-size:12px;">
                <strong><?= e($en['group_name']) ?></strong> — <?= e($en['program_name']) ?>
                <span style="margin-right:6px;padding:2px 6px;border-radius:4px;background:<?= $en['status'] === 'active' ? '#059669' : '#D97706' ?>;color:#fff;font-size:10px;"><?= $en['status'] === 'active' ? 'نشط' : 'معلق' ?></span>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Step 1: Choose Sport -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:12px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:14px;color:#0D7377;">
            <span style="display:inline-block;width:22px;height:22px;background:#0D7377;color:#fff;border-radius:50%;text-align:center;line-height:22px;font-size:12px;margin-left:8px;">1</span>
            اختر النشاط الرياضي
        </h3>
    </div>
    <div style="padding:15px 20px;">
        <div style="display:flex;flex-wrap:wrap;gap:8px;">
            <a href="/sa/assignments/<?= (int) $player['id'] ?>"
               class="btn <?= $selectedDiscipline === '' ? 'btn-primary' : 'btn-outline' ?>" style="font-size:13px;padding:6px 14px;">
                الكل
            </a>
            <?php foreach ($disciplines as $d): ?>
            <a href="/sa/assignments/<?= (int) $player['id'] ?>?discipline_id=<?= (int) $d['id'] ?>"
               class="btn <?= $selectedDiscipline == $d['id'] ? 'btn-primary' : 'btn-outline' ?>" style="font-size:13px;padding:6px 14px;">
                <?= e($d['name_ar']) ?>
            </a>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<!-- Step 2: Choose Program -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:12px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;font-size:14px;color:#0D7377;">
            <span style="display:inline-block;width:22px;height:22px;background:#0D7377;color:#fff;border-radius:50%;text-align:center;line-height:22px;font-size:12px;margin-left:8px;">2</span>
            اختر البرنامج
        </h3>
        <form method="GET" action="/sa/assignments/<?= (int) $player['id'] ?>" style="display:flex;gap:8px;">
            <input type="hidden" name="discipline_id" value="<?= e($selectedDiscipline) ?>">
            <input type="text" name="program_search" class="form-input" value="<?= e($programSearch) ?>" placeholder="بحث عن برنامج..." style="font-size:12px;padding:5px 10px;width:180px;">
            <button type="submit" class="btn btn-sm btn-outline" style="font-size:12px;padding:5px 10px;">بحث</button>
        </form>
    </div>
    <div style="padding:15px 20px;">
        <?php if (empty($programs)): ?>
        <p style="color:#6B7280;text-align:center;padding:15px;font-size:13px;">
            <?= $selectedDiscipline ? 'لا توجد برامج لهذا النشاط' : 'اختر نشاط رياضي أولاً أو اعرض الكل' ?>
        </p>
        <?php else: ?>
        <div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(280px, 1fr));gap:12px;">
            <?php foreach ($programs as $prog):
                $isSelected = ($selectedProgram == $prog['id']);
                $ageNote = '';
                if (!empty($prog['age_from']) || !empty($prog['age_to'])) {
                    $ageNote = ($prog['age_from'] ?? '?') . '-' . ($prog['age_to'] ?? '?') . ' سنة';
                }
                $fee = $isMember ? (float) $prog['monthly_fee_member'] : (float) $prog['monthly_fee_nonmember'];
            ?>
            <a href="/sa/assignments/<?= (int) $player['id'] ?>?discipline_id=<?= e($selectedDiscipline) ?>&program_id=<?= (int) $prog['id'] ?>"
               style="display:block;padding:14px;border:2px solid <?= $isSelected ? '#0D7377' : '#E5E7EB' ?>;border-radius:8px;text-decoration:none;color:inherit;background:<?= $isSelected ? '#F0FDFA' : '#fff' ?>;transition:border-color 0.2s;">
                <div style="display:flex;justify-content:space-between;align-items:start;">
                    <div>
                        <div style="font-weight:700;font-size:14px;color:#1A1A2E;margin-bottom:4px;"><?= e($prog['name_ar']) ?></div>
                        <div style="font-size:11px;color:#6B7280;">
                            <?= e($prog['discipline_name']) ?>
                            <?php if ($ageNote): ?> • <?= $ageNote ?><?php endif; ?>
                        </div>
                    </div>
                    <div style="text-align:left;">
                        <div style="font-size:13px;font-weight:700;color:#0D7377;"><?= money($fee) ?></div>
                        <?php if ((int) $prog['available_groups'] > 0): ?>
                        <div style="font-size:10px;color:#059669;margin-top:2px;"><?= (int) $prog['available_groups'] ?> مجموعة متاحة</div>
                        <?php else: ?>
                        <div style="font-size:10px;color:#D97706;margin-top:2px;">سيتم إنشاء مجموعة</div>
                        <?php endif; ?>
                    </div>
                </div>
            </a>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Step 3: Choose Trainer -->
<?php if ($selectedProgram !== ''): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:12px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:14px;color:#0D7377;">
            <span style="display:inline-block;width:22px;height:22px;background:#0D7377;color:#fff;border-radius:50%;text-align:center;line-height:22px;font-size:12px;margin-left:8px;">3</span>
            اختر المدرب
        </h3>
    </div>
    <div style="padding:15px 20px;">
        <?php if (empty($coaches)): ?>
        <p style="color:#6B7280;text-align:center;padding:15px;font-size:13px;">لا يوجد مدربين مسجلين لهذا البرنامج</p>
        <?php else: ?>
        <div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(250px, 1fr));gap:12px;">
            <?php foreach ($coaches as $coach): ?>
            <div style="padding:14px;border:1px solid #E5E7EB;border-radius:8px;display:flex;align-items:center;gap:12px;">
                <div style="width:44px;height:44px;border-radius:50%;background:#F3F4F6;display:flex;align-items:center;justify-content:center;overflow:hidden;flex-shrink:0;">
                    <?php if (!empty($coach['photo_path'])): ?>
                    <img src="/<?= e($coach['photo_path']) ?>" style="width:100%;height:100%;object-fit:cover;">
                    <?php else: ?>
                    <i data-lucide="user" style="width:20px;height:20px;color:#9CA3AF;"></i>
                    <?php endif; ?>
                </div>
                <div style="flex:1;min-width:0;">
                    <div style="font-weight:600;font-size:13px;"><?= e($coach['full_name_ar']) ?></div>
                    <div style="font-size:11px;color:#6B7280;">
                        <?php if (isset($coach['available_groups'])): ?>
                            <?php if ((int) $coach['available_groups'] > 0): ?>
                            <span style="color:#059669;"><?= (int) $coach['available_groups'] ?> مجموعة متاحة</span>
                            <?php else: ?>
                            <span style="color:#D97706;">سيتم إنشاء مجموعة</span>
                            <?php endif; ?>
                            <?php if (isset($coach['total_players'])): ?>
                            • <?= (int) $coach['total_players'] ?> لاعب
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>
                <form method="POST" action="/sa/assignments/<?= (int) $player['id'] ?>/assign-coach" style="margin:0;">
                    <?= csrf_field() ?>
                    <input type="hidden" name="program_id" value="<?= (int) $selectedProgram ?>">
                    <input type="hidden" name="coach_id" value="<?= (int) $coach['id'] ?>">
                    <button type="submit" class="btn btn-sm btn-primary" style="font-size:11px;padding:5px 12px;white-space:nowrap;">
                        تعيين
                    </button>
                </form>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <!-- Any available coach option -->
        <div style="margin-top:15px;padding-top:15px;border-top:1px solid #F3F4F6;text-align:center;">
            <?php if (!empty($bestGroupId)): ?>
            <form method="POST" action="/sa/assignments/<?= (int) $player['id'] ?>/assign" style="display:inline;">
                <?= csrf_field() ?>
                <input type="hidden" name="group_id" value="<?= (int) $bestGroupId ?>">
                <button type="submit" class="btn btn-outline" style="font-size:13px;padding:8px 20px;color:#0D7377;border-color:#0D7377;">
                    <i data-lucide="shuffle" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> أي مدرب متاح (أقل مجموعة ازدحاماً)
                </button>
            </form>
            <?php else: ?>
            <span style="font-size:12px;color:#6B7280;">لا توجد مجموعات متاحة — اختر مدرب لإنشاء مجموعة جديدة</span>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});
</script>
<?php $__template->endSection(); ?>
