<div class="row justify-content-center">
    <div class="col-xl-10">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="fas fa-user-plus me-2"></i><?= lang('members.create') ?></h5>
                <a href="/members" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i><?= lang('common.back') ?></a>
            </div>
            <div class="card-body">
                <form id="member-form" method="POST" action="/api/members/store" enctype="multipart/form-data" data-ajax="true">
                    <?= csrf_field() ?>

                    <!-- Tabs -->
                    <ul class="nav nav-tabs mb-4" role="tablist">
                        <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#tab-personal"><i class="fas fa-user me-1"></i> <?= lang('members.personal_info') ?></a></li>
                        <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-contact"><i class="fas fa-phone me-1"></i> <?= lang('members.contact') ?></a></li>
                        <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-address"><i class="fas fa-map-marker-alt me-1"></i> <?= lang('members.address') ?></a></li>
                        <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-work"><i class="fas fa-briefcase me-1"></i> <?= lang('members.employment') ?></a></li>
                        <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-extra"><i class="fas fa-cog me-1"></i> <?= lang('members.additional') ?></a></li>
                    </ul>

                    <div class="tab-content">
                        <!-- Personal Info Tab -->
                        <div class="tab-pane fade show active" id="tab-personal">
                            <div class="row g-3">
                                <div class="col-md-3">
                                    <label class="form-label"><?= lang('members.membership_type') ?> <span class="text-danger">*</span></label>
                                    <select name="membership_type_id" class="form-select" required>
                                        <option value="">— <?= lang('common.select') ?> —</option>
                                        <?php foreach ($types as $t): ?>
                                        <option value="<?= $t['id'] ?>" data-family="<?= $t['is_family_membership'] ?>"><?= e(current_lang() === 'ar' ? $t['name_ar'] : $t['name_en']) ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                    <div class="invalid-feedback" data-field="membership_type_id"></div>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label"><?= lang('members.title_field') ?></label>
                                    <select name="title" class="form-select">
                                        <option value="">—</option>
                                        <option value="mr">Mr</option><option value="mrs">Mrs</option><option value="ms">Ms</option>
                                        <option value="dr">Dr</option><option value="eng">Eng</option><option value="prof">Prof</option>
                                    </select>
                                </div>
                                <div class="col-md-7">
                                    <label class="form-label"><?= lang('members.gender') ?> <span class="text-danger">*</span></label>
                                    <div class="d-flex gap-3 mt-1">
                                        <div class="form-check"><input class="form-check-input" type="radio" name="gender" value="male" id="g-m" required><label class="form-check-label" for="g-m"><?= lang('members.male') ?></label></div>
                                        <div class="form-check"><input class="form-check-input" type="radio" name="gender" value="female" id="g-f"><label class="form-check-label" for="g-f"><?= lang('members.female') ?></label></div>
                                    </div>
                                    <div class="invalid-feedback" data-field="gender"></div>
                                </div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.first_name_en') ?> <span class="text-danger">*</span></label><input type="text" name="first_name_en" class="form-control" required><div class="invalid-feedback" data-field="first_name_en"></div></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.second_name_en') ?></label><input type="text" name="second_name_en" class="form-control"><div class="invalid-feedback" data-field="second_name_en"></div></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.third_name_en') ?></label><input type="text" name="third_name_en" class="form-control"></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.last_name_en') ?> <span class="text-danger">*</span></label><input type="text" name="last_name_en" class="form-control" required><div class="invalid-feedback" data-field="last_name_en"></div></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.first_name_ar') ?></label><input type="text" name="first_name_ar" class="form-control" dir="rtl"></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.second_name_ar') ?></label><input type="text" name="second_name_ar" class="form-control" dir="rtl"></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.third_name_ar') ?></label><input type="text" name="third_name_ar" class="form-control" dir="rtl"></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.last_name_ar') ?></label><input type="text" name="last_name_ar" class="form-control" dir="rtl"></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.national_id') ?></label><input type="text" name="national_id" class="form-control" maxlength="20"><div class="invalid-feedback" data-field="national_id"></div></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.passport') ?></label><input type="text" name="passport_number" class="form-control" maxlength="30"></div>
                                <div class="col-md-3">
                                    <label class="form-label"><?= lang('members.nationality') ?></label>
                                    <select name="nationality_id" class="form-select select2">
                                        <option value="">—</option>
                                        <?php foreach ($nationalities as $n): ?>
                                        <option value="<?= $n['id'] ?>"><?= e(current_lang() === 'ar' ? $n['name_ar'] : $n['name_en']) ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.dob') ?></label><input type="date" name="date_of_birth" class="form-control"></div>
                                <div class="col-md-3">
                                    <label class="form-label"><?= lang('members.marital_status') ?></label>
                                    <select name="marital_status" class="form-select">
                                        <option value="">—</option>
                                        <option value="single"><?= lang('members.single') ?></option>
                                        <option value="married"><?= lang('members.married') ?></option>
                                        <option value="divorced"><?= lang('members.divorced') ?></option>
                                        <option value="widowed"><?= lang('members.widowed') ?></option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label"><?= lang('members.photo') ?></label>
                                    <input type="file" name="photo" class="form-control" accept="image/*">
                                </div>
                            </div>
                        </div>

                        <!-- Contact Tab -->
                        <div class="tab-pane fade" id="tab-contact">
                            <div class="row g-3">
                                <div class="col-md-4"><label class="form-label"><?= lang('members.primary_phone') ?></label><input type="text" name="primary_phone" class="form-control"><div class="invalid-feedback" data-field="primary_phone"></div></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.secondary_phone') ?></label><input type="text" name="secondary_phone" class="form-control"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.email') ?></label><input type="email" name="primary_email" class="form-control"><div class="invalid-feedback" data-field="primary_email"></div></div>
                            </div>
                        </div>

                        <!-- Address Tab -->
                        <div class="tab-pane fade" id="tab-address">
                            <div class="row g-3">
                                <div class="col-md-8"><label class="form-label"><?= lang('members.address_line1') ?></label><input type="text" name="address_line1" class="form-control"></div>
                                <div class="col-md-4">
                                    <label class="form-label"><?= lang('members.country') ?></label>
                                    <select name="country_id" class="form-select select2" id="country-select">
                                        <option value="">—</option>
                                        <?php foreach ($countries as $co): ?>
                                        <option value="<?= $co['id'] ?>"><?= e(current_lang() === 'ar' ? $co['name_ar'] : $co['name_en']) ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label"><?= lang('members.city') ?></label>
                                    <select name="city_id" class="form-select" id="city-select"><option value="">—</option></select>
                                </div>
                            </div>
                        </div>

                        <!-- Employment Tab -->
                        <div class="tab-pane fade" id="tab-work">
                            <div class="row g-3">
                                <div class="col-md-4"><label class="form-label"><?= lang('members.occupation') ?></label><input type="text" name="occupation" class="form-control"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.employer') ?></label><input type="text" name="employer" class="form-control"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.job_title') ?></label><input type="text" name="job_title" class="form-control"></div>
                            </div>
                        </div>

                        <!-- Additional Tab -->
                        <div class="tab-pane fade" id="tab-extra">
                            <div class="row g-3">
                                <div class="col-md-4"><label class="form-label"><?= lang('members.emergency_name') ?></label><input type="text" name="emergency_contact_name" class="form-control"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.emergency_phone') ?></label><input type="text" name="emergency_contact_phone" class="form-control"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.emergency_relation') ?></label><input type="text" name="emergency_contact_relation" class="form-control"></div>
                                <div class="col-md-3">
                                    <div class="form-check mt-4"><input class="form-check-input" type="checkbox" name="vip" value="1" id="vip-check"><label class="form-check-label" for="vip-check"><i class="fas fa-star text-warning me-1"></i> VIP</label></div>
                                </div>
                                <div class="col-md-12"><label class="form-label"><?= lang('members.internal_notes') ?></label><textarea name="internal_notes" class="form-control" rows="3"></textarea></div>
                            </div>
                        </div>
                    </div>

                    <div class="mt-4 d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i><?= lang('common.save') ?></button>
                        <a href="/members" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php $__view->push('scripts') ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Country → City cascade
    document.getElementById('country-select')?.addEventListener('change', function() {
        const countryId = this.value;
        const citySelect = document.getElementById('city-select');
        citySelect.innerHTML = '<option value="">—</option>';
        if (!countryId) return;
        $.get('/api/membership-categories/list?country_id=' + countryId, function(r) {
            // The cities would need a separate API endpoint in a full implementation
            // For now, we handle this via a generic lookup
        });
    });
});
</script>
<?php $__view->endPush() ?>