<?php
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>تعديل بيانات اللاعب<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/players/<?= (int) $player['id'] ?>" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> رجوع</a>
<?php $__template->endSection(); ?>

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

<form method="POST" action="/sa/players/<?= (int) $player['id'] ?>/update">
    <?= csrf_field() ?>

    <!-- Basic Information -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:16px;font-weight:600;">البيانات الأساسية</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:repeat(auto-fit, minmax(250px, 1fr));gap:15px;">
            <div>
                <label class="form-label">نوع اللاعب <span style="color:#DC2626;">*</span></label>
                <select name="player_type" class="form-select" id="player_type" required>
                    <option value="">اختر...</option>
                    <option value="member" <?= (old('player_type') ?? $player['player_type']) === 'member' ? 'selected' : '' ?>>عضو</option>
                    <option value="non_member" <?= (old('player_type') ?? $player['player_type']) === 'non_member' ? 'selected' : '' ?>>غير عضو</option>
                </select>
            </div>
            <div id="member_id_field" style="<?= ($player['player_type'] ?? '') !== 'member' ? 'display:none;' : '' ?>">
                <label class="form-label">رقم العضوية <span style="color:#DC2626;">*</span></label>
                <input type="number" name="member_id" value="<?= e(old('member_id') ?? $player['member_id'] ?? '') ?>" class="form-input" placeholder="أدخل رقم العضوية">
            </div>
            <div>
                <label class="form-label">الاسم بالعربي <span style="color:#DC2626;">*</span></label>
                <input type="text" name="full_name_ar" value="<?= e(old('full_name_ar') ?? $player['full_name_ar'] ?? '') ?>" class="form-input" required maxlength="300">
            </div>
            <div>
                <label class="form-label">الاسم بالإنجليزي</label>
                <input type="text" name="full_name_en" value="<?= e(old('full_name_en') ?? $player['full_name_en'] ?? '') ?>" class="form-input" dir="ltr" maxlength="300">
            </div>
            <div>
                <label class="form-label">الرقم القومي</label>
                <input type="text" name="national_id" id="nidInput" value="<?= e(old('national_id') ?? $player['national_id'] ?? '') ?>" class="form-input" maxlength="14" dir="ltr">
                <small id="nidStatus" style="display:none;margin-top:4px;font-size:12px;"></small>
            </div>
            <div>
                <label class="form-label">تاريخ الميلاد</label>
                <input type="date" name="date_of_birth" id="dobInput" value="<?= e(old('date_of_birth') ?? $player['date_of_birth'] ?? '') ?>" class="form-input" readonly style="background:#F9FAFB;">
            </div>
            <div>
                <label class="form-label">النوع</label>
                <select id="genderSelect" class="form-select" disabled>
                    <option value="male" <?= (old('gender') ?? $player['gender'] ?? '') === 'male' ? 'selected' : '' ?>>ذكر</option>
                    <option value="female" <?= (old('gender') ?? $player['gender'] ?? '') === 'female' ? 'selected' : '' ?>>أنثى</option>
                </select>
                <input type="hidden" name="gender" id="genderHidden" value="<?= e(old('gender') ?? $player['gender'] ?? 'male') ?>">
            </div>
            <div>
                <label class="form-label">الهاتف</label>
                <input type="text" name="phone" value="<?= e(old('phone') ?? $player['phone'] ?? '') ?>" class="form-input" dir="ltr" maxlength="30">
            </div>
            <div>
                <label class="form-label">البريد الإلكتروني</label>
                <input type="email" name="email" value="<?= e(old('email') ?? $player['email'] ?? '') ?>" class="form-input" dir="ltr" maxlength="200">
            </div>
        </div>
    </div>

    <!-- Guardian Information -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:16px;font-weight:600;">بيانات ولي الأمر</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:repeat(auto-fit, minmax(250px, 1fr));gap:15px;">
            <div>
                <label class="form-label">اسم ولي الأمر</label>
                <input type="text" name="guardian_name" value="<?= e(old('guardian_name') ?? $player['guardian_name'] ?? '') ?>" class="form-input" maxlength="300">
            </div>
            <div>
                <label class="form-label">هاتف ولي الأمر</label>
                <input type="text" name="guardian_phone" value="<?= e(old('guardian_phone') ?? $player['guardian_phone'] ?? '') ?>" class="form-input" dir="ltr" maxlength="30">
            </div>
            <div>
                <label class="form-label">الرقم القومي لولي الأمر</label>
                <input type="text" name="guardian_national_id" value="<?= e(old('guardian_national_id') ?? $player['guardian_national_id'] ?? '') ?>" class="form-input" maxlength="14" dir="ltr">
            </div>
            <div>
                <label class="form-label">صلة القرابة</label>
                <select name="guardian_relationship" class="form-select">
                    <option value="">اختر...</option>
                    <option value="father" <?= (old('guardian_relationship') ?? $player['guardian_relationship'] ?? '') === 'father' ? 'selected' : '' ?>>أب</option>
                    <option value="mother" <?= (old('guardian_relationship') ?? $player['guardian_relationship'] ?? '') === 'mother' ? 'selected' : '' ?>>أم</option>
                    <option value="brother" <?= (old('guardian_relationship') ?? $player['guardian_relationship'] ?? '') === 'brother' ? 'selected' : '' ?>>أخ</option>
                    <option value="sister" <?= (old('guardian_relationship') ?? $player['guardian_relationship'] ?? '') === 'sister' ? 'selected' : '' ?>>أخت</option>
                    <option value="uncle" <?= (old('guardian_relationship') ?? $player['guardian_relationship'] ?? '') === 'uncle' ? 'selected' : '' ?>>عم / خال</option>
                    <option value="other" <?= (old('guardian_relationship') ?? $player['guardian_relationship'] ?? '') === 'other' ? 'selected' : '' ?>>أخرى</option>
                </select>
            </div>
        </div>
    </div>

    <!-- Notes -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:16px;font-weight:600;">ملاحظات</h3>
        </div>
        <div style="padding:20px;">
            <textarea name="notes" class="form-input" rows="4" placeholder="ملاحظات إضافية..."><?= e(old('notes') ?? $player['notes'] ?? '') ?></textarea>
        </div>
    </div>

    <div style="display:flex;gap:10px;justify-content:flex-start;">
        <button type="submit" class="btn btn-primary"><i data-lucide="save" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حفظ التعديلات</button>
        <a href="/sa/players/<?= (int) $player['id'] ?>" class="btn btn-outline">إلغاء</a>
    </div>
</form>

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

    var playerType = document.getElementById('player_type');
    var memberField = document.getElementById('member_id_field');

    function toggleMemberId() {
        memberField.style.display = playerType.value === 'member' ? '' : 'none';
    }
    playerType.addEventListener('change', toggleMemberId);
});
</script>

<script>
(function(){
    var nid = document.getElementById('nidInput');
    var dob = document.getElementById('dobInput');
    var genderSelect = document.getElementById('genderSelect');
    var genderHidden = document.getElementById('genderHidden');
    var status = document.getElementById('nidStatus');
    if (!nid) return;

    function parseNid(val) {
        if (val.length !== 14) { status.style.display = 'none'; dob.readOnly = false; dob.style.background = ''; genderSelect.disabled = false; return; }
        fetch('/api/members/parse-nid', {
            method: 'POST',
            headers: {'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest'},
            body: JSON.stringify({national_id: val})
        })
        .then(function(r){ return r.json(); })
        .then(function(d){
            if (d.success && d.data) {
                dob.value = d.data.dob || '';
                dob.readOnly = true;
                dob.style.background = '#F9FAFB';
                var g = d.data.gender || '';
                genderSelect.value = g;
                genderSelect.disabled = true;
                genderHidden.value = g;
                status.style.display = 'block';
                status.style.color = '#059669';
                status.textContent = (d.data.governorate || '') + ' — ' + (d.data.age || '') + ' سنة';
            } else {
                status.style.display = 'block';
                status.style.color = '#DC2626';
                status.textContent = d.message || 'رقم قومي غير صحيح';
            }
        }).catch(function(){});
    }

    nid.addEventListener('input', function(){ parseNid(this.value.trim()); });
    genderSelect.addEventListener('change', function(){ genderHidden.value = this.value; });
    if (nid.value.trim().length === 14) parseNid(nid.value.trim());
})();
</script>
<?php $__template->endSection(); ?>
