<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white"><i class="fas fa-user mr-2"></i><?= htmlspecialchars($user->first_name ? trim($user->first_name . ' ' . $user->last_name) : $user->name) ?></h1>
    <div>
        <a href="<?= $baseUrl ?>/members" class="btn btn-sm btn-secondary mr-1"><i class="fas fa-arrow-left mr-1"></i>Back to Directory</a>
        <a href="<?= $baseUrl ?>/users/<?= $user->id ?>/edit" class="btn btn-sm btn-primary"><i class="fas fa-edit mr-1"></i>Edit Member</a>
    </div>
</div>

<?php $isAdmin = isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'; ?>

<div class="row">
    <div class="col-lg-4 mb-4">
        <div class="card glass-card">
            <div class="card-body text-center">
                <?php
                $avatar = $user->avatar
                    ? $baseUrl . '/' . ltrim($user->avatar, '/')
                    : $baseUrl . '/img/undraw_profile.svg';
                ?>
                <img src="<?= $avatar ?>" class="rounded-circle mb-3" style="width: 100px; height: 100px; object-fit: cover; border: 3px solid rgba(78, 115, 223, 0.3);">
                <h5 class="text-white"><?= htmlspecialchars($user->first_name ? trim($user->first_name . ' ' . $user->last_name) : $user->name) ?></h5>
                <p class="text-muted mb-1"><?= htmlspecialchars($user->email) ?></p>
                <p class="mb-2">
                    <span class="badge badge-<?= $user->status === 'active' ? 'success' : 'secondary' ?>"><?= ucfirst($user->status) ?></span>
                    <span class="badge badge-<?= $user->approval_status === 'approved' ? 'success' : ($user->approval_status === 'pending' ? 'warning' : 'danger') ?>"><?= ucfirst($user->approval_status) ?></span>
                    <span class="badge badge-info"><?= ucfirst($user->role ?? 'viewer') ?></span>
                </p>
                <?php if ($profile): ?>
                <div class="mb-2">
                    <small class="text-muted">Profile Completion</small>
                    <div class="progress" style="height: 6px; background: rgba(255,255,255,0.1);">
                        <div class="progress-bar bg-success" style="width: <?= $completionPct ?>%"></div>
                    </div>
                    <small class="text-muted"><?= $completionPct ?>%</small>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <?php if ($isAdmin): ?>
        <div class="card glass-card mt-3">
            <div class="card-header"><i class="fas fa-bolt mr-2"></i>Quick Actions</div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="<?= $baseUrl ?>/users/<?= $user->id ?>/edit" class="btn btn-primary btn-block btn-sm mb-2"><i class="fas fa-edit mr-1"></i>Edit Profile</a>
                    <button type="button" class="btn btn-info btn-block btn-sm mb-2" data-toggle="modal" data-target="#messageModal"><i class="fas fa-envelope mr-1"></i>Send Message</button>
                    <a href="<?= $baseUrl ?>/contributions/create?user_id=<?= $user->id ?>" class="btn btn-success btn-block btn-sm mb-2"><i class="fas fa-hand-holding-usd mr-1"></i>Pay Contribution</a>
                    <a href="<?= $baseUrl ?>/contributions?user_id=<?= $user->id ?>" class="btn btn-secondary btn-block btn-sm mb-2"><i class="fas fa-list mr-1"></i>View Contributions</a>
                    <form method="POST" action="<?= $baseUrl ?>/members/<?= $user->id ?>/toggle-status" onsubmit="return confirm('Are you sure you want to <?= $user->status === 'active' ? 'suspend' : 'activate' ?> this member?')">
                        <button type="submit" class="btn btn-<?= $user->status === 'active' ? 'warning' : 'success' ?> btn-block btn-sm">
                            <i class="fas fa-<?= $user->status === 'active' ? 'ban' : 'check-circle' ?> mr-1"></i><?= $user->status === 'active' ? 'Suspend' : 'Activate' ?> Member
                        </button>
                    </form>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-lg-8 mb-4">
        <div class="card glass-card mb-3">
            <div class="card-header"><i class="fas fa-address-card mr-2"></i>Personal Information</div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Name:</strong><br><span class="text-white"><?= htmlspecialchars($user->first_name ? trim($user->first_name . ' ' . $user->other_names . ' ' . $user->last_name) : $user->name) ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Email:</strong><br><span class="text-white"><?= htmlspecialchars($user->email) ?></span></div>
                    <?php if ($profile): ?>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Gender:</strong><br><span class="text-white"><?= htmlspecialchars(ucfirst($profile->gender ?? '-')) ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Nationality:</strong><br><span class="text-white"><?= htmlspecialchars($profile->nationality ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Mobile Phone:</strong><br><span class="text-white"><?= htmlspecialchars($profile->mobile_phone ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Alternative Phone:</strong><br><span class="text-white"><?= htmlspecialchars($profile->alternative_phone ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Professional PIN:</strong><br><span class="text-white"><?= htmlspecialchars($user->professional_pin ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Medical Council #:</strong><br><span class="text-white"><?= htmlspecialchars($user->medical_council_number ?? '-') ?></span></div>
                    <div class="col-md-12 mb-2"><strong class="text-white-50">Postal Address:</strong><br><span class="text-white"><?= nl2br(htmlspecialchars($profile->postal_address ?? '-')) ?></span></div>
                    <div class="col-md-12 mb-2"><strong class="text-white-50">Residential Address:</strong><br><span class="text-white"><?= nl2br(htmlspecialchars($profile->residential_address ?? '-')) ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Institution/Hospital:</strong><br><span class="text-white"><?= htmlspecialchars($profile->institution_hospital ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">City/Town:</strong><br><span class="text-white"><?= htmlspecialchars($profile->city_town ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Country:</strong><br><span class="text-white"><?= htmlspecialchars($profile->country ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Years in Practice:</strong><br><span class="text-white"><?= htmlspecialchars($profile->years_practice ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Highest Qualification:</strong><br><span class="text-white"><?= htmlspecialchars($profile->highest_qualification ?? '-') ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Membership Category:</strong><br><span class="text-white"><?= htmlspecialchars($profile->membership_category ?? '-') ?></span></div>
                    <?php endif; ?>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Member Since:</strong><br><span class="text-white"><?= $user->created_at ? date('M j, Y', strtotime($user->created_at)) : '-' ?></span></div>
                    <div class="col-md-6 mb-2"><strong class="text-white-50">Last Login:</strong><br><span class="text-white"><?= $user->last_login ? date('M j, Y g:i A', strtotime($user->last_login)) : 'Never' ?></span></div>
                </div>
            </div>
        </div>

        <?php if (!empty($profileFields) && !empty($profileFieldValues)): ?>
        <div class="card glass-card mb-3">
            <div class="card-header"><i class="fas fa-layer-group mr-2"></i>Custom Profile Fields</div>
            <div class="card-body">
                <div class="row">
                    <?php foreach ($profileFields as $pf):
                        $val = $profileFieldValues[$pf->field_name] ?? '';
                        if ($val === '') continue;
                    ?>
                    <div class="col-md-6 mb-2">
                        <strong class="text-white-50"><?= htmlspecialchars($pf->label) ?>:</strong><br>
                        <span class="text-white"><?= htmlspecialchars($val) ?></span>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <div class="card glass-card mb-3">
            <div class="card-header">
                <div class="d-flex justify-content-between align-items-center">
                    <span><i class="fas fa-tags mr-2"></i>Contribution Types</span>
                    <?php if ($isAdmin): ?>
                    <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#assignTypeModal"><i class="fas fa-plus mr-1"></i>Assign Type</button>
                    <?php endif; ?>
                </div>
            </div>
            <div class="card-body">
                <?php if (!empty($assignedTypes)): ?>
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead><tr><th>Type</th><th>Amount</th><th>Frequency</th><th>Assigned</th><?php if ($isAdmin): ?><th>Action</th><?php endif; ?></tr></thead>
                        <tbody>
                            <?php foreach ($assignedTypes as $at): ?>
                            <tr>
                                <td class="text-white"><?= htmlspecialchars($at->name) ?></td>
                                <td class="text-white">GH₵ <?= number_format($at->amount, 2) ?></td>
                                <td class="text-white"><?= ucfirst($at->frequency ?? 'monthly') ?></td>
                                <td class="text-white"><?= date('M j, Y', strtotime($at->assigned_at)) ?></td>
                                <?php if ($isAdmin): ?>
                                <td>
                                    <form method="POST" action="<?= $baseUrl ?>/members/<?= $user->id ?>/unassign-type/<?= $at->id ?>" onsubmit="return confirm('Remove this contribution type?')">
                                        <button type="submit" class="btn btn-sm btn-danger"><i class="fas fa-times"></i></button>
                                    </form>
                                </td>
                                <?php endif; ?>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <p class="text-muted mb-0">No contribution types assigned to this member.</p>
                <?php endif; ?>
            </div>
        </div>

        <div class="card glass-card mb-3">
            <div class="card-header"><i class="fas fa-hand-holding-usd mr-2"></i>Contribution History (Last 10)</div>
            <div class="card-body">
                <?php if (!empty($contributions)): ?>
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead><tr><th>Date</th><th>Type</th><th>Month</th><th>Amount</th><th>Status</th></tr></thead>
                        <tbody>
                            <?php foreach (array_slice($contributions, 0, 10) as $c): ?>
                            <tr>
                                <td class="text-white"><?= date('M j, Y', strtotime($c->created_at)) ?></td>
                                <td class="text-white"><?= htmlspecialchars($c->type_name ?? '-') ?></td>
                                <td class="text-white"><?= date('M Y', strtotime($c->paid_for_year . '-' . $c->paid_for_month . '-01')) ?></td>
                                <td class="text-white">GH₵ <?= number_format($c->amount, 2) ?></td>
                                <td><span class="badge badge-<?= $c->status === 'completed' ? 'success' : ($c->status === 'partial' ? 'warning' : 'secondary') ?>"><?= ucfirst($c->status) ?></span></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <a href="<?= $baseUrl ?>/contributions?user_id=<?= $user->id ?>" class="btn btn-sm btn-outline-light mt-2">View All Contributions</a>
                <?php else: ?>
                <p class="text-muted mb-0">No contributions recorded yet.</p>
                <?php endif; ?>
            </div>
        </div>

        <?php if (!empty($obligations)): ?>
        <div class="card glass-card mb-3">
            <div class="card-header"><i class="fas fa-exclamation-triangle text-warning mr-2"></i>Outstanding Obligations</div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead><tr><th>Type</th><th>Period</th><th>Amount</th><th>Paid</th><th>Balance</th></tr></thead>
                        <tbody>
                            <?php foreach ($obligations as $o): ?>
                            <tr>
                                <td class="text-white"><?= htmlspecialchars($o->type_name ?? '-') ?></td>
                                <td class="text-white"><?= date('M Y', strtotime($o->year . '-' . $o->month . '-01')) ?></td>
                                <td class="text-white">GH₵ <?= number_format($o->amount, 2) ?></td>
                                <td class="text-white">GH₵ <?= number_format(($o->amount - $o->balance), 2) ?></td>
                                <td class="text-<?= $o->balance > 0 ? 'warning' : 'success' ?>">GH₵ <?= number_format($o->balance, 2) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php if ($isAdmin): ?>
<div class="modal fade" id="messageModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content glass-modal-content">
            <form method="POST" action="<?= $baseUrl ?>/members/<?= $user->id ?>/message">
                <div class="modal-header"><h5 class="modal-title"><i class="fas fa-envelope mr-2"></i>Send Message to <?= htmlspecialchars($user->first_name ? trim($user->first_name . ' ' . $user->last_name) : $user->name) ?></h5><button type="button" class="close" data-dismiss="modal"><span>&times;</span></button></div>
                <div class="modal-body">
                    <div class="form-group">
                        <label class="text-white-50">Subject</label>
                        <input type="text" name="subject" class="form-control glass-input" required>
                    </div>
                    <div class="form-group">
                        <label class="text-white-50">Message</label>
                        <textarea name="body" class="form-control glass-input" rows="5" required></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary"><i class="fas fa-paper-plane mr-1"></i>Send</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="assignTypeModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content glass-modal-content">
            <form method="POST" action="<?= $baseUrl ?>/members/<?= $user->id ?>/assign-type">
                <div class="modal-header"><h5 class="modal-title"><i class="fas fa-tag mr-2"></i>Assign Contribution Type</h5><button type="button" class="close" data-dismiss="modal"><span>&times;</span></button></div>
                <div class="modal-body">
                    <div class="form-group">
                        <label class="text-white-50">Contribution Type</label>
                        <select name="contribution_type_id" class="form-control glass-input" required>
                            <option value="">Select type...</option>
                            <?php foreach ($allTypes as $t): ?>
                            <option value="<?= $t->id ?>"><?= htmlspecialchars($t->name) ?> (GH₵ <?= number_format($t->amount, 2) ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary"><i class="fas fa-check mr-1"></i>Assign</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php endif; ?>
