<?php
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>اللاعبون<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.player.manage')): ?>
<a href="/sa/players/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'); ?>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/players" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:200px;">
            <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-select">
                <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:150px;">
            <label class="form-label" style="font-size:12px;">المجموعة</label>
            <select name="group_id" class="form-select"
                    data-searchable="true"
                    data-cascade-from="discipline_id"
                    data-cascade-url="/api/sa/groups/search?active_only=1&discipline_id={value}"
                    data-placeholder="-- الكل --">
                <option value="">الكل</option>
                <?php foreach ($groups as $g): ?>
                <option value="<?= (int) $g['id'] ?>" <?= ($filters['group_id'] ?? '') == $g['id'] ? 'selected' : '' ?>><?= e($g['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">نوع اللاعب</label>
            <select name="player_type" class="form-select">
                <option value="">الكل</option>
                <option value="member" <?= ($filters['player_type'] ?? '') === 'member' ? 'selected' : '' ?>>عضو</option>
                <option value="non_member" <?= ($filters['player_type'] ?? '') === 'non_member' ? 'selected' : '' ?>>غير عضو</option>
            </select>
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">الحالة الطبية</label>
            <select name="medical_status" class="form-select">
                <option value="">الكل</option>
                <option value="fit" <?= ($filters['medical_status'] ?? '') === 'fit' ? 'selected' : '' ?>>لائق</option>
                <option value="unfit" <?= ($filters['medical_status'] ?? '') === 'unfit' ? 'selected' : '' ?>>غير لائق</option>
                <option value="pending" <?= ($filters['medical_status'] ?? '') === 'pending' ? 'selected' : '' ?>>في الانتظار</option>
                <option value="expired" <?= ($filters['medical_status'] ?? '') === 'expired' ? 'selected' : '' ?>>منتهي</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/sa/players" class="btn btn-outline">مسح</a>
    </form>
</div>

<!-- Players Table -->
<?php if (!empty($players)): ?>
<div class="card">
    <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 foreach ($players as $player): ?>
                <tr>
                    <td><code style="font-size:12px;"><?= e($player['registration_serial'] ?? '') ?></code></td>
                    <td>
                        <a href="/sa/players/<?= (int) $player['id'] ?>" style="text-decoration:none;color:#0D7377;font-weight:600;">
                            <?= e($player['full_name_ar'] ?? '') ?>
                        </a>
                        <?php if (!in_array($player['medical_status'] ?? 'pending', ['fit', 'conditional'])): ?>
                        <i data-lucide="alert-triangle" style="width:14px;height:14px;color:#D97706;vertical-align:middle;margin-right:4px;" title="بدون شهادة طبية سارية"></i>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php
                        $typeLabels = ['member' => 'عضو', 'non_member' => 'غير عضو'];
                        $typeColors = ['member' => '#059669', 'non_member' => '#D97706'];
                        $pType = $player['player_type'] ?? '';
                        $tColor = $typeColors[$pType] ?? '#6B7280';
                        ?>
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $tColor ?>15;color:<?= $tColor ?>;">
                            <?= e($typeLabels[$pType] ?? $pType) ?>
                        </span>
                    </td>
                    <td style="font-size:13px;"><?= e($player['national_id'] ?? '-') ?></td>
                    <td>
                        <?php
                        $medLabels = ['fit' => 'لائق', 'unfit' => 'غير لائق', 'pending' => 'في الانتظار', 'expired' => 'منتهي'];
                        $medColors = ['fit' => '#059669', 'unfit' => '#DC2626', 'pending' => '#D97706', 'expired' => '#6B7280'];
                        $mStatus = $player['medical_status'] ?? 'pending';
                        $mColor = $medColors[$mStatus] ?? '#6B7280';
                        ?>
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $mColor ?>15;color:<?= $mColor ?>;">
                            <?= e($medLabels[$mStatus] ?? $mStatus) ?>
                        </span>
                    </td>
                    <td>
                        <?php
                        $cardLabels = ['inactive' => 'غير مفعلة', 'active' => 'فعالة', 'suspended' => 'موقوفة', 'revoked' => 'ملغاة'];
                        $cardColors = ['inactive' => '#6B7280', 'active' => '#059669', 'suspended' => '#D97706', 'revoked' => '#DC2626'];
                        $cStatus = $player['card_status'] ?? 'inactive';
                        $cColor = $cardColors[$cStatus] ?? '#6B7280';
                        ?>
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $cColor ?>15;color:<?= $cColor ?>;">
                            <?= e($cardLabels[$cStatus] ?? $cStatus) ?>
                        </span>
                    </td>
                    <td>
                        <div style="display:flex;gap:6px;">
                            <a href="/sa/players/<?= (int) $player['id'] ?>" class="btn btn-sm btn-outline" title="عرض">
                                <i data-lucide="eye" style="width:14px;height:14px;"></i>
                            </a>
                            <?php if (can('sa.player.manage')): ?>
                            <a href="/sa/players/<?= (int) $player['id'] ?>/edit" class="btn btn-sm btn-outline" title="تعديل">
                                <i data-lucide="edit-3" style="width:14px;height:14px;"></i>
                            </a>
                            <?php endif; ?>
                            <a href="/sa/players/<?= (int) $player['id'] ?>/documents" class="btn btn-sm btn-outline" title="المستندات">
                                <i data-lucide="file-text" style="width:14px;height:14px;"></i>
                            </a>
                        </div>
                    </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, 'baseUrl' => '/sa/players?' . http_build_query(array_filter($filters))]); ?>
    </div>
    <?php endif; ?>
</div>
<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="users" 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['player_type']) || !empty($filters['medical_status'])): ?>
            لا توجد نتائج مطابقة لبحثك.
        <?php else: ?>
            ابدأ بإضافة لاعب جديد.
        <?php endif; ?>
    </p>
    <?php if (can('sa.player.manage')): ?>
    <a href="/sa/players/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة لاعب</a>
    <?php endif; ?>
</div>
<?php endif; ?>

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