<?php
require_once 'includes/config.php';
redirectIfNotLoggedIn();

$userData = getMemberData($pdo, $_SESSION['user_id']);
$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fullname = trim($_POST['fullname'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $dob = $_POST['dob'] ?? '';
    $gender = $_POST['gender'] ?? '';
    $address = trim($_POST['address'] ?? '');
    $city = trim($_POST['city'] ?? '');
    $state = trim($_POST['state'] ?? '');
    $pincode = trim($_POST['pincode'] ?? '');
    $occupation = trim($_POST['occupation'] ?? '');
    
    if (empty($fullname)) {
        $error = 'Full name is required';
    } else {
        $profile_photo = $userData['profile_photo'];
        
        if (isset($_FILES['profile_photo']) && $_FILES['profile_photo']['error'] == 0) {
            $uploadResult = uploadFile($_FILES['profile_photo']);
            if (isset($uploadResult['success'])) {
                // Delete old photo if not default
                if ($profile_photo != 'default.jpg' && file_exists(UPLOAD_PATH . $profile_photo)) {
                    unlink(UPLOAD_PATH . $profile_photo);
                }
                $profile_photo = $uploadResult['filename'];
            }
        }
        
        try {
            $stmt = $pdo->prepare("UPDATE members SET fullname = ?, phone = ?, dob = ?, gender = ?, 
                                  address = ?, city = ?, state = ?, pincode = ?, occupation = ?, profile_photo = ? 
                                  WHERE id = ?");
            $stmt->execute([$fullname, $phone, $dob, $gender, $address, $city, $state, $pincode, $occupation, $profile_photo, $_SESSION['user_id']]);
            
            $success = 'Profile updated successfully';
            $userData = getMemberData($pdo, $_SESSION['user_id']);
        } catch (Exception $e) {
            $error = 'Update failed: ' . $e->getMessage();
        }
    }
}
?>

<?php include 'header.php'; ?>

<div class="row">
    <div class="col-md-4">
        <div class="card text-center">
            <div class="card-body">
                <img src="<?php echo UPLOAD_URL . $userData['profile_photo']; ?>" class="profile-pic mb-3" onerror="this.src='assets/images/default-avatar.png'">
                <h4><?php echo htmlspecialchars($userData['fullname']); ?></h4>
                <p class="text-muted"><?php echo htmlspecialchars($userData['email']); ?></p>
                <p><strong>Membership No:</strong> <?php echo $userData['membership_number']; ?></p>
            </div>
        </div>
    </div>
    
    <div class="col-md-8">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h4 class="mb-0"><i class="fas fa-user-edit me-2"></i>Edit Profile</h4>
            </div>
            <div class="card-body">
                <?php if ($error): ?>
                    <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>
                
                <?php if ($success): ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                <?php endif; ?>
                
                <form method="POST" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Full Name *</label>
                                <input type="text" class="form-control" name="fullname" value="<?php echo htmlspecialchars($userData['fullname']); ?>" required>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">Phone Number</label>
                                <input type="tel" class="form-control" name="phone" value="<?php echo htmlspecialchars($userData['phone']); ?>">
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">Date of Birth</label>
                                <input type="date" class="form-control" name="dob" value="<?php echo $userData['dob']; ?>">
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">Gender</label>
                                <select class="form-control" name="gender">
                                    <option value="">Select</option>
                                    <option value="male" <?php echo $userData['gender'] == 'male' ? 'selected' : ''; ?>>Male</option>
                                    <option value="female" <?php echo $userData['gender'] == 'female' ? 'selected' : ''; ?>>Female</option>
                                    <option value="other" <?php echo $userData['gender'] == 'other' ? 'selected' : ''; ?>>Other</option>
                                </select>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Profile Photo</label>
                                <input type="file" class="form-control" name="profile_photo" accept="image/*">
                                <small class="text-muted">Leave empty to keep current photo</small>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">Occupation</label>
                                <input type="text" class="form-control" name="occupation" value="<?php echo htmlspecialchars($userData['occupation']); ?>">
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Address</label>
                        <textarea class="form-control" name="address" rows="2"><?php echo htmlspecialchars($userData['address']); ?></textarea>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">City</label>
                                <input type="text" class="form-control" name="city" value="<?php echo htmlspecialchars($userData['city']); ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">State</label>
                                <input type="text" class="form-control" name="state" value="<?php echo htmlspecialchars($userData['state']); ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">Pincode</label>
                                <input type="text" class="form-control" name="pincode" value="<?php echo htmlspecialchars($userData['pincode']); ?>">
                            </div>
                        </div>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">Update Profile</button>
                    <a href="dashboard.php" class="btn btn-secondary">Cancel</a>
                </form>
            </div>
        </div>
        
        <!-- Change Password Section -->
        <div class="card mt-4">
            <div class="card-header bg-warning text-dark">
                <h4 class="mb-0"><i class="fas fa-key me-2"></i>Change Password</h4>
            </div>
            <div class="card-body">
                <form method="POST" action="change_password.php">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="mb-3">
                                <label class="form-label">Current Password</label>
                                <input type="password" class="form-control" name="current_password" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">New Password</label>
                                <input type="password" class="form-control" name="new_password" required minlength="6">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Confirm New Password</label>
                                <input type="password" class="form-control" name="confirm_password" required>
                            </div>
                            <button type="submit" name="change_password" class="btn btn-warning">Change Password</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>