<?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'); ?>

<!-- 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><?= $player['player_type'] === 'member' ? 'عضو' : 'غير عضو' ?></span>
                <?php if ($player['date_of_birth']): ?>
                <span>العمر: <?= (int) date_diff(date_create($player['date_of_birth']), date_create())->y ?> سنة</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; ?>

<!-- Available Groups -->
<div class="card">
    <div style="padding:12px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:14px;color:#374151;">المجموعات المتاحة</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>المجموعة</th>
                    <th>البرنامج</th>
                    <th>النشاط</th>
                    <th>المدرب</th>
                    <th>السعة</th>
                    <th>الرسوم</th>
                    <th style="width:120px;">إجراء</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($groups)): ?>
                <tr><td colspan="7" style="text-align:center;padding:30px;color:#6B7280;">لا توجد مجموعات متاحة</td></tr>
                <?php else: ?>
                <?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;
                    }
                ?>
                <?php foreach ($groups as $g):
                    $current = (int) $g['current_count'];
                    $max = (int) $g['max_capacity'];
                    $isFull = $current >= $max;
                    $pct = $max > 0 ? round(($current / $max) * 100) : 0;
                    $barColor = $pct >= 90 ? '#DC2626' : ($pct >= 70 ? '#D97706' : '#059669');
                    $fee = $isMember ? (float) $g['monthly_fee_member'] : (float) $g['monthly_fee_nonmember'];

                    $ageOk = true;
                    if ($playerAge !== null) {
                        if (!empty($g['min_age']) && $playerAge < (int) $g['min_age']) $ageOk = false;
                        if (!empty($g['max_age']) && $playerAge > (int) $g['max_age']) $ageOk = false;
                    }
                ?>
                <tr style="<?= !$ageOk ? 'opacity:0.5;' : '' ?>">
                    <td style="font-weight:600;font-size:13px;"><?= e($g['name_ar']) ?></td>
                    <td style="font-size:12px;"><?= e($g['program_name']) ?></td>
                    <td style="font-size:12px;"><?= e($g['discipline_name']) ?></td>
                    <td style="font-size:12px;"><?= e($g['coach_name'] ?? '—') ?></td>
                    <td>
                        <div style="display:flex;align-items:center;gap:6px;">
                            <div style="flex:1;height:6px;background:#E5E7EB;border-radius:3px;min-width:50px;">
                                <div style="height:100%;width:<?= $pct ?>%;background:<?= $barColor ?>;border-radius:3px;"></div>
                            </div>
                            <span style="font-size:11px;color:#6B7280;"><?= $current ?>/<?= $max ?></span>
                        </div>
                    </td>
                    <td style="font-weight:700;color:#0D7377;font-size:13px;"><?= money($fee) ?></td>
                    <td>
                        <?php if (!$ageOk): ?>
                        <span style="font-size:11px;color:#DC2626;">عمر غير مناسب</span>
                        <?php elseif ($isFull): ?>
                        <form method="POST" action="/sa/assignments/<?= (int) $player['id'] ?>/assign" style="display:inline;">
                            <?= csrf_field() ?>
                            <input type="hidden" name="group_id" value="<?= (int) $g['id'] ?>">
                            <input type="hidden" name="force" value="1">
                            <button type="submit" class="btn btn-sm" style="font-size:11px;padding:4px 10px;background:#FEF3C7;color:#D97706;border:1px solid #FCD34D;border-radius:4px;" onclick="return confirm('المجموعة ممتلئة. تعيين بالقوة؟')">
                                تعيين (ممتلئة)
                            </button>
                        </form>
                        <?php else: ?>
                        <form method="POST" action="/sa/assignments/<?= (int) $player['id'] ?>/assign" style="display:inline;">
                            <?= csrf_field() ?>
                            <input type="hidden" name="group_id" value="<?= (int) $g['id'] ?>">
                            <button type="submit" class="btn btn-sm btn-primary" style="font-size:11px;padding:4px 10px;">
                                <i data-lucide="plus" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i> تعيين
                            </button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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