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

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.coach.manage')): ?>
<a href="/sa/coaches/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 -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/coaches" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:250px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($search ?? '') ?>" placeholder="ابحث بالاسم، الرقم القومي، أو الكود..." class="form-input">
        </div>
        <div style="min-width:160px;">
            <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'] ?>" <?= ($disciplineFilter ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <?php if (($search ?? '') !== '' || ($disciplineFilter ?? '') !== ''): ?>
        <a href="/sa/coaches" class="btn btn-outline" style="color:#6B7280;">مسح</a>
        <?php endif; ?>
    </form>
</div>

<!-- Results -->
<?php if (!empty($coaches)): ?>
<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
                $employmentTypes = \App\Modules\SportsActivity\Models\Coach::getEmploymentTypeOptions();
                foreach ($coaches as $coach):
                    $isActive = (int) ($coach['is_active'] ?? 0);
                    $empType = $employmentTypes[$coach['employment_type'] ?? ''] ?? '-';
                ?>
                <tr>
                    <td><code style="font-size:12px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($coach['code'] ?? '') ?></code></td>
                    <td>
                        <a href="/sa/coaches/<?= (int) $coach['id'] ?>" style="color:#0D7377;text-decoration:none;font-weight:600;">
                            <?= e($coach['full_name_ar'] ?? '') ?>
                        </a>
                        <?php if (!empty($coach['full_name_en'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;"><?= e($coach['full_name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td style="direction:ltr;text-align:right;"><?= e($coach['phone'] ?? '-') ?></td>
                    <td><?= e($empType) ?></td>
                    <td>
                        <?php if (!empty($coach['discipline_names'])): ?>
                        <span style="font-size:12px;color:#374151;"><?= e($coach['discipline_names']) ?></span>
                        <?php else: ?>
                        <span style="font-size:12px;color:#9CA3AF;">-</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ($isActive): ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:#D1FAE5;color:#065F46;">نشط</span>
                        <?php else: ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:#FEE2E2;color:#DC2626;">معطّل</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div style="display:flex;gap:4px;">
                            <a href="/sa/coaches/<?= (int) $coach['id'] ?>" class="btn btn-sm btn-outline" title="عرض">
                                <i data-lucide="eye" style="width:14px;height:14px;"></i>
                            </a>
                            <?php if (can('sa.coach.manage')): ?>
                            <a href="/sa/coaches/<?= (int) $coach['id'] ?>/edit" class="btn btn-sm btn-outline" title="تعديل">
                                <i data-lucide="edit-3" style="width:14px;height:14px;"></i>
                            </a>
                            <form method="POST" action="/sa/coaches/<?= (int) $coach['id'] ?>/toggle" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-outline" title="<?= $isActive ? 'تعطيل' : 'تفعيل' ?>" onclick="return confirm('<?= $isActive ? 'هل أنت متأكد من تعطيل هذا المدرب؟' : 'هل أنت متأكد من تفعيل هذا المدرب؟' ?>');">
                                    <i data-lucide="<?= $isActive ? 'toggle-right' : 'toggle-left' ?>" style="width:14px;height:14px;"></i>
                                </button>
                            </form>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

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

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="user-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 (($search ?? '') !== ''): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة مدرب جديد.
        <?php endif; ?>
    </p>
    <?php if (can('sa.coach.manage')): ?>
    <a href="/sa/coaches/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(); ?>
