<?php
use App\Modules\Sessions\Models\TrainingSession;
use App\Modules\Sessions\Models\SessionAttendance;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>تفاصيل الحصة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sessions" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php if (in_array($session['status'], ['scheduled', 'in_progress'], true)): ?>
<a href="/sessions/<?= (int) $session['id'] ?>/attendance" class="btn btn-primary"><i data-lucide="clipboard-check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تسجيل الحضور</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<?php
$statusColors = [
    'scheduled'   => '#3B82F6',
    'in_progress' => '#F59E0B',
    'completed'   => '#10B981',
    'cancelled'   => '#EF4444',
];
$typeColors = [
    'regular'    => '#6B7280',
    'makeup'     => '#7C3AED',
    'extra'      => '#D97706',
    'assessment' => '#2563EB',
];
$sStatus = $session['status'] ?? 'scheduled';
$sType = $session['session_type'] ?? 'regular';
$sColor = $statusColors[$sStatus] ?? '#6B7280';
$tColor = $typeColors[$sType] ?? '#6B7280';
?>

<!-- Session Header -->
<div class="card" style="margin-bottom:20px;">
    <div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px;margin-bottom:20px;">
        <div style="display:flex;align-items:center;gap:12px;">
            <div style="width:48px;height:48px;border-radius:12px;background:<?= $sColor ?>15;display:flex;align-items:center;justify-content:center;">
                <i data-lucide="calendar-days" style="width:24px;height:24px;color:<?= $sColor ?>;"></i>
            </div>
            <div>
                <h2 style="margin:0;font-size:20px;font-weight:700;color:#1A1A2E;">
                    حصة <?= e($session['group_name'] ?? '—') ?>
                </h2>
                <p style="margin:4px 0 0;font-size:13px;color:#6B7280;">
                    <?= e($session['session_date']) ?> | <?= e(substr($session['start_time'], 0, 5)) ?> - <?= e(substr($session['end_time'], 0, 5)) ?>
                </p>
            </div>
        </div>
        <div style="display:flex;gap:8px;align-items:center;">
            <span style="padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $tColor ?>15;color:<?= $tColor ?>;">
                <?= e($types[$sType] ?? $sType) ?>
            </span>
            <span style="padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $sColor ?>15;color:<?= $sColor ?>;">
                <?= e($statuses[$sStatus] ?? $sStatus) ?>
            </span>
        </div>
    </div>

    <!-- Session Details Grid -->
    <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(200px, 1fr));gap:16px;">
        <div style="padding:12px;background:#F9FAFB;border-radius:8px;">
            <div style="font-size:11px;color:#6B7280;margin-bottom:4px;">الأكاديمية</div>
            <div style="font-size:14px;font-weight:600;color:#1A1A2E;"><?= e($session['academy_name'] ?? '—') ?></div>
        </div>
        <div style="padding:12px;background:#F9FAFB;border-radius:8px;">
            <div style="font-size:11px;color:#6B7280;margin-bottom:4px;">المدرب</div>
            <div style="font-size:14px;font-weight:600;color:#1A1A2E;"><?= e($session['coach_name'] ?? '—') ?></div>
        </div>
        <div style="padding:12px;background:#F9FAFB;border-radius:8px;">
            <div style="font-size:11px;color:#6B7280;margin-bottom:4px;">المنشأة</div>
            <div style="font-size:14px;font-weight:600;color:#1A1A2E;"><?= e($session['facility_name'] ?? '—') ?></div>
        </div>
        <div style="padding:12px;background:#F9FAFB;border-radius:8px;">
            <div style="font-size:11px;color:#6B7280;margin-bottom:4px;">الحضور</div>
            <div style="font-size:14px;font-weight:600;color:#1A1A2E;">
                <?= (int) $session['players_attended'] ?> / <?= (int) $session['players_expected'] ?>
            </div>
        </div>
    </div>

    <?php if (!empty($session['notes'])): ?>
    <div style="margin-top:16px;padding:12px;background:#FFFBEB;border-radius:8px;border:1px solid #FDE68A;">
        <div style="font-size:11px;color:#92400E;margin-bottom:4px;">ملاحظات</div>
        <div style="font-size:13px;color:#78350F;"><?= e($session['notes']) ?></div>
    </div>
    <?php endif; ?>

    <?php if ($sStatus === 'cancelled' && !empty($session['cancellation_reason'])): ?>
    <div style="margin-top:16px;padding:12px;background:#FEF2F2;border-radius:8px;border:1px solid #FECACA;">
        <div style="font-size:11px;color:#991B1B;margin-bottom:4px;">سبب الإلغاء</div>
        <div style="font-size:13px;color:#7F1D1D;"><?= e($session['cancellation_reason']) ?></div>
    </div>
    <?php endif; ?>
</div>

<!-- Actions -->
<?php if (in_array($sStatus, ['scheduled', 'in_progress'], true)): ?>
<div class="card" style="margin-bottom:20px;padding:15px;">
    <div style="display:flex;gap:10px;flex-wrap:wrap;">
        <?php if ($sStatus === 'in_progress'): ?>
        <form method="POST" action="/sessions/<?= (int) $session['id'] ?>/complete" style="display:inline;">
            <?= csrf_field() ?>
            <button type="submit" class="btn btn-primary" onclick="return confirm('هل تريد إكمال هذه الحصة؟')">
                <i data-lucide="check-circle" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إكمال الحصة
            </button>
        </form>
        <?php endif; ?>

        <button type="button" class="btn btn-outline" style="color:#EF4444;border-color:#EF4444;" onclick="document.getElementById('cancelModal').style.display='flex'">
            <i data-lucide="x-circle" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إلغاء الحصة
        </button>
    </div>
</div>

<!-- Cancel Modal -->
<div id="cancelModal" style="display:none;position:fixed;inset:0;z-index:1000;align-items:center;justify-content:center;background:rgba(0,0,0,0.5);">
    <div class="card" style="width:100%;max-width:400px;padding:24px;">
        <h3 style="margin:0 0 16px;font-size:18px;font-weight:700;color:#EF4444;">إلغاء الحصة</h3>
        <form method="POST" action="/sessions/<?= (int) $session['id'] ?>/cancel">
            <?= csrf_field() ?>
            <div style="margin-bottom:16px;">
                <label class="form-label">سبب الإلغاء <span style="color:#EF4444;">*</span></label>
                <textarea name="cancellation_reason" class="form-input" rows="3" required placeholder="أدخل سبب إلغاء الحصة..."></textarea>
            </div>
            <p style="font-size:12px;color:#6B7280;margin-bottom:16px;">
                سيتم إصدار رصيد تعويضي لجميع لاعبي المجموعة.
            </p>
            <div style="display:flex;gap:10px;justify-content:end;">
                <button type="button" class="btn btn-outline" onclick="document.getElementById('cancelModal').style.display='none'">تراجع</button>
                <button type="submit" class="btn" style="background:#EF4444;color:#fff;">تأكيد الإلغاء</button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<!-- Attendance Table -->
<?php if (!empty($attendance)): ?>
<div class="card" style="padding:0;overflow:hidden;">
    <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;font-weight:700;color:#1A1A2E;">
            <i data-lucide="clipboard-list" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;"></i>
            سجل الحضور
        </h3>
    </div>
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;font-size:14px;">
            <thead>
                <tr style="background:#F9FAFB;border-bottom:2px solid #E5E7EB;">
                    <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">#</th>
                    <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">اللاعب</th>
                    <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">الحالة</th>
                    <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">وقت الحضور</th>
                    <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">ملاحظات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($attendance as $i => $att):
                    $attStatus = $att['status'] ?? 'present';
                    $attColor = SessionAttendance::getStatusColor($attStatus);
                    $attStatuses = SessionAttendance::getStatuses();
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 16px;color:#6B7280;"><?= $i + 1 ?></td>
                    <td style="padding:10px 16px;font-weight:500;"><?= e($att['player_name'] ?? '—') ?></td>
                    <td style="padding:10px 16px;text-align:center;">
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $attColor ?>15;color:<?= $attColor ?>;">
                            <?= e($attStatuses[$attStatus] ?? $attStatus) ?>
                        </span>
                    </td>
                    <td style="padding:10px 16px;text-align:center;color:#6B7280;">
                        <?= !empty($att['check_in_time']) ? e(substr($att['check_in_time'], 0, 5)) : '—' ?>
                    </td>
                    <td style="padding:10px 16px;color:#6B7280;font-size:12px;"><?= e($att['notes'] ?? '') ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

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