<?php
use App\Modules\Achievements\Models\AchievementDefinition;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>إدارة الإنجازات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/achievements/admin/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إنجاز جديد</a>
<?php $__template->endSection(); ?>

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

<?php $currentCategory = $filters['category'] ?? ''; ?>

<!-- Category 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="/achievements/admin"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentCategory === '' ? '#1e3a5f' : 'transparent' ?>;color:<?= $currentCategory === '' ? '#1e3a5f' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($categories as $catKey => $catLabel): ?>
        <a href="/achievements/admin?category=<?= e($catKey) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentCategory === $catKey ? '#1e3a5f' : 'transparent' ?>;color:<?= $currentCategory === $catKey ? '#1e3a5f' : '#6B7280' ?>;white-space:nowrap;">
            <?= e($catLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/achievements/admin" 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>
        <?php if ($currentCategory !== ''): ?>
        <input type="hidden" name="category" value="<?= e($currentCategory) ?>">
        <?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="/achievements/admin" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Definitions Table -->
<?php if (!empty($definitions)): ?>
<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>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($definitions as $def):
                $isActive = (int) ($def['is_active'] ?? 0);
                $badgeColor = $def['badge_color'] ?? 'gold';
                if (strpos($badgeColor, '#') !== 0) {
                    $badgeColor = AchievementDefinition::getBadgeColors()[$badgeColor] ?? '#F59E0B';
                }
                $catLabel = $categories[$def['category'] ?? ''] ?? ($def['category'] ?? '');
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;<?= !$isActive ? 'opacity:0.6;' : '' ?>" onmouseover="this.style.background='#F9FAFB'" onmouseout="this.style.background='transparent'">
                <td style="padding:12px 15px;">
                    <div style="display:flex;align-items:center;gap:10px;">
                        <div style="width:36px;height:36px;border-radius:50%;background:<?= $badgeColor ?>20;display:flex;align-items:center;justify-content:center;">
                            <i data-lucide="<?= e($def['icon'] ?? 'award') ?>" style="width:18px;height:18px;color:<?= $badgeColor ?>;"></i>
                        </div>
                        <div>
                            <div style="font-weight:600;color:#1A1A2E;"><?= e($def['name_ar'] ?? '') ?></div>
                            <?php if (!empty($def['description_ar'])): ?>
                            <div style="font-size:11px;color:#6B7280;max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><?= e($def['description_ar']) ?></div>
                            <?php endif; ?>
                        </div>
                    </div>
                </td>
                <td style="padding:12px 15px;">
                    <code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($def['code'] ?? '') ?></code>
                </td>
                <td style="padding:12px 15px;font-size:13px;"><?= e($catLabel) ?></td>
                <td style="padding:12px 15px;font-size:13px;"><?= e($def['discipline_name'] ?? 'عام') ?></td>
                <td style="padding:12px 15px;font-weight:600;color:#F59E0B;"><?= (int) ($def['points'] ?? 0) ?></td>
                <td style="padding:12px 15px;">
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $isActive ? '#05966915' : '#DC262615' ?>;color:<?= $isActive ? '#059669' : '#DC2626' ?>;">
                        <?= $isActive ? 'مفعّل' : 'معطّل' ?>
                    </span>
                </td>
                <td style="padding:12px 15px;">
                    <form method="POST" action="/achievements/admin/<?= (int) $def['id'] ?>/toggle" style="display:inline;">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <?= $isActive ? 'تعطيل' : 'تفعيل' ?>
                        </button>
                    </form>
                </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="award" 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;">ابدأ بإنشاء تعريفات الإنجازات التي يمكن للاعبين الحصول عليها.</p>
    <a href="/achievements/admin/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(); ?>
