<?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:#0D7377;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:#0D7377;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>

    <!-- 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:#0D7377;">عرض</a>
            </small>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

    <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(); ?>