<?php
use App\Modules\Coaches\Models\Coach;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>المدربين<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/coaches/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 $currentType = $filters['employment_type'] ?? ''; ?>

<!-- Employment 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="/coaches"
           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 ($employmentTypes as $typeKey => $typeLabel): ?>
        <a href="/coaches?employment_type=<?= e($typeKey) ?><?= ($filters['q'] ?? '') !== '' ? '&q=' . urlencode($filters['q']) : '' ?>"
           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 Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/coaches" 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:200px;">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">نموذج الدفع</label>
            <select name="payment_model" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($paymentModels as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['payment_model'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if ($currentType !== ''): ?>
        <input type="hidden" name="employment_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="/coaches" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Coaches Grid -->
<?php if (!empty($coaches)): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(320px, 1fr));gap:20px;margin-bottom:20px;">
    <?php foreach ($coaches as $c):
        $isActive = (int) ($c['is_active'] ?? 0);
        $empType = $c['employment_type'] ?? 'contract';
        $empLabel = $employmentTypes[$empType] ?? $empType;
        $payModel = $c['payment_model'] ?? 'per_session';
        $payLabel = $paymentModels[$payModel] ?? $payModel;
        $empColors = ['staff' => '#059669', 'contract' => '#2563EB', 'freelance' => '#D97706'];
        $empColor = $empColors[$empType] ?? '#6B7280';
    ?>
    <div class="card" style="transition:box-shadow 0.2s;position:relative;overflow:hidden;<?= !$isActive ? 'opacity:0.65;' : '' ?>">
        <?php if (!$isActive): ?>
        <div style="position:absolute;top:12px;left:12px;z-index:2;">
            <span class="badge" style="background:#FEE2E2;color:#DC2626;font-size:11px;padding:3px 10px;border-radius:10px;">معطّل</span>
        </div>
        <?php endif; ?>

        <a href="/coaches/<?= (int) $c['id'] ?>" style="text-decoration:none;color:inherit;display:block;">
            <div style="padding:20px 20px 15px;display:flex;align-items:start;gap:15px;">
                <div style="width:52px;height:52px;border-radius:50%;background:linear-gradient(135deg, <?= $empColor ?>15, <?= $empColor ?>30);display:flex;align-items:center;justify-content:center;flex-shrink:0;overflow:hidden;">
                    <?php if (!empty($c['photo_path'])): ?>
                    <img src="<?= e($c['photo_path']) ?>" style="width:52px;height:52px;object-fit:cover;border-radius:50%;" alt="">
                    <?php else: ?>
                    <i data-lucide="user" style="width:26px;height:26px;color:<?= $empColor ?>;"></i>
                    <?php endif; ?>
                </div>
                <div style="flex:1;min-width:0;">
                    <h3 style="margin:0 0 4px;font-size:16px;font-weight:700;color:#1A1A2E;"><?= e($c['full_name_ar']) ?></h3>
                    <?php if (!empty($c['full_name_en'])): ?>
                    <div style="font-size:12px;color:#9CA3AF;margin-bottom:6px;"><?= e($c['full_name_en']) ?></div>
                    <?php endif; ?>
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $empColor ?>15;color:<?= $empColor ?>;"><?= e($empLabel) ?></span>
                </div>
            </div>

            <div style="padding:0 20px 15px;display:flex;gap:15px;font-size:12px;color:#6B7280;">
                <?php if (!empty($c['phone'])): ?>
                <span style="display:flex;align-items:center;gap:4px;">
                    <i data-lucide="phone" style="width:13px;height:13px;"></i>
                    <?= e($c['phone']) ?>
                </span>
                <?php endif; ?>
                <span style="display:flex;align-items:center;gap:4px;">
                    <i data-lucide="banknote" style="width:13px;height:13px;"></i>
                    <?= e($payLabel) ?>
                </span>
                <span style="display:flex;align-items:center;gap:4px;">
                    <i data-lucide="users" style="width:13px;height:13px;"></i>
                    حد أقصى: <?= (int) ($c['max_players_default'] ?? 20) ?>
                </span>
            </div>
        </a>

        <div style="padding:10px 20px;border-top:1px solid #F3F4F6;display:flex;justify-content:space-between;align-items:center;">
            <code style="font-size:11px;color:#9CA3AF;background:#F9FAFB;padding:2px 6px;border-radius:4px;"><?= e($c['code'] ?? '') ?></code>
            <div style="display:flex;gap:6px;">
                <a href="/coaches/<?= (int) $c['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>
                <a href="/coaches/<?= (int) $c['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>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</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="user" 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['employment_type']) || !empty($filters['payment_model'])): ?>
            لا توجد نتائج مطابقة. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة مدرب جديد.
        <?php endif; ?>
    </p>
    <a href="/coaches/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(); ?>
