<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>ملء الاستمارة — <?= e($member->full_name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<!-- Header -->
<div class="card" style="margin-bottom:20px;padding:20px;background:#FFF7ED;border:1px solid #FED7AA;">
    <div style="display:flex;justify-content:space-between;align-items:center;">
        <div>
            <h2 style="margin:0 0 5px;color:#D97706;">📋 استمارة رقم <?= e($member->form_number ?? '—') ?></h2>
            <p style="margin:0;color:#6B7280;font-size:14px;">
                <strong><?= e($member->full_name_ar) ?></strong>
                — الرقم القومي: <?= e($member->national_id ?? '—') ?>
                — تاريخ الاستمارة: <?= e($member->form_date ?? date('Y-m-d')) ?>
            </p>
        </div>
        <a href="/members/<?= (int) $member->id ?>" class="btn btn-outline">← العودة للعضو</a>
    </div>
</div>

<form method="POST" action="/members/<?= (int) $member->id ?>/fill-form">
    <?= csrf_field() ?>

<?php if (!empty($schemaHtml)): ?>
    <!-- Schema-driven form (from /forms/builder) -->
    <?= $schemaHtml ?>

    <div style="display:flex;gap:10px;margin-top:20px;">
        <button type="submit" class="btn btn-primary" style="padding:12px 30px;font-size:16px;">✓ حفظ الاستمارة</button>
        <a href="/members/<?= (int) $member->id ?>" class="btn btn-outline" style="padding:12px 20px;">إلغاء</a>
    </div>
</form>
<?php $__template->endSection(); ?>
<?php return; ?>
<?php endif; ?>

    <!-- Section 1: Personal Details -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#0D7377;">1. البيانات الشخصية التكميلية</h3>
        </div>
        <div style="padding:20px;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" style="direction:ltr;text-align:left;">
            </div>
            <div class="form-group">
                <label class="form-label">محل الميلاد</label>
                <input type="text" name="place_of_birth" value="<?= e($member->place_of_birth ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">الجنسية</label>
                <select name="nationality" class="form-select">
                    <option value="مصري" <?= ($member->nationality ?? '') === 'مصري' ? 'selected' : '' ?>>مصري</option>
                    <?php foreach ($countries as $c): ?>
                        <?php if ($c['nationality_ar'] !== 'مصري'): ?>
                        <option value="<?= e($c['nationality_ar']) ?>" <?= ($member->nationality ?? '') === $c['nationality_ar'] ? 'selected' : '' ?>><?= e($c['nationality_ar']) ?></option>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">الديانة</label>
                <select name="religion" class="form-select">
                    <option value="">-- اختر --</option>
                    <option value="muslim" <?= ($member->religion ?? '') === 'muslim' ? 'selected' : '' ?>>مسلم</option>
                    <option value="christian" <?= ($member->religion ?? '') === 'christian' ? 'selected' : '' ?>>مسيحي</option>
                    <option value="other" <?= ($member->religion ?? '') === 'other' ? 'selected' : '' ?>>أخرى</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">المؤهل الدراسي <span style="color:#DC2626;">*</span></label>
                <select name="qualification_id" class="form-select" required>
                    <option value="">-- اختر المؤهل --</option>
                    <?php foreach ($qualifications as $q): ?>
                        <option value="<?= (int) $q['id'] ?>" <?= ($member->qualification_id ?? 0) == $q['id'] ? 'selected' : '' ?>><?= e($q['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
                <small style="color:#D97706;">المؤهل يؤثر على قيمة العضوية</small>
            </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">تاريخ إصدار إثبات الشخصية</label>
                <input type="date" name="id_issue_date" value="<?= e($member->id_issue_date ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">تاريخ انتهاء إثبات الشخصية</label>
                <input type="date" name="id_expiry_date" value="<?= e($member->id_expiry_date ?? '') ?>" class="form-input">
            </div>
        </div>
    </div>

    <!-- Section 2: Contact -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#0D7377;">2. بيانات الاتصال</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <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="tel" name="phone_international" value="<?= e($member->phone_international ?? '') ?>" 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"></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>
    </div>

    <!-- Section 3: Residence -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#0D7377;">3. بيانات السكن</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div class="form-group">
                <label class="form-label">نوع السكن</label>
                <select name="residence_type" class="form-select">
                    <option value="">-- اختر --</option>
                    <option value="owned" <?= ($member->residence_type ?? '') === 'owned' ? 'selected' : '' ?>>ملك</option>
                    <option value="rented" <?= ($member->residence_type ?? '') === 'rented' ? 'selected' : '' ?>>إيجار</option>
                    <option value="other" <?= ($member->residence_type ?? '') === 'other' ? 'selected' : '' ?>>أخرى</option>
                </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 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>
                <input type="text" name="landmark" value="<?= e($member->landmark ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">الدور</label>
                <input type="text" name="floor" value="<?= e($member->floor ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">الشقة</label>
                <input type="text" name="apartment" value="<?= e($member->apartment ?? '') ?>" class="form-input">
            </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>
                <select name="correspondence_address" class="form-select">
                    <option value="">-- اختر --</option>
                    <option value="residence" <?= ($member->correspondence_address ?? '') === 'residence' ? 'selected' : '' ?>>السكن</option>
                    <option value="work" <?= ($member->correspondence_address ?? '') === 'work' ? 'selected' : '' ?>>العمل</option>
                    <option value="other" <?= ($member->correspondence_address ?? '') === 'other' ? 'selected' : '' ?>>أخرى</option>
                </select>
            </div>
        </div>
    </div>

    <!-- Section 4: Employment -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#0D7377;">4. بيانات العمل</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div class="form-group">
                <label class="form-label">نوع التوظيف</label>
                <select name="employment_type" class="form-select">
                    <option value="">-- اختر --</option>
                    <option value="employed" <?= ($member->employment_type ?? '') === 'employed' ? 'selected' : '' ?>>موظف</option>
                    <option value="self_employed" <?= ($member->employment_type ?? '') === 'self_employed' ? 'selected' : '' ?>>أعمال حرة</option>
                    <option value="professions" <?= ($member->employment_type ?? '') === 'professions' ? 'selected' : '' ?>>مهن حرة</option>
                    <option value="retired" <?= ($member->employment_type ?? '') === 'retired' ? 'selected' : '' ?>>متقاعد</option>
                    <option value="other" <?= ($member->employment_type ?? '') === 'other' ? 'selected' : '' ?>>أخرى</option>
                </select>
            </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">
                <label class="form-label">تاريخ الالتحاق بالعمل</label>
                <input type="date" name="employment_date" value="<?= e($member->employment_date ?? '') ?>" class="form-input">
            </div>
            <div class="form-group" style="grid-column:1/-1;">
                <label class="form-label">عنوان العمل</label>
                <textarea name="business_address" class="form-textarea" rows="2"><?= e($member->business_address ?? '') ?></textarea>
            </div>
            <div class="form-group">
                <label class="form-label">تليفون العمل</label>
                <input type="tel" name="office_phone" value="<?= e($member->office_phone ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;">
            </div>
            <div class="form-group">
                <label class="form-label">فاكس العمل</label>
                <input type="tel" name="office_fax" value="<?= e($member->office_fax ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;">
            </div>
            <div class="form-group">
                <label class="form-label">نشاط العمل</label>
                <input type="text" name="business_activity" value="<?= e($member->business_activity ?? '') ?>" class="form-input">
            </div>
        </div>
    </div>

    <!-- Section 5: How did you hear -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#0D7377;">5. معلومات إضافية</h3>
        </div>
        <div style="padding:20px;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>
    </div>

    <!-- Special Discount -->
    <?php if (!empty($specialDiscounts)): ?>
    <div class="card" style="margin-bottom:20px;border-right:4px solid #D97706;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#D97706;">خصم خاص (اختياري)</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div class="form-group">
                <label class="form-label">نوع الخصم الخاص</label>
                <select name="special_discount_id" class="form-select">
                    <option value="">— بدون خصم —</option>
                    <?php foreach ($specialDiscounts as $sd): ?>
                    <option value="<?= (int) $sd['id'] ?>"><?= e($sd['name_ar']) ?> (<?= e($sd['discount_percentage']) ?>%)</option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary" style="padding:12px 30px;font-size:16px;">✓ حفظ الاستمارة</button>
        <a href="/members/<?= (int) $member->id ?>" class="btn btn-outline" style="padding:12px 20px;">إلغاء</a>
    </div>
</form>

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