<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= e($game['name_ar']) ?> — جلسة <?= e($session['session_date']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/games/<?= (int) $game['id'] ?>" 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'); ?>

<!-- Session Info -->
<div class="card" style="padding:15px 20px;margin-bottom:20px;">
    <div style="display:flex;gap:20px;font-size:13px;flex-wrap:wrap;align-items:center;">
        <div><strong>التاريخ:</strong> <?= e($session['session_date']) ?></div>
        <div><strong>الوقت:</strong> <?= e(substr($session['start_time'], 0, 5)) ?> - <?= e(substr($session['end_time'], 0, 5)) ?></div>
        <div><strong>اللاعبون:</strong> <?= (int) $session['current_players'] ?> / <?= (int) $session['max_players'] ?></div>
        <?php
            $statusLabels = ['open' => 'متاحة', 'full' => 'ممتلئة', 'in_progress' => 'جارية', 'completed' => 'مكتملة', 'cancelled' => 'ملغية'];
            $statusColors = ['open' => '#059669', 'full' => '#D97706', 'in_progress' => '#2563EB', 'completed' => '#6B7280', 'cancelled' => '#DC2626'];
            $ss = $session['status'] ?? 'open';
        ?>
        <div>
            <span style="padding:3px 10px;border-radius:8px;font-size:12px;font-weight:600;background:<?= $statusColors[$ss] ?? '#6B7280' ?>15;color:<?= $statusColors[$ss] ?? '#6B7280' ?>;"><?= $statusLabels[$ss] ?? $ss ?></span>
        </div>
        <div style="margin-right:auto;display:flex;gap:8px;">
            <?php if ($ss === 'open'): ?>
            <form method="POST" action="/sa/games/<?= (int) $game['id'] ?>/sessions/<?= (int) $session['id'] ?>/start" style="display:inline;">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-sm" style="background:#2563EB;color:#fff;padding:5px 12px;font-size:12px;border-radius:6px;">بدء الجلسة</button>
            </form>
            <?php endif; ?>
            <?php if (in_array($ss, ['open', 'in_progress', 'full'])): ?>
            <form method="POST" action="/sa/games/<?= (int) $game['id'] ?>/sessions/<?= (int) $session['id'] ?>/complete" style="display:inline;">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-sm" style="background:#059669;color:#fff;padding:5px 12px;font-size:12px;border-radius:6px;">إنهاء الجلسة</button>
            </form>
            <?php endif; ?>
            <?php if ($ss !== 'completed' && $ss !== 'cancelled'): ?>
            <form method="POST" action="/sa/games/<?= (int) $game['id'] ?>/sessions/<?= (int) $session['id'] ?>/cancel" style="display:inline;" onsubmit="return confirm('هل أنت متأكد من إلغاء الجلسة؟')">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-sm" style="background:#DC2626;color:#fff;padding:5px 12px;font-size:12px;border-radius:6px;">إلغاء</button>
            </form>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Issue Ticket -->
<?php if (in_array($session['status'], ['open', 'in_progress'])): ?>
<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;">
        <form method="POST" action="/sa/games/<?= (int) $game['id'] ?>/sessions/<?= (int) $session['id'] ?>/ticket" style="display:flex;gap:12px;align-items:end;flex-wrap:wrap;">
            <?= csrf_field() ?>
            <div class="form-group" style="margin:0;min-width:200px;">
                <label class="form-label" style="font-size:12px;">اسم اللاعب / الضيف</label>
                <input type="text" name="player_name" class="form-input" placeholder="اسم اللاعب أو الضيف" style="font-size:13px;">
            </div>
            <div class="form-group" style="margin:0;">
                <label class="form-label" style="font-size:12px;">النوع</label>
                <select name="player_type" class="form-input" style="font-size:13px;">
                    <option value="guest">ضيف</option>
                    <option value="member">عضو</option>
                    <option value="non_member">غير عضو</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary" style="font-size:13px;padding:8px 16px;">
                <i data-lucide="ticket" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> إصدار تذكرة
            </button>
        </form>
    </div>
</div>
<?php endif; ?>

<!-- Tickets List -->
<div class="card">
    <div style="padding:12px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:14px;color:#374151;">التذاكر (<?= count($tickets) ?>)</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>الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($tickets)): ?>
                <tr><td colspan="7" style="text-align:center;padding:30px;color:#6B7280;">لا توجد تذاكر</td></tr>
                <?php else: ?>
                <?php foreach ($tickets as $i => $t):
                    $typeLabels = ['member' => 'عضو', 'non_member' => 'غير عضو', 'guest' => 'ضيف'];
                    $payLabels = ['unpaid' => 'غير مدفوع', 'paid' => 'مدفوع', 'refunded' => 'مسترد'];
                    $payColors = ['unpaid' => '#D97706', 'paid' => '#059669', 'refunded' => '#6B7280'];
                ?>
                <tr>
                    <td><?= $i + 1 ?></td>
                    <td><code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($t['ticket_number']) ?></code></td>
                    <td style="font-weight:600;"><?= e($t['sa_player_name'] ?? $t['player_name'] ?? '—') ?></td>
                    <td style="font-size:12px;"><?= $typeLabels[$t['player_type'] ?? ''] ?? $t['player_type'] ?></td>
                    <td style="font-weight:600;color:#0D7377;"><?= money((float) $t['fee_charged']) ?></td>
                    <td>
                        <?php $ps = $t['payment_status'] ?? 'unpaid'; ?>
                        <span style="padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $payColors[$ps] ?? '#6B7280' ?>15;color:<?= $payColors[$ps] ?? '#6B7280' ?>;">
                            <?= $payLabels[$ps] ?? $ps ?>
                        </span>
                    </td>
                    <td>
                        <?php if ($t['checked_in']): ?>
                        <span style="color:#059669;font-size:12px;font-weight:600;">✓ حاضر</span>
                        <?php else: ?>
                        <span style="color:#6B7280;font-size:12px;">—</span>
                        <?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(); ?>
