<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تعديل العضو: <?= e($member->full_name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<form method="POST" action="/members/<?= (int) $member->id ?>" enctype="multipart/form-data">
    <?= csrf_field() ?>

    <?php if (!empty($isSuperAdmin)): ?>
    <!-- ═══ SuperAdmin Only: Name & National ID ═══ -->
    <div class="card" style="margin-bottom:20px;padding:20px;border-right:4px solid #DC2626;">
        <div style="display:flex;align-items:center;gap:10px;margin-bottom:15px;">
            <h3 style="color:#DC2626;margin:0;">🔒 صلاحيات المشرف العام</h3>
            <span style="background:#FEF2F2;color:#DC2626;padding:3px 10px;border-radius:4px;font-size:11px;font-weight:600;">SUPER ADMIN ONLY</span>
        </div>
        <p style="color:#6B7280;font-size:13px;margin-bottom:15px;">⚠ تغيير هذه البيانات يؤثر على كل سجلات العضو — تأكد من صحة البيانات قبل الحفظ</p>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div class="form-group">
                <label class="form-label">الاسم بالعربي <span style="color:#DC2626;">*</span></label>
                <input type="text" name="full_name_ar" value="<?= e($member->full_name_ar) ?>" class="form-input" style="font-size:16px;font-weight:600;border-color:#DC2626;" required minlength="5" maxlength="200">
            </div>
            <div class="form-group">
                <label class="form-label">الرقم القومي</label>
                <input type="text" name="national_id" value="<?= e($member->national_id ?? '') ?>" class="form-input" maxlength="14" style="direction:ltr;text-align:left;font-size:16px;letter-spacing:1px;border-color:#DC2626;">
                <small style="color:#DC2626;font-size:11px;">تغيير الرقم القومي سيعيد حساب تاريخ الميلاد والنوع والمحافظة</small>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <div class="card" style="margin-bottom:20px;padding:20px;">
        <h3 style="color:#1e3a5f;margin-bottom:15px;">البيانات الأساسية (للقراءة فقط)</h3>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <?php if (empty($isSuperAdmin)): ?>
            <div class="form-group"><label class="form-label">الاسم بالعربي 🔒</label><input type="text" value="<?= e($member->full_name_ar) ?>" class="form-input" disabled style="background:#F3F4F6;"></div>
            <div class="form-group"><label class="form-label">الرقم القومي 🔒</label><input type="text" value="<?= e($member->national_id ?: '—') ?>" class="form-input" disabled style="background:#F3F4F6;direction:ltr;text-align:left;"></div>
            <?php endif; ?>
            <div class="form-group"><label class="form-label">تاريخ الميلاد</label><input type="text" value="<?= e($member->date_of_birth) ?>" class="form-input" disabled style="background:#F3F4F6;"></div>
            <div class="form-group"><label class="form-label">النوع</label><input type="text" value="<?= e($member->getGenderLabel()) ?>" class="form-input" disabled style="background:#F3F4F6;"></div>
        </div>
    </div>

    <div class="card" style="margin-bottom:20px;padding:20px;">
        <h3 style="color:#1e3a5f;margin-bottom:15px;">البيانات القابلة للتعديل</h3>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div class="form-group"><label class="form-label">الاسم بالإنجليزي</label><input type="text" name="full_name_en" value="<?= e($member->full_name_en ?? '') ?>" class="form-input"></div>
            <div class="form-group"><label class="form-label">الحالة الاجتماعية</label>
                <select name="marital_status" class="form-select"><option value="">—</option><option value="single" <?= $member->marital_status === 'single' ? 'selected' : '' ?>>أعزب</option><option value="married" <?= $member->marital_status === 'married' ? 'selected' : '' ?>>متزوج</option><option value="divorced" <?= $member->marital_status === 'divorced' ? 'selected' : '' ?>>مطلق</option><option value="widowed" <?= $member->marital_status === 'widowed' ? 'selected' : '' ?>>أرمل</option></select>
            </div>
            <div class="form-group"><label class="form-label">رقم المحمول <span style="color:#DC2626;">*</span></label><input type="tel" name="phone_mobile" value="<?= e($member->phone_mobile) ?>" class="form-input" required style="direction:ltr;text-align:left;"></div>
            <div class="form-group"><label class="form-label">تليفون المنزل</label><input type="tel" name="phone_home" value="<?= e($member->phone_home ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;"></div>
            <div class="form-group"><label class="form-label">البريد الإلكتروني</label><input type="email" name="email" value="<?= e($member->email ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;"></div>
            <div class="form-group"><label class="form-label">شخص للطوارئ</label><input type="text" name="emergency_name" value="<?= e($member->emergency_name ?? '') ?>" class="form-input"></div>
            <div class="form-group"><label class="form-label">هاتف الطوارئ</label><input type="tel" name="emergency_phone" value="<?= e($member->emergency_phone ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;"></div>
            <div class="form-group"><label class="form-label">المهنة</label><input type="text" name="occupation" value="<?= e($member->occupation ?? '') ?>" class="form-input"></div>
            <div class="form-group"><label class="form-label">المركز الوظيفي</label><input type="text" name="job_title" value="<?= e($member->job_title ?? '') ?>" class="form-input"></div>
            <div class="form-group" style="grid-column:1/-1;"><label class="form-label">عنوان السكن</label><textarea name="residence_address" class="form-textarea" rows="2"><?= e($member->residence_address ?? '') ?></textarea></div>
            <div class="form-group">
                <label class="form-label">المحافظة</label>
                <select name="governorate" class="form-select"><option value="">—</option>
                    <?php foreach ($governorates as $g): ?><option value="<?= e($g['name_ar']) ?>" <?= $member->governorate === $g['name_ar'] ? 'selected' : '' ?>><?= e($g['name_ar']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="form-group"><label class="form-label">المنطقة</label><input type="text" name="area" value="<?= e($member->area ?? '') ?>" class="form-input"></div>
        </div>
    </div>

    <!-- Referral Source & Sales Rep -->
    <div class="card" style="margin-bottom:20px;padding:20px;">
        <h3 style="color:#1e3a5f;margin-bottom:15px;">كيف تعرفت على النادي</h3>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div class="form-group">
                <label class="form-label">مصدر المعرفة</label>
                <select name="referral_source" class="form-select">
                    <option value="">-- اختر --</option>
                    <option value="social_media" <?= ($member->referral_source ?? '') === 'social_media' ? 'selected' : '' ?>>مواقع التواصل الاجتماعي</option>
                    <option value="tv" <?= ($member->referral_source ?? '') === 'tv' ? 'selected' : '' ?>>إعلان التليفزيون</option>
                    <option value="friend" <?= ($member->referral_source ?? '') === 'friend' ? 'selected' : '' ?>>من خلال صديق</option>
                    <option value="radio" <?= ($member->referral_source ?? '') === 'radio' ? 'selected' : '' ?>>إعلان الراديو</option>
                    <option value="outdoor" <?= ($member->referral_source ?? '') === 'outdoor' ? 'selected' : '' ?>>إعلانات الطريق</option>
                    <option value="member" <?= ($member->referral_source ?? '') === 'member' ? 'selected' : '' ?>>عضو بالنادي</option>
                    <option value="other" <?= ($member->referral_source ?? '') === 'other' ? 'selected' : '' ?>>أخرى</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">موظف المبيعات المُحيل</label>
                <select name="referred_by_representative_id" class="form-select">
                    <option value="">-- لا يوجد --</option>
                    <?php foreach ($salesReps as $rep): ?>
                    <option value="<?= (int) $rep['id'] ?>" <?= (int) ($member->referred_by_representative_id ?? 0) === (int) $rep['id'] ? 'selected' : '' ?>><?= e($rep['name_ar']) ?> (<?= e($rep['code']) ?>)</option>
                    <?php endforeach; ?>
                </select>
                <small style="color:#6B7280;display:block;margin-top:4px;">اختر موظف المبيعات إذا كان العضو جاي عن طريقه</small>
            </div>
        </div>
    </div>

    <!-- Special Discount -->
    <?php if (!empty($specialDiscounts)): ?>
    <div class="card" style="margin-bottom:20px;padding:20px;border-right:4px solid #D97706;">
        <div style="display:flex;align-items:center;gap:8px;margin-bottom:15px;">
            <i data-lucide="percent" style="width:18px;height:18px;color:#D97706;"></i>
            <h3 style="margin:0;color:#D97706;font-size:15px;">خصم خاص</h3>
        </div>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div class="form-group">
                <label class="form-label">نوع الخصم الخاص</label>
                <select name="special_discount_id" id="special_discount_select" class="form-select">
                    <option value="">— بدون خصم —</option>
                    <?php foreach ($specialDiscounts as $sd): ?>
                    <option value="<?= (int) $sd['id'] ?>"
                        data-pct="<?= e($sd['discount_percentage']) ?>"
                        data-doc="<?= (int) $sd['requires_document'] ?>"
                        <?= ((int) ($member->special_discount_id ?? 0)) === (int) $sd['id'] ? 'selected' : '' ?>>
                        <?= e($sd['name_ar']) ?> (<?= e($sd['discount_percentage']) ?>%)
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group" id="discount-info" style="display:<?= $member->special_discount_id ? 'block' : 'none' ?>;">
                <label class="form-label">قيمة الخصم</label>
                <div id="discount-amount" style="font-size:18px;font-weight:700;color:#059669;padding:8px 0;">
                    <?php if ($member->discount_amount && bccomp($member->discount_amount, '0', 2) > 0): ?>
                        <?= money($member->discount_amount) ?>
                    <?php else: ?>
                        —
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div class="form-group" id="discount-doc-group" style="display:none;margin-top:10px;">
            <label class="form-label">مستند إثبات الخصم (PDF, JPG, PNG) <span style="color:#DC2626;">*</span></label>
            <input type="file" name="discount_document" id="discount_document" class="form-input" accept=".pdf,.jpg,.jpeg,.png" style="padding:8px;">
            <?php if (!empty($member->special_discount_document)): ?>
            <small style="color:#059669;font-size:12px;display:block;margin-top:4px;">
                <i data-lucide="file-check" style="width:12px;height:12px;vertical-align:middle;"></i>
                مستند مرفق بالفعل — <a href="/<?= e($member->special_discount_document) ?>" target="_blank" style="color:#1e3a5f;">عرض</a>
            </small>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Profile Photo -->
    <div class="card" style="margin-bottom:20px;padding:20px;border-right:4px solid #7C3AED;">
        <h3 style="color:#7C3AED;margin-bottom:15px;">الصورة الشخصية</h3>
        <?php $currentPhoto = $member->photo_path ?? null; $required = false; $fieldName = 'photo'; $label = 'الصورة الشخصية'; include \App\Core\App::getInstance()->basePath() . '/app/Shared/Components/photo-upload-field.php'; ?>
    </div>

    <button type="submit" class="btn btn-primary">حفظ التعديلات</button>
    <a href="/members/<?= (int) $member->id ?>" class="btn btn-outline">إلغاء</a>
</form>

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

    var sel = document.getElementById('special_discount_select');
    if (!sel) return;
    var infoDiv = document.getElementById('discount-info');
    var amountDiv = document.getElementById('discount-amount');
    var docGroup = document.getElementById('discount-doc-group');
    var membershipValue = <?= json_encode($member->membership_value ?? '0.00') ?>;

    function updateDiscount() {
        var opt = sel.options[sel.selectedIndex];
        if (!opt || !opt.value) {
            infoDiv.style.display = 'none';
            docGroup.style.display = 'none';
            return;
        }
        var pct = parseFloat(opt.getAttribute('data-pct') || '0');
        var needsDoc = parseInt(opt.getAttribute('data-doc') || '0');
        var val = parseFloat(membershipValue) || 0;
        var discountAmt = (val * pct / 100).toFixed(2);
        amountDiv.textContent = discountAmt + ' ج.م';
        infoDiv.style.display = 'block';
        docGroup.style.display = needsDoc ? 'block' : 'none';
    }

    sel.addEventListener('change', updateDiscount);
    updateDiscount();
});
</script>
<?php $__template->endSection(); ?>