<?php
session_start();

// If the admin is not logged in, redirect to login page
if (!isset($_SESSION['admin_id'])) {
    header("Location: adminpassword.php");
    exit;
}

$success = "";
$error = "";

// Handle password change request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $conn = new mysqli("localhost", "root", "", "job_portal");

    // Check DB connection
    if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);

    $adminId = $_SESSION['admin_id']; // Get admin ID from session
    $currentPassword = $_POST['current_password'];
    $newPassword = $_POST['new_password'];

    // Fetch current password from the database
    $stmt = $conn->prepare("SELECT password FROM admins WHERE id = ?");
    $stmt->bind_param("i", $adminId);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($row = $result->fetch_assoc()) {
        // Verify if current password matches
        if (password_verify($currentPassword, $row['password'])) {
            // Hash the new password
            $newHashed = password_hash($newPassword, PASSWORD_DEFAULT);

            // Update the password in the database
            $update = $conn->prepare("UPDATE admins SET password = ? WHERE id = ?");
            $update->bind_param("si", $newHashed, $adminId);
            if ($update->execute()) {
                $success = "Password updated successfully!";
            } else {
                $error = "Something went wrong. Please try again.";
            }
            $update->close();
        } else {
            $error = "Current password is incorrect.";
        }
    } else {
        $error = "Admin not found.";
    }

    $stmt->close();
    $conn->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Change Password</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        body {
            background-color: #1abc9c;
            font-family: 'Poppins', sans-serif;
            display: flex;
            height: 100vh;
            justify-content: center;
            align-items: center;
            color: white;
        }
        .card {
            background-color: #2c3e50;
            padding: 30px;
            border-radius: 10px;
            width: 400px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }
        h4 {
            text-align: center;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="card">
        <h4>Admin Change Password</h4>

        <!-- Display success or error message -->
        <?php if ($success): ?>
            <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
        <?php elseif ($error): ?>
            <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>

        <!-- Admin password change form -->
        <form method="POST">
            <div class="mb-3">
                <label for="current_password">Current Password</label>
                <input type="password" id="current_password" name="current_password" class="form-control" required />
            </div>
            <div class="mb-3">
                <label for="new_password">New Password</label>
                <input type="password" id="new_password" name="new_password" class="form-control" required />
            </div>
            <button class="btn btn-warning w-100" type="submit">Change Password</button>
        </form>
    </div>
</body>
</html>
