<?php
/**
 * Standings partial view.
 * Included from show.php or rendered standalone.
 * Variables: $standings, $tournament
 */
use App\Modules\Tournaments\Models\TournamentParticipant;
?>

<div class="card" style="padding:0;overflow-x:auto;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;color:#1A1A2E;">
            <i data-lucide="list-ordered" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i>
            ترتيب المشاركين
        </h3>
    </div>

    <?php if (!empty($standings)): ?>
    <table style="width:100%;border-collapse:collapse;font-size:14px;">
        <thead>
            <tr style="background:#F9FAFB;border-bottom:2px solid #E5E7EB;">
                <th style="padding:10px 12px;text-align:right;font-weight:600;color:#374151;width:50px;">المركز</th>
                <th style="padding:10px 12px;text-align:right;font-weight:600;color:#374151;">المشارك</th>
                <th style="padding:10px 12px;text-align:center;font-weight:600;color:#374151;">لعب</th>
                <th style="padding:10px 12px;text-align:center;font-weight:600;color:#374151;">فاز</th>
                <th style="padding:10px 12px;text-align:center;font-weight:600;color:#374151;">تعادل</th>
                <th style="padding:10px 12px;text-align:center;font-weight:600;color:#374151;">خسر</th>
                <th style="padding:10px 12px;text-align:center;font-weight:600;color:#374151;">النقاط</th>
                <th style="padding:10px 12px;text-align:right;font-weight:600;color:#374151;">الحالة</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($standings as $i => $s):
                $position = $i + 1;
                $played = (int)($s['wins'] ?? 0) + (int)($s['draws'] ?? 0) + (int)($s['losses'] ?? 0);
                $positionStyle = '';
                if ($position === 1) $positionStyle = 'color:#7C3AED;font-weight:700;';
                elseif ($position === 2) $positionStyle = 'color:#0D7377;font-weight:700;';
                elseif ($position === 3) $positionStyle = 'color:#D97706;font-weight:700;';

                $pStatus = $s['status'] ?? 'registered';
                $pStatusLabel = TournamentParticipant::getStatuses()[$pStatus] ?? $pStatus;
                $pStatusColor = TournamentParticipant::getStatusColor($pStatus);
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;<?= $position <= 3 ? 'background:#FAFAFA;' : '' ?>">
                <td style="padding:10px 12px;<?= $positionStyle ?>">
                    <?php if ($position === 1): ?>
                        <i data-lucide="trophy" style="width:14px;height:14px;vertical-align:middle;color:#7C3AED;"></i>
                    <?php endif; ?>
                    <?= $position ?>
                </td>
                <td style="padding:10px 12px;font-weight:600;">
                    <?= e($s['team_name'] ?? $s['player_name'] ?? '—') ?>
                </td>
                <td style="padding:10px 12px;text-align:center;color:#6B7280;"><?= $played ?></td>
                <td style="padding:10px 12px;text-align:center;color:#059669;font-weight:600;"><?= (int) ($s['wins'] ?? 0) ?></td>
                <td style="padding:10px 12px;text-align:center;color:#D97706;"><?= (int) ($s['draws'] ?? 0) ?></td>
                <td style="padding:10px 12px;text-align:center;color:#DC2626;"><?= (int) ($s['losses'] ?? 0) ?></td>
                <td style="padding:10px 12px;text-align:center;font-weight:700;font-size:16px;color:#1A1A2E;"><?= (int) ($s['points'] ?? 0) ?></td>
                <td style="padding:10px 12px;">
                    <span style="display:inline-block;padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $pStatusColor ?>15;color:<?= $pStatusColor ?>;"><?= e($pStatusLabel) ?></span>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php else: ?>
    <div style="padding:40px;text-align:center;color:#9CA3AF;">
        لا توجد بيانات ترتيب بعد.
    </div>
    <?php endif; ?>
</div>
