<?php
/**
 * Member Profile
 * Member self-service profile management
 * Kenyan Pension & Life Insurance Management System
 */

require_once 'layouts/main.php';
require_once 'includes/auth.php';

requireLogin();
if (!isMember()) {
    header('Location: access_denied.php');
    exit();
}

$member_id = $_SESSION['member_id'] ?? $_SESSION['user_id'];
$message = '';
$messageType = '';

// Get member data
$member = $conn->query("SELECT m.*, ps.scheme_name, ps.scheme_code, e.employer_name
                       FROM members m
                       LEFT JOIN pension_schemes ps ON m.scheme_id = ps.scheme_id
                       LEFT JOIN employers e ON m.employer_id = e.employer_id
                       WHERE m.member_id = $member_id")->fetch_assoc();

if (!$member) {
    header('Location: member_portal.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Update contact information only (for security, other fields require admin approval)
    $sql = "UPDATE members SET 
           email = ?,
           phone_number = ?,
           updated_at = CURRENT_TIMESTAMP
           WHERE member_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssi",
        $_POST['email'],
        $_POST['phone_number'],
        $member_id
    );
    
    if ($stmt->execute()) {
        $message = 'Profile updated successfully';
        $messageType = 'success';
        // Refresh member data
        $member = $conn->query("SELECT m.*, ps.scheme_name, ps.scheme_code, e.employer_name
                               FROM members m
                               LEFT JOIN pension_schemes ps ON m.scheme_id = ps.scheme_id
                               LEFT JOIN employers e ON m.employer_id = e.employer_id
                               WHERE m.member_id = $member_id")->fetch_assoc();
    } else {
        $message = 'Failed to update profile';
        $messageType = 'danger';
    }
}

$pageTitle = 'My Profile';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Profile')); ?>
    <link rel="stylesheet" href="assets/css/pension-dashboard.css">
    <?php include 'layouts/head-css.php'; ?>
</head>

<body data-sidebar="dark">
    <div id="layout-wrapper">
        <?php include 'layouts/sidebar.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="welcome-header" style="background: linear-gradient(135deg, #10b981 0%, #059669 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>My Profile</h2>
                                <p>Manage your personal information</p>
                            </div>
                        </div>
                    </div>

                    <?php if ($message): ?>
                    <div class="alert alert-<?php echo $messageType; ?> alert-dismissible fade show">
                        <?php echo htmlspecialchars($message); ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                    <?php endif; ?>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Personal Information</h4>
                                </div>
                                <div class="page-card-body">
                                    <table class="table table-borderless">
                                        <tr>
                                            <td><strong>Member Number:</strong></td>
                                            <td><?php echo htmlspecialchars($member['member_number']); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Name:</strong></td>
                                            <td><?php echo htmlspecialchars($member['first_name'] . ' ' . ($member['middle_name'] ?? '') . ' ' . $member['last_name']); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Date of Birth:</strong></td>
                                            <td><?php echo date('d M Y', strtotime($member['date_of_birth'])); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Gender:</strong></td>
                                            <td><?php echo htmlspecialchars($member['gender'] ?? 'N/A'); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>National ID:</strong></td>
                                            <td><?php echo htmlspecialchars($member['national_id'] ?? 'N/A'); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Scheme:</strong></td>
                                            <td><?php echo htmlspecialchars($member['scheme_name'] ?? 'N/A'); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Employer:</strong></td>
                                            <td><?php echo htmlspecialchars($member['employer_name'] ?? 'N/A'); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Employment Status:</strong></td>
                                            <td><span class="status-badge status-<?php echo strtolower($member['employment_status']); ?>"><?php echo htmlspecialchars($member['employment_status']); ?></span></td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Contact Information</h4>
                                </div>
                                <div class="page-card-body">
                                    <form method="POST">
                                        <div class="mb-3">
                                            <label class="form-label">Email</label>
                                            <input type="email" name="email" class="form-control" 
                                                   value="<?php echo htmlspecialchars($member['email'] ?? ''); ?>">
                                        </div>
                                        
                                        <div class="mb-3">
                                            <label class="form-label">Phone Number</label>
                                            <input type="text" name="phone_number" class="form-control" 
                                                   value="<?php echo htmlspecialchars($member['phone_number'] ?? ''); ?>">
                                        </div>
                                        
                                        <div class="alert alert-info">
                                            <i class="ri-information-line me-2"></i>
                                            Only contact information can be updated here. For changes to personal details, please contact the administrator.
                                        </div>
                                        
                                        <button type="submit" class="btn btn-primary">
                                            <i class="ri-save-line me-2"></i>Update Contact Info
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
