<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إضافة مدرب جديد<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/coaches" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

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

<form method="POST" action="/coaches" enctype="multipart/form-data" id="coachForm">
    <?= csrf_field() ?>

    <!-- Basic Information -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="user" style="width:18px;height:18px;color:#0D7377;"></i>
            <h3 style="margin:0;color:#0D7377;font-size:15px;">البيانات الأساسية</h3>
        </div>
        <div style="padding:20px;">
            <!-- Photo Upload -->
            <div style="display:flex;align-items:center;gap:20px;margin-bottom:20px;">
                <label style="cursor:pointer;">
                    <div id="photoPreview" style="width:80px;height:80px;border-radius:50%;background:#F3F4F6;display:flex;align-items:center;justify-content:center;overflow:hidden;border:3px dashed #D1D5DB;">
                        <i data-lucide="camera" style="width:24px;height:24px;color:#9CA3AF;"></i>
                    </div>
                    <input type="file" name="photo" accept="image/jpeg,image/png,image/webp" style="display:none;" id="photoInput">
                </label>
                <div>
                    <div style="font-size:13px;font-weight:600;color:#1A1A2E;">صورة المدرب</div>
                    <div style="font-size:11px;color:#9CA3AF;">JPG, PNG أو WebP — حد أقصى 5MB</div>
                </div>
            </div>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">الاسم بالعربي <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="full_name_ar" value="<?= e(old('full_name_ar') ?? '') ?>" class="form-input" required>
                </div>
                <div class="form-group">
                    <label class="form-label">الاسم بالإنجليزي</label>
                    <input type="text" name="full_name_en" value="<?= e(old('full_name_en') ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;">
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">كود المدرب <span style="color:#9CA3AF;font-size:11px;">(اختياري)</span></label>
                    <input type="text" name="code" value="<?= e(old('code') ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;text-transform:uppercase;" placeholder="يُولّد تلقائياً">
                </div>
                <div class="form-group">
                    <label class="form-label">الرقم القومي</label>
                    <input type="text" name="national_id" value="<?= e(old('national_id') ?? '') ?>" class="form-input" maxlength="14" style="direction:ltr;text-align:left;" id="nidInput">
                </div>
                <div class="form-group">
                    <label class="form-label">الهاتف</label>
                    <input type="text" name="phone" value="<?= e(old('phone') ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;">
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">البريد الإلكتروني</label>
                    <input type="email" name="email" value="<?= e(old('email') ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">تاريخ الميلاد</label>
                    <input type="date" name="date_of_birth" value="<?= e(old('date_of_birth') ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;" id="dobInput">
                </div>
                <div class="form-group">
                    <label class="form-label">النوع</label>
                    <select name="gender" class="form-select">
                        <option value="">-- اختر --</option>
                        <option value="male" <?= old('gender') === 'male' ? 'selected' : '' ?>>ذكر</option>
                        <option value="female" <?= old('gender') === 'female' ? 'selected' : '' ?>>أنثى</option>
                    </select>
                </div>
            </div>
        </div>
    </div>

    <!-- Employment & Payment -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="briefcase" style="width:18px;height:18px;color:#D97706;"></i>
            <h3 style="margin:0;color:#D97706;font-size:15px;">التوظيف والدفع</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">نوع التوظيف <span style="color:#DC2626;">*</span></label>
                    <select name="employment_type" class="form-select" required>
                        <?php foreach ($employmentTypes as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= (old('employment_type') ?? 'contract') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">نموذج الدفع <span style="color:#DC2626;">*</span></label>
                    <select name="payment_model" class="form-select" required>
                        <?php foreach ($paymentModels as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= (old('payment_model') ?? 'per_session') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">الحد الأقصى للاعبين</label>
                    <input type="number" name="max_players_default" value="<?= e(old('max_players_default') ?? '20') ?>" class="form-input" min="1" max="100" style="direction:ltr;text-align:left;">
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">سعر الساعة (ج.م)</label>
                    <input type="number" name="hourly_rate" value="<?= e(old('hourly_rate') ?? '') ?>" class="form-input" min="0" step="0.01" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">سعر الحصة (ج.م)</label>
                    <input type="number" name="session_rate" value="<?= e(old('session_rate') ?? '') ?>" class="form-input" min="0" step="0.01" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">الراتب الشهري (ج.م)</label>
                    <input type="number" name="monthly_rate" value="<?= e(old('monthly_rate') ?? '') ?>" class="form-input" min="0" step="0.01" style="direction:ltr;text-align:left;">
                </div>
            </div>
        </div>
    </div>

    <!-- Disciplines -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="activity" style="width:18px;height:18px;color:#2563EB;"></i>
            <h3 style="margin:0;color:#2563EB;font-size:15px;">التخصصات الرياضية</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:flex;flex-wrap:wrap;gap:10px;">
                <?php foreach ($disciplines as $disc): ?>
                <label style="display:flex;align-items:center;gap:6px;padding:8px 14px;border:1px solid #E5E7EB;border-radius:8px;cursor:pointer;transition:all 0.2s;">
                    <input type="checkbox" name="discipline_ids[]" value="<?= (int) $disc['id'] ?>" style="width:16px;height:16px;accent-color:#0D7377;">
                    <span style="font-size:13px;"><?= e($disc['name_ar']) ?></span>
                </label>
                <?php endforeach; ?>
            </div>
            <?php if (empty($disciplines)): ?>
            <p style="color:#9CA3AF;font-size:13px;margin:0;">لا توجد أنشطة رياضية. أضف الأنشطة أولاً من صفحة الأنشطة الرياضية.</p>
            <?php endif; ?>
        </div>
    </div>

    <!-- Bio -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="file-text" style="width:18px;height:18px;color:#059669;"></i>
            <h3 style="margin:0;color:#059669;font-size:15px;">نبذة وشهادات</h3>
        </div>
        <div style="padding:20px;">
            <div class="form-group">
                <label class="form-label">نبذة عن المدرب</label>
                <textarea name="bio_ar" class="form-input" rows="3"><?= e(old('bio_ar') ?? '') ?></textarea>
            </div>
            <div class="form-group" style="margin-top:15px;">
                <label class="form-label">الشهادات والتراخيص</label>
                <div id="certsContainer">
                    <div style="display:flex;gap:8px;margin-bottom:8px;">
                        <input type="text" name="certifications[]" class="form-input" placeholder="مثال: رخصة تدريب AFC-B" style="flex:1;">
                        <button type="button" onclick="addCertRow()" class="btn btn-outline" style="padding:8px 12px;"><i data-lucide="plus" style="width:14px;height:14px;"></i></button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Submit -->
    <div style="display:flex;gap:10px;justify-content:flex-start;">
        <button type="submit" class="btn btn-primary" style="padding:12px 30px;font-size:15px;">
            <i data-lucide="check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حفظ المدرب
        </button>
        <a href="/coaches" class="btn btn-outline" style="padding:12px 30px;font-size:15px;">إلغاء</a>
    </div>
</form>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();

    var photoInput = document.getElementById('photoInput');
    if (photoInput) {
        photoInput.addEventListener('change', function() {
            var file = this.files[0];
            if (!file) return;
            var reader = new FileReader();
            reader.onload = function(e) {
                document.getElementById('photoPreview').innerHTML = '<img src="' + e.target.result + '" style="width:80px;height:80px;object-fit:cover;border-radius:50%;">';
            };
            reader.readAsDataURL(file);
        });
    }

    var nidInput = document.getElementById('nidInput');
    if (nidInput) {
        nidInput.addEventListener('input', function() {
            var v = this.value.replace(/\D/g, '');
            if (v.length === 14) {
                var century = v[0] === '2' ? '19' : '20';
                var year = century + v.substring(1,3);
                var month = v.substring(3,5);
                var day = v.substring(5,7);
                var dob = year + '-' + month + '-' + day;
                var dobInput = document.getElementById('dobInput');
                if (dobInput) dobInput.value = dob;
                var genderDigit = parseInt(v[12]);
                var genderSelect = document.querySelector('[name=gender]');
                if (genderSelect) genderSelect.value = (genderDigit % 2 === 1) ? 'male' : 'female';
            }
        });
    }
});

function addCertRow() {
    var container = document.getElementById('certsContainer');
    var div = document.createElement('div');
    div.style.cssText = 'display:flex;gap:8px;margin-bottom:8px;';
    div.innerHTML = '<input type="text" name="certifications[]" class="form-input" placeholder="شهادة إضافية..." style="flex:1;"><button type="button" onclick="this.parentElement.remove()" class="btn btn-outline" style="padding:8px 12px;color:#DC2626;"><i data-lucide="x" style="width:14px;height:14px;"></i></button>';
    container.appendChild(div);
    if (typeof lucide !== 'undefined') lucide.createIcons();
}
</script>

<?php $__template->endSection(); ?>
