<?php
use App\Modules\PlayerAffairs\Models\Player;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>شئون اللاعبين<?php $__template->endSection(); ?>

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

<?php
$currentType = $filters['player_type'] ?? '';
$currentCardStatus = $filters['card_status'] ?? '';
$allPlayerTypes = Player::getPlayerTypes();
$allCardStatuses = Player::getCardStatuses();
?>

<!-- Player Type Filter Tabs -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="display:flex;align-items:center;gap:0;overflow-x:auto;border-bottom:2px solid #E5E7EB;">
        <a href="/players<?= ($filters['q'] ?? '') !== '' ? '?q=' . urlencode($filters['q']) : '' ?><?= $currentCardStatus !== '' ? (($filters['q'] ?? '') !== '' ? '&' : '?') . 'card_status=' . urlencode($currentCardStatus) : '' ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentType === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentType === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($allPlayerTypes as $typeKey => $typeLabel): ?>
        <?php
            $tabParams = [];
            if (($filters['q'] ?? '') !== '') $tabParams['q'] = $filters['q'];
            $tabParams['player_type'] = $typeKey;
            if ($currentCardStatus !== '') $tabParams['card_status'] = $currentCardStatus;
            $tabQuery = http_build_query($tabParams);
        ?>
        <a href="/players?<?= $tabQuery ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentType === $typeKey ? '#0D7377' : 'transparent' ?>;color:<?= $currentType === $typeKey ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            <?= e($typeLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search & Filter Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/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" style="min-width:250px;">
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">حالة الكارنيه</label>
            <select name="card_status" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($allCardStatuses as $csKey => $csLabel): ?>
                <option value="<?= e($csKey) ?>" <?= $currentCardStatus === $csKey ? 'selected' : '' ?>><?= e($csLabel) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if ($currentType !== ''): ?>
        <input type="hidden" name="player_type" value="<?= e($currentType) ?>">
        <?php endif; ?>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/players" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Players Table -->
<?php if (!empty($players)): ?>
<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;white-space:nowrap;">#</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">رقم التسلسل</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">الاسم</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">النوع</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">الرقم القومي</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">الهاتف</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">حالة الكارنيه</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">الحالة الطبية</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;white-space:nowrap;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $startIndex = (($pagination['current_page'] ?? 1) - 1) * ($pagination['per_page'] ?? 25);
            foreach ($players as $i => $p):
                $cardStatus = $p['card_status'] ?? 'inactive';
                $medicalStatus = $p['medical_status'] ?? 'pending';
                $cardColor = Player::getCardStatusColor($cardStatus);
                $cardLabel = Player::getCardStatusLabel($cardStatus);
                $medicalColor = Player::getMedicalStatusColor($medicalStatus);
                $medicalLabel = Player::getMedicalStatusLabel($medicalStatus);
                $playerType = $p['player_type'] ?? '';
                $typeLabel = ($allPlayerTypes[$playerType] ?? $playerType);
                $typeColor = $playerType === 'member' ? '#0284C7' : '#7C3AED';
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;transition:background 0.15s;" 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;">
                    <code style="font-size:12px;color:#6B7280;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($p['registration_serial'] ?? '—') ?></code>
                </td>
                <td style="padding:12px 15px;">
                    <div style="font-weight:600;color:#1A1A2E;"><?= e($p['full_name_ar'] ?? '') ?></div>
                    <?php if (!empty($p['full_name_en'])): ?>
                    <div style="font-size:12px;color:#9CA3AF;"><?= e($p['full_name_en']) ?></div>
                    <?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:<?= $typeColor ?>15;color:<?= $typeColor ?>;"><?= e($typeLabel) ?></span>
                </td>
                <td style="padding:12px 15px;color:#374151;font-size:13px;"><?= e($p['national_id'] ?? '—') ?></td>
                <td style="padding:12px 15px;color:#374151;font-size:13px;" dir="ltr"><?= e($p['phone'] ?? '—') ?></td>
                <td style="padding:12px 15px;">
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $cardColor ?>15;color:<?= $cardColor ?>;"><?= e($cardLabel) ?></span>
                </td>
                <td style="padding:12px 15px;">
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $medicalColor ?>15;color:<?= $medicalColor ?>;"><?= e($medicalLabel) ?></span>
                </td>
                <td style="padding:12px 15px;white-space:nowrap;">
                    <div style="display:flex;gap:6px;">
                        <a href="/players/<?= (int) $p['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>
                        <?php if (can('sa.player.manage')): ?>
                        <a href="/players/<?= (int) $p['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="edit-3" style="width:13px;height:13px;vertical-align:middle;"></i> تعديل
                        </a>
                        <?php endif; ?>
                    </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]); ?>
</div>
<?php endif; ?>

<?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['card_status'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بتسجيل لاعب جديد في النادي.
        <?php endif; ?>
    </p>
    <?php if (can('sa.player.manage')): ?>
    <a href="/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(); ?>
