<?php
session_start();
include("config.php");

$admin_username = $_SESSION['admin_username'] ?? '';
if (!$admin_username) {
    header("Location: login.php");
    exit;
}

$msg = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $current = $_POST['current_password'];
    $new = $_POST['new_password'];
    $confirm = $_POST['confirm_password'];

    $stmt = $dbh->prepare("SELECT * FROM admins WHERE username = ?");
    $stmt->execute([$admin_username]);
    $admin = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$admin || !password_verify($current, $admin['password'])) {
        $msg = "Current password is incorrect.";
    } elseif ($new !== $confirm) {
        $msg = "New passwords do not match.";
    } else {
        $hashed = password_hash($new, PASSWORD_BCRYPT);
        $update = $dbh->prepare("UPDATE admins SET password = ? WHERE username = ?");
        $update->execute([$hashed, $admin_username]);
        $msg = "Password changed successfully.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Change Password</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f1f4f9;
        }
        .form-card {
            max-width: 500px;
            margin: 60px auto;
            border-radius: 8px;
        }
        .card-header {
            font-weight: 600;
            font-size: 18px;
        }
    </style>
</head>
<body>
<?php include "includes/header.php"; ?>

<div class="container">
    <div class="card form-card shadow-sm">
        <div class="card-header text-white text-center" style="background-color:grey;">
            Change Password
        </div>
        <div class="card-body">
            <?php if ($msg): ?>
                <div class="alert alert-<?= strpos($msg, 'success') !== false ? 'success' : 'danger' ?>">
                    <?= htmlentities($msg) ?>
                </div>
            <?php endif; ?>

            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">Current Password</label>
                    <input type="password" name="current_password" class="form-control" placeholder="Enter current password" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">New Password</label>
                    <input type="password" name="new_password" class="form-control" placeholder="Enter new password" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Confirm New Password</label>
                    <input type="password" name="confirm_password" class="form-control" placeholder="Re-enter new password" required>
                </div>
                <button type="submit" class="btn btn-primary w-100" style="background-color:grey;">Change Password</button>
            </form>
        </div>
    </div>
</div>

<?php include "includes/footer.php"; ?>
</body>
</html>
