<?php
session_start();
include 'layouts/main.php';
include 'layouts/dbconn.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$success_msg = '';
$error_msg = '';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $current_password = $_POST['current_password'];
    $new_password = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];
    $user_id = $_SESSION['user_id'];

    if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
        $error_msg = "All fields are required.";
    } elseif ($new_password !== $confirm_password) {
        $error_msg = "New passwords do not match.";
    } elseif (strlen($new_password) < 8) {
        $error_msg = "Password must be at least 8 characters long.";
    } else {
        // Verify current password
        $sql = "SELECT password FROM users WHERE user_id = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows === 1) {
            $row = $result->fetch_assoc();
            if (password_verify($current_password, $row['password'])) {
                // Update password
                $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
                $update_sql = "UPDATE users SET password = ? WHERE user_id = ?";
                $update_stmt = $conn->prepare($update_sql);
                $update_stmt->bind_param("si", $hashed_password, $user_id);

                if ($update_stmt->execute()) {
                    $success_msg = "Password updated successfully.";
                } else {
                    $error_msg = "Error updating password. Please try again.";
                }
            } else {
                $error_msg = "Incorrect current password.";
            }
        } else {
            $error_msg = "User not found.";
        }
    }
}
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Change Password')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/sweetalert2/sweetalert2.min.css" rel="stylesheet">
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Change Password</h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                                        <li class="breadcrumb-item"><a href="profile.php">My Account</a></li>
                                        <li class="breadcrumb-item active">Change Password</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xxl-6 col-lg-8">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">Password Update</h5>
                                </div>
                                <div class="card-body">
                                    <?php if ($success_msg): ?>
                                        <div class="alert alert-success"><?php echo $success_msg; ?></div>
                                    <?php endif; ?>

                                    <?php if ($error_msg): ?>
                                        <div class="alert alert-danger"><?php echo $error_msg; ?></div>
                                    <?php endif; ?>

                                    <form method="POST" action="">
                                        <div class="row mb-3">
                                            <div class="col-lg-4">
                                                <label for="current_password" class="form-label">Current
                                                    Password</label>
                                            </div>
                                            <div class="col-lg-8">
                                                <input type="password" class="form-control" id="current_password"
                                                    name="current_password" required>
                                            </div>
                                        </div>
                                        <div class="row mb-3">
                                            <div class="col-lg-4">
                                                <label for="new_password" class="form-label">New Password</label>
                                            </div>
                                            <div class="col-lg-8">
                                                <input type="password" class="form-control" id="new_password"
                                                    name="new_password" required>
                                                <div class="form-text">Must be at least 8 characters long.</div>
                                            </div>
                                        </div>
                                        <div class="row mb-3">
                                            <div class="col-lg-4">
                                                <label for="confirm_password" class="form-label">Confirm
                                                    Password</label>
                                            </div>
                                            <div class="col-lg-8">
                                                <input type="password" class="form-control" id="confirm_password"
                                                    name="confirm_password" required>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-8 offset-lg-4">
                                                <button type="submit" class="btn btn-primary">Update Password</button>
                                                <a href="profile.php" class="btn btn-light">Cancel</a>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>

                        <div class="col-xxl-6 col-lg-4">
                            <div class="card bg-light border-0">
                                <div class="card-body">
                                    <h5 class="card-title text-primary"><i class="ri-shield-keyhole-line me-2"></i>
                                        Password Security Tips</h5>
                                    <ul class="list-unstyled mt-3 mb-0 vstack gap-2">
                                        <li><i class="ri-checkbox-circle-line text-success me-2"></i> Use a mix of
                                            letters, numbers, and symbols.</li>
                                        <li><i class="ri-checkbox-circle-line text-success me-2"></i> Avoid using
                                            personal information like birthdays.</li>
                                        <li><i class="ri-checkbox-circle-line text-success me-2"></i> Don't reuse
                                            passwords across different accounts.</li>
                                        <li><i class="ri-checkbox-circle-line text-success me-2"></i> Change your
                                            password regularly.</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>