<?php $title = 'Membership Profile - Ghana Anaesthesiologists Society'; ?>

<?php if ($completionPct < 100): ?>
<div class="reminder-banner">
    <i class="fas fa-exclamation-triangle"></i>
    <div class="flex-grow-1">
        <strong>Profile is <?= $completionPct ?>% complete.</strong> Please fill in all fields to complete your membership profile.
    </div>
</div>
<?php endif; ?>

<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><i class="fas fa-id-card mr-2"></i>Membership Profile</h1>
    <div class="text-center">
        <div class="profile-complete-ring mb-2" style="--pct: <?= $completionPct ?>%;">
            <span class="pct-text"><?= $completionPct ?>%</span>
        </div>
        <small style="color: rgba(255,255,255,0.5);">Profile Complete</small>
    </div>
</div>

<div class="glass-card mb-4">
    <div class="row align-items-center">
        <div class="col-md-2 text-center">
            <?php if ($user && $user->avatar): ?>
            <img src="<?= $baseUrl . '/' . ltrim($user->avatar, '/') ?>" class="rounded-circle" style="width: 120px; height: 120px; object-fit: cover; border: 3px solid rgba(78,115,223,0.3);">
            <?php else: ?>
            <div style="width: 120px; height: 120px; border-radius: 50%; background: rgba(78,115,223,0.15); display: flex; align-items: center; justify-content: center; margin: 0 auto; border: 3px solid rgba(78,115,223,0.3);">
                <i class="fas fa-user-md" style="font-size: 3rem; color: rgba(78,115,223,0.5);"></i>
            </div>
            <?php endif; ?>
        </div>
        <div class="col-md-10">
            <form method="POST" action="<?= $baseUrl ?>/member/profile/avatar" enctype="multipart/form-data" class="form-inline">
                <div class="custom-file mr-3" style="max-width: 250px;">
                    <input type="file" class="custom-file-input glass-input" name="avatar" id="avatarUpload" accept="image/*" style="padding: 0.4rem;">
                    <label class="custom-file-label" for="avatarUpload" style="background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.1); color: rgba(255,255,255,0.5);">Choose photo...</label>
                </div>
                <button type="submit" class="glass-btn glass-btn-primary btn-sm"><i class="fas fa-upload mr-1"></i>Upload Photo</button>
            </form>
            <small style="color: rgba(255,255,255,0.4);">Upload a professional photo. Allowed: JPG, PNG, GIF</small>
        </div>
    </div>
</div>

<form method="POST" action="<?= $baseUrl ?>/member/profile/save">
    <div class="form-section">
        <h5><i class="fas fa-user mr-2"></i>1. Personal Information</h5>
        <div class="row">
            <div class="col-md-4 form-group">
                <label style="color: rgba(255,255,255,0.7);">Full Name *</label>
                <input type="text" class="form-control glass-input" value="<?= htmlspecialchars($_SESSION['user_name'] ?? '') ?>" disabled>
            </div>
            <div class="col-md-4 form-group">
                <label style="color: rgba(255,255,255,0.7);">Gender *</label>
                <select name="gender" class="form-control glass-input">
                    <option value="">Select Gender</option>
                    <option value="male" <?= ($profile->gender ?? '') === 'male' ? 'selected' : '' ?>>Male</option>
                    <option value="female" <?= ($profile->gender ?? '') === 'female' ? 'selected' : '' ?>>Female</option>
                    <option value="other" <?= ($profile->gender ?? '') === 'other' ? 'selected' : '' ?>>Other</option>
                </select>
            </div>
            <div class="col-md-4 form-group">
                <label style="color: rgba(255,255,255,0.7);">Nationality *</label>
                <input type="text" name="nationality" class="form-control glass-input" value="<?= htmlspecialchars($profile->nationality ?? '') ?>" placeholder="Ghanaian">
            </div>
        </div>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Medical/Dental Council Registration Number *</label>
                <input type="text" name="medical_council_number" class="form-control glass-input" value="<?= htmlspecialchars($user->medical_council_number ?? $profile->medical_council_number ?? '') ?>">
            </div>
        </div>
    </div>

    <div class="form-section">
        <h5><i class="fas fa-address-book mr-2"></i>2. Contact Information</h5>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Postal Address</label>
                <textarea name="postal_address" class="form-control glass-input" rows="2"><?= htmlspecialchars($profile->postal_address ?? '') ?></textarea>
            </div>
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Residential Address</label>
                <textarea name="residential_address" class="form-control glass-input" rows="2"><?= htmlspecialchars($profile->residential_address ?? '') ?></textarea>
            </div>
        </div>
        <div class="row">
            <div class="col-md-4 form-group">
                <label style="color: rgba(255,255,255,0.7);">Mobile Phone *</label>
                <input type="text" name="mobile_phone" class="form-control glass-input" value="<?= htmlspecialchars($profile->mobile_phone ?? '') ?>" placeholder="+233 XX XXX XXXX">
            </div>
            <div class="col-md-4 form-group">
                <label style="color: rgba(255,255,255,0.7);">Alternative Phone</label>
                <input type="text" name="alternative_phone" class="form-control glass-input" value="<?= htmlspecialchars($profile->alternative_phone ?? '') ?>">
            </div>
            <div class="col-md-4 form-group">
                <label style="color: rgba(255,255,255,0.7);">Email Address</label>
                <input type="email" class="form-control glass-input" value="<?= htmlspecialchars($_SESSION['user_email'] ?? '') ?>" disabled>
            </div>
        </div>
    </div>

    <div class="form-section">
        <h5><i class="fas fa-briefcase mr-2"></i>3. Professional Information</h5>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Institution/Hospital *</label>
                <input type="text" name="institution_hospital" class="form-control glass-input" value="<?= htmlspecialchars($profile->institution_hospital ?? '') ?>">
            </div>
            <div class="col-md-3 form-group">
                <label style="color: rgba(255,255,255,0.7);">City/Town</label>
                <input type="text" name="city_town" class="form-control glass-input" value="<?= htmlspecialchars($profile->city_town ?? '') ?>">
            </div>
            <div class="col-md-3 form-group">
                <label style="color: rgba(255,255,255,0.7);">Country</label>
                <input type="text" name="country" class="form-control glass-input" value="<?= htmlspecialchars($profile->country ?? '') ?>" placeholder="Ghana">
            </div>
        </div>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Years in Anaesthesia Practice</label>
                <input type="number" name="years_practice" class="form-control glass-input" value="<?= htmlspecialchars($profile->years_practice ?? '') ?>">
            </div>
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Highest Qualification *</label>
                <input type="text" name="highest_qualification" class="form-control glass-input" value="<?= htmlspecialchars($profile->highest_qualification ?? '') ?>" placeholder="e.g. FWACS, MD, PhD">
            </div>
        </div>
    </div>

    <div class="form-section">
        <h5><i class="fas fa-tag mr-2"></i>4. Membership Category</h5>
        <div class="form-group">
            <select name="membership_category" class="form-control glass-input">
                <option value="">Select Membership Category</option>
                <option value="Student Member" <?= ($profile->membership_category ?? '') === 'Student Member' ? 'selected' : '' ?>>Student Member</option>
                <option value="Resident/Trainee Member" <?= ($profile->membership_category ?? '') === 'Resident/Trainee Member' ? 'selected' : '' ?>>Resident/Trainee Member</option>
                <option value="Associate Member" <?= ($profile->membership_category ?? '') === 'Associate Member' ? 'selected' : '' ?>>Associate Member</option>
                <option value="Member" <?= ($profile->membership_category ?? '') === 'Member' ? 'selected' : '' ?>>Member</option>
                <option value="Fellow" <?= ($profile->membership_category ?? '') === 'Fellow' ? 'selected' : '' ?>>Fellow</option>
                <option value="Honorary Member" <?= ($profile->membership_category ?? '') === 'Honorary Member' ? 'selected' : '' ?>>Honorary Member</option>
            </select>
        </div>
    </div>

    <div class="form-section">
        <h5><i class="fas fa-history mr-2"></i>5. Previous Society Membership</h5>
        <div class="form-group">
            <label style="color: rgba(255,255,255,0.7);">Have you previously been a member of the Society?</label>
            <div class="mt-2">
                <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" class="custom-control-input" id="previous_member_yes" name="previous_member" value="yes" <?= (($profile->previous_member ?? 0) == 1) ? 'checked' : '' ?>>
                    <label class="custom-control-label" for="previous_member_yes" style="color: rgba(255,255,255,0.7);">Yes</label>
                </div>
                <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" class="custom-control-input" id="previous_member_no" name="previous_member" value="no" <?= (($profile->previous_member ?? 0) == 1) ? '' : 'checked' ?>>
                    <label class="custom-control-label" for="previous_member_no" style="color: rgba(255,255,255,0.7);">No</label>
                </div>
            </div>
        </div>
    </div>

    <div class="form-section">
        <h5><i class="fas fa-bell mr-2"></i>6. Notification Preferences</h5>
        <div class="row">
            <div class="col-md-4">
                <div class="custom-control custom-checkbox mb-2">
                    <input type="checkbox" class="custom-control-input" id="notify_conferences" name="notify_conferences" value="1" <?= ($profile->notify_conferences ?? 1) ? 'checked' : '' ?>>
                    <label class="custom-control-label" for="notify_conferences" style="color: rgba(255,255,255,0.7);">Conferences</label>
                </div>
            </div>
            <div class="col-md-4">
                <div class="custom-control custom-checkbox mb-2">
                    <input type="checkbox" class="custom-control-input" id="notify_workshops" name="notify_workshops" value="1" <?= ($profile->notify_workshops ?? 1) ? 'checked' : '' ?>>
                    <label class="custom-control-label" for="notify_workshops" style="color: rgba(255,255,255,0.7);">Workshops</label>
                </div>
            </div>
            <div class="col-md-4">
                <div class="custom-control custom-checkbox mb-2">
                    <input type="checkbox" class="custom-control-input" id="notify_webinars" name="notify_webinars" value="1" <?= ($profile->notify_webinars ?? 1) ? 'checked' : '' ?>>
                    <label class="custom-control-label" for="notify_webinars" style="color: rgba(255,255,255,0.7);">Webinars</label>
                </div>
            </div>
            <div class="col-md-4">
                <div class="custom-control custom-checkbox mb-2">
                    <input type="checkbox" class="custom-control-input" id="notify_research" name="notify_research" value="1" <?= ($profile->notify_research ?? 0) ? 'checked' : '' ?>>
                    <label class="custom-control-label" for="notify_research" style="color: rgba(255,255,255,0.7);">Research Opportunities</label>
                </div>
            </div>
            <div class="col-md-4">
                <div class="custom-control custom-checkbox mb-2">
                    <input type="checkbox" class="custom-control-input" id="notify_scholarships" name="notify_scholarships" value="1" <?= ($profile->notify_scholarships ?? 0) ? 'checked' : '' ?>>
                    <label class="custom-control-label" for="notify_scholarships" style="color: rgba(255,255,255,0.7);">Scholarships</label>
                </div>
            </div>
            <div class="col-md-4">
                <div class="custom-control custom-checkbox mb-2">
                    <input type="checkbox" class="custom-control-input" id="notify_news" name="notify_news" value="1" <?= ($profile->notify_news ?? 1) ? 'checked' : '' ?>>
                    <label class="custom-control-label" for="notify_news" style="color: rgba(255,255,255,0.7);">Society News</label>
                </div>
            </div>
        </div>
    </div>

    <?php if (!empty($profileFields)): ?>
    <div class="form-section">
        <h5><i class="fas fa-plus-circle mr-2"></i>7. Additional Information</h5>
        <div class="row">
            <?php foreach ($profileFields as $pf): ?>
            <div class="col-md-<?= $pf->field_type === 'textarea' ? '12' : '6' ?> form-group">
                <label style="color: rgba(255,255,255,0.7);">
                    <?= htmlspecialchars($pf->label) ?>
                    <?= $pf->is_required ? '<span class="text-danger">*</span>' : '' ?>
                </label>
                <?php if ($pf->field_type === 'textarea'): ?>
                <textarea name="pf_<?= htmlspecialchars($pf->field_name) ?>" class="form-control glass-input" rows="3" <?= $pf->is_required ? 'required' : '' ?> placeholder="<?= htmlspecialchars($pf->placeholder ?? '') ?>"><?= htmlspecialchars($profileFieldValues[$pf->field_name] ?? '') ?></textarea>
                <?php elseif ($pf->field_type === 'select'): ?>
                <select name="pf_<?= htmlspecialchars($pf->field_name) ?>" class="form-control glass-input" <?= $pf->is_required ? 'required' : '' ?>>
                    <option value=""><?= htmlspecialchars($pf->placeholder ?: 'Select...') ?></option>
                    <?php foreach (explode("\n", $pf->options ?? '') as $opt): $opt = trim($opt); if (empty($opt)) continue; ?>
                    <option value="<?= htmlspecialchars($opt) ?>" <?= ($profileFieldValues[$pf->field_name] ?? '') === $opt ? 'selected' : '' ?>><?= htmlspecialchars($opt) ?></option>
                    <?php endforeach; ?>
                </select>
                <?php elseif ($pf->field_type === 'radio'): ?>
                <div class="mt-2">
                    <?php foreach (explode("\n", $pf->options ?? '') as $opt): $opt = trim($opt); if (empty($opt)) continue; ?>
                    <div class="custom-control custom-radio custom-control-inline mb-2">
                        <input type="radio" class="custom-control-input" name="pf_<?= htmlspecialchars($pf->field_name) ?>" id="pf_<?= htmlspecialchars($pf->field_name) . '_' . preg_replace('/[^a-z0-9]/i', '', $opt) ?>" value="<?= htmlspecialchars($opt) ?>" <?= ($profileFieldValues[$pf->field_name] ?? '') === $opt ? 'checked' : '' ?> <?= $pf->is_required ? 'required' : '' ?>>
                        <label class="custom-control-label" for="pf_<?= htmlspecialchars($pf->field_name) . '_' . preg_replace('/[^a-z0-9]/i', '', $opt) ?>" style="color: rgba(255,255,255,0.7);"><?= htmlspecialchars($opt) ?></label>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php elseif ($pf->field_type === 'checkbox'): ?>
                <div class="custom-control custom-checkbox mt-2">
                    <input type="checkbox" class="custom-control-input" name="pf_<?= htmlspecialchars($pf->field_name) ?>" id="pf_<?= htmlspecialchars($pf->field_name) ?>" value="1" <?= ($profileFieldValues[$pf->field_name] ?? '') === '1' ? 'checked' : '' ?>>
                    <label class="custom-control-label" for="pf_<?= htmlspecialchars($pf->field_name) ?>" style="color: rgba(255,255,255,0.7);"><?= htmlspecialchars($pf->label) ?></label>
                </div>
                <?php else: ?>
                <input type="<?= htmlspecialchars($pf->field_type) ?>" name="pf_<?= htmlspecialchars($pf->field_name) ?>" class="form-control glass-input" value="<?= htmlspecialchars($profileFieldValues[$pf->field_name] ?? '') ?>" <?= $pf->is_required ? 'required' : '' ?> placeholder="<?= htmlspecialchars($pf->placeholder ?? '') ?>">
                <?php endif; ?>
                <?php if ($pf->help_text): ?>
                <small style="color: rgba(255,255,255,0.4);"><?= htmlspecialchars($pf->help_text) ?></small>
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <div class="form-section">
        <h5><i class="fas fa-check-circle mr-2"></i>8. Declaration</h5>
        <p style="color: rgba(255,255,255,0.7); font-size: 0.9rem;">I certify that the information provided is true and complete. I agree to abide by the Constitution, By-laws, and Code of Ethics of the Ghana Anaesthesiologists Society and consent to the Society maintaining my information for membership administration and official communications.</p>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Applicant Initials *</label>
                <input type="text" name="declaration_initials" class="form-control glass-input" value="<?= htmlspecialchars($profile->declaration_initials ?? '') ?>" required>
            </div>
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Date *</label>
                <input type="date" name="declaration_date" class="form-control glass-input" value="<?= htmlspecialchars($profile->declaration_date ?? date('Y-m-d')) ?>" required>
            </div>
        </div>
    </div>

    <div class="text-center mt-4">
        <button type="submit" class="glass-btn glass-btn-primary btn-lg px-5"><i class="fas fa-save mr-2"></i>Save Profile</button>
    </div>
</form>
