<?php
session_start();

// DB connection
$conn = new PDO('mysql:host=localhost; dbname=omeixzen_vogue', 'omeixzen_vogue', 'vogueJan12345$12345');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Sample login simulation (replace with real login logic)
$_SESSION['user_id'] = 1;

$user_id = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT * FROM admin WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();

$message = "";

if (isset($_POST['change_password'])) {
    $new_password     = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];

    if ($new_password === $confirm_password) {
        $hashed = password_hash($new_password, PASSWORD_DEFAULT);
        $update = $conn->prepare("UPDATE admin SET password = ? WHERE id = ?");
        $update->execute([$hashed, $user_id]);
        $message = "<div class='alert alert-success'>رمز عبور جدید با موفقیت ذخیره شد.</div>";
    } else {
        $message = "<div class='alert alert-danger'>رمز عبور جدید با تکرار آن مطابقت ندارد.</div>";
    }
}
?>

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>تغییر رمز عبور</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-5">
    <div class="card mx-auto" style="max-width: 500px;">
        <div class="card-header bg-primary text-white text-center">تغییر رمز عبور</div>
        <div class="card-body">
            <?php echo $message; ?>
            <form method="post">
                <div class="mb-3">
                    <label class="form-label">رمز عبور جدید</label>
                    <input type="password" name="new_password" class="form-control" required>
                </div>

                <div class="mb-3">
                    <label class="form-label">تکرار رمز عبور جدید</label>
                    <input type="password" name="confirm_password" class="form-control" required>
                </div>

                <button type="submit" name="change_password" class="btn btn-success w-100">ذخیره رمز عبور</button>
            </form>
        </div>
    </div>
</div>

</body>
</html>
