<?php
use App\Modules\Tournaments\Models\TournamentMatch;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>مباريات: <?= e($tournament['name_ar'] ?? '') ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/tournaments/<?= (int) $tournament['id'] ?>" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> العودة للبطولة</a>
<?php $__template->endSection(); ?>

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

<?php if (!empty($rounds)): ?>
    <?php foreach ($rounds as $roundNum => $roundMatches): ?>
    <div class="card" style="padding:0;margin-bottom:15px;overflow-x:auto;">
        <div style="padding:12px 20px;background:#F9FAFB;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:15px;color:#374151;">الجولة <?= (int) $roundNum ?></h3>
        </div>
        <table style="width:100%;border-collapse:collapse;font-size:14px;">
            <thead>
                <tr style="border-bottom:1px solid #E5E7EB;">
                    <th style="padding:8px 12px;text-align:right;font-weight:600;color:#6B7280;font-size:12px;">#</th>
                    <th style="padding:8px 12px;text-align:right;font-weight:600;color:#6B7280;font-size:12px;">المشارك أ</th>
                    <th style="padding:8px 12px;text-align:center;font-weight:600;color:#6B7280;font-size:12px;">النتيجة</th>
                    <th style="padding:8px 12px;text-align:right;font-weight:600;color:#6B7280;font-size:12px;">المشارك ب</th>
                    <th style="padding:8px 12px;text-align:right;font-weight:600;color:#6B7280;font-size:12px;">الموعد</th>
                    <th style="padding:8px 12px;text-align:right;font-weight:600;color:#6B7280;font-size:12px;">الحالة</th>
                    <th style="padding:8px 12px;text-align:right;font-weight:600;color:#6B7280;font-size:12px;">إجراء</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($roundMatches as $m):
                    $mStatus = $m['status'] ?? 'scheduled';
                    $mStatusLabel = TournamentMatch::getStatuses()[$mStatus] ?? $mStatus;
                    $mStatusColor = TournamentMatch::getStatusColor($mStatus);
                    $winnerId = (int) ($m['winner_participant_id'] ?? 0);
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 12px;color:#6B7280;"><?= (int) $m['match_number'] ?></td>
                    <td style="padding:10px 12px;font-weight:<?= $winnerId === (int)($m['participant_a_id'] ?? 0) ? '700' : '400' ?>;">
                        <?= e($m['participant_a_name'] ?? 'يُحدد') ?>
                    </td>
                    <td style="padding:10px 12px;text-align:center;font-weight:600;">
                        <?php if ($mStatus === 'completed'): ?>
                            <?= e($m['score_a'] ?? '0') ?> - <?= e($m['score_b'] ?? '0') ?>
                        <?php else: ?>
                            <span style="color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                    <td style="padding:10px 12px;font-weight:<?= $winnerId === (int)($m['participant_b_id'] ?? 0) ? '700' : '400' ?>;">
                        <?= e($m['participant_b_name'] ?? 'يُحدد') ?>
                    </td>
                    <td style="padding:10px 12px;font-size:13px;color:#6B7280;">
                        <?= !empty($m['scheduled_date']) ? e($m['scheduled_date']) : '—' ?>
                        <?= !empty($m['scheduled_time']) ? ' ' . e($m['scheduled_time']) : '' ?>
                    </td>
                    <td style="padding:10px 12px;">
                        <span style="display:inline-block;padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $mStatusColor ?>15;color:<?= $mStatusColor ?>;"><?= e($mStatusLabel) ?></span>
                    </td>
                    <td style="padding:10px 12px;">
                        <?php if ($mStatus === 'scheduled' && $m['participant_a_id'] && $m['participant_b_id']): ?>
                        <a href="/matches/<?= (int) $m['id'] ?>/result" class="btn btn-sm btn-primary" style="font-size:11px;padding:3px 8px;">نتيجة</a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endforeach; ?>
<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <i data-lucide="swords" style="width:48px;height:48px;color:#D1D5DB;"></i>
    <h3 style="color:#6B7280;margin:15px 0 8px;">لا توجد مباريات</h3>
    <p style="color:#9CA3AF;font-size:14px;">يجب توليد شجرة المباريات أولاً.</p>
</div>
<?php endif; ?>

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