<?php
use App\Modules\PlayerAffairs\Models\PlayerEvaluation;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>تقييمات اللاعبين<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.player.manage')): ?>
<a href="/evaluations/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تقييم جديد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<?php
$statuses = PlayerEvaluation::getStatuses();
$skillLevels = PlayerEvaluation::getSkillLevels();
?>

<!-- Filter Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/evaluations" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:180px;">
            <label class="form-label" style="font-size:12px;">بحث بالاسم</label>
            <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="اسم اللاعب..." class="form-input">
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">الرياضة</label>
            <select name="discipline_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($disciplines as $d): ?>
                <option value="<?= (int) $d['id'] ?>" <?= ($filters['discipline_id'] ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:120px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($statuses as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:120px;">
            <label class="form-label" style="font-size:12px;">المستوى</label>
            <select name="skill_level" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($skillLevels as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['skill_level'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">من تاريخ</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/evaluations" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Evaluations Table -->
<?php if (!empty($evaluations)): ?>
<div class="card" style="padding:0;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 15px;text-align:right;font-weight:600;color:#374151;">#</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">اللاعب</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الرياضة</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">المدرب</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">التاريخ</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الدرجة</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">المستوى</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الحالة</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $startIndex = (($pagination['current_page'] ?? 1) - 1) * ($pagination['per_page'] ?? 25);
            foreach ($evaluations as $i => $eval):
                $status = $eval['status'] ?? 'draft';
                $statusLabel = $statuses[$status] ?? $status;
                $statusColor = PlayerEvaluation::getStatusColor($status);
                $skillLevel = $eval['skill_level'] ?? '';
                $skillLabel = $skillLevels[$skillLevel] ?? $skillLevel;
                $skillColor = PlayerEvaluation::getSkillLevelColor($skillLevel);
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;" onmouseover="this.style.background='#F9FAFB'" onmouseout="this.style.background='transparent'">
                <td style="padding:12px 15px;color:#6B7280;"><?= $startIndex + $i + 1 ?></td>
                <td style="padding:12px 15px;font-weight:600;color:#1A1A2E;"><?= e($eval['player_name'] ?? '') ?></td>
                <td style="padding:12px 15px;"><?= e($eval['discipline_name'] ?? '') ?></td>
                <td style="padding:12px 15px;"><?= e($eval['coach_name'] ?? '—') ?></td>
                <td style="padding:12px 15px;"><?= e($eval['evaluation_date'] ?? '') ?></td>
                <td style="padding:12px 15px;">
                    <strong style="color:#1e3a5f;"><?= number_format((float)($eval['overall_score'] ?? 0), 1) ?></strong>/10
                </td>
                <td style="padding:12px 15px;">
                    <?php if ($skillLevel): ?>
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $skillColor ?>15;color:<?= $skillColor ?>;"><?= e($skillLabel) ?></span>
                    <?php else: ?>—<?php endif; ?>
                </td>
                <td style="padding:12px 15px;">
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $statusColor ?>15;color:<?= $statusColor ?>;"><?= e($statusLabel) ?></span>
                </td>
                <td style="padding:12px 15px;white-space:nowrap;">
                    <a href="/evaluations/<?= (int) $eval['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                        <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                    </a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="padding:15px;">
    <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
</div>
<?php endif; ?>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="clipboard-check" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد تقييمات</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">
        <?php if (!empty($filters['q']) || !empty($filters['discipline_id']) || !empty($filters['status'])): ?>
            لا توجد نتائج مطابقة. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة تقييم جديد للاعبين.
        <?php endif; ?>
    </p>
    <a href="/evaluations/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تقييم جديد</a>
</div>
<?php endif; ?>

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