<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-edit me-2"></i><?= lang('members.edit') ?>: <?= e($member['membership_number']) ?></h5>
                <a href="/members/<?= $member['id'] ?>" 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/<?= $member['id'] ?>/update" enctype="multipart/form-data" data-ajax="true">
                    <?= csrf_field() ?>

                    <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">
                        <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>
                                        <?php foreach ($types as $t): ?>
                                        <option value="<?= $t['id'] ?>" <?= selected($t['id'], $member['membership_type_id']) ?>><?= e(current_lang() === 'ar' ? $t['name_ar'] : $t['name_en']) ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label"><?= lang('members.title_field') ?></label>
                                    <select name="title" class="form-select">
                                        <option value="">—</option>
                                        <?php foreach (['mr','mrs','ms','dr','eng','prof'] as $t): ?>
                                        <option value="<?= $t ?>" <?= selected($t, $member['title']) ?>><?= ucfirst($t) ?></option>
                                        <?php endforeach; ?>
                                    </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" <?= checked('male', $member['gender']) ?> required><label class="form-check-label"><?= lang('members.male') ?></label></div>
                                        <div class="form-check"><input class="form-check-input" type="radio" name="gender" value="female" <?= checked('female', $member['gender']) ?>><label class="form-check-label"><?= lang('members.female') ?></label></div>
                                    </div>
                                </div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.first_name_en') ?> *</label><input type="text" name="first_name_en" class="form-control" value="<?= e($member['first_name_en']) ?>" required></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" value="<?= e($member['second_name_en'] ?? '') ?>"></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" value="<?= e($member['third_name_en'] ?? '') ?>"></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.last_name_en') ?> *</label><input type="text" name="last_name_en" class="form-control" value="<?= e($member['last_name_en']) ?>" required></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" value="<?= e($member['first_name_ar'] ?? '') ?>"></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" value="<?= e($member['second_name_ar'] ?? '') ?>"></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" value="<?= e($member['third_name_ar'] ?? '') ?>"></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" value="<?= e($member['last_name_ar'] ?? '') ?>"></div>
                                <div class="col-md-3"><label class="form-label"><?= lang('members.national_id') ?></label><input type="text" name="national_id" class="form-control" value="<?= e($member['national_id'] ?? '') ?>"><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" value="<?= e($member['passport_number'] ?? '') ?>"></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'] ?>" <?= selected($n['id'], $member['nationality_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" value="<?= e($member['date_of_birth'] ?? '') ?>"></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>
                                        <?php foreach (['single','married','divorced','widowed'] as $ms): ?>
                                        <option value="<?= $ms ?>" <?= selected($ms, $member['marital_status']) ?>><?= ucfirst($ms) ?></option>
                                        <?php endforeach; ?>
                                    </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/*">
                                    <?php if ($member['photo_path']): ?><small class="text-muted">Current photo set</small><?php endif; ?>
                                </div>
                            </div>
                        </div>

                        <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" value="<?= e($member['primary_phone'] ?? '') ?>"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.secondary_phone') ?></label><input type="text" name="secondary_phone" class="form-control" value="<?= e($member['secondary_phone'] ?? '') ?>"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.email') ?></label><input type="email" name="primary_email" class="form-control" value="<?= e($member['primary_email'] ?? '') ?>"></div>
                            </div>
                        </div>

                        <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" value="<?= e($member['address_line1'] ?? '') ?>"></div>
                                <div class="col-md-4">
                                    <label class="form-label"><?= lang('members.country') ?></label>
                                    <select name="country_id" class="form-select select2">
                                        <option value="">—</option>
                                        <?php foreach ($countries as $co): ?>
                                        <option value="<?= $co['id'] ?>" <?= selected($co['id'], $member['country_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">
                                        <option value="">—</option>
                                        <?php foreach ($cities as $ci): ?>
                                        <option value="<?= $ci['id'] ?>" <?= selected($ci['id'], $member['city_id']) ?>><?= e(current_lang() === 'ar' ? $ci['name_ar'] : $ci['name_en']) ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <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" value="<?= e($member['occupation'] ?? '') ?>"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.employer') ?></label><input type="text" name="employer" class="form-control" value="<?= e($member['employer'] ?? '') ?>"></div>
                                <div class="col-md-4"><label class="form-label"><?= lang('members.job_title') ?></label><input type="text" name="job_title" class="form-control" value="<?= e($member['job_title'] ?? '') ?>"></div>
                            </div>
                        </div>

                        <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" value="<?= e($member['emergency_contact_name'] ?? '') ?>"></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" value="<?= e($member['emergency_contact_phone'] ?? '') ?>"></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" value="<?= e($member['emergency_contact_relation'] ?? '') ?>"></div>
                                <div class="col-md-3"><div class="form-check mt-4"><input class="form-check-input" type="checkbox" name="vip" value="1" <?= checked(1, $member['vip']) ?>><label class="form-check-label"><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"><?= e($member['internal_notes'] ?? '') ?></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/<?= $member['id'] ?>" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>