<?php
session_start();

// If the admin is not logged in, redirect to login page
if (!isset($_SESSION['admin_id'])) {
    header("Location: memberpassword.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);

    // Get the member's ID and the new password
    $memberEmail = $_POST['email'];
    $newPassword = $_POST['new_password'];

    // Check if member exists
    $stmt = $conn->prepare("SELECT id FROM memberships WHERE email = ?");
    $stmt->bind_param("s", $memberEmail);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($row = $result->fetch_assoc()) {
        // Hash the new password
        $newHashed = password_hash($newPassword, PASSWORD_DEFAULT);

        // Update the password in the database
        $update = $conn->prepare("UPDATE memberships SET password = ? WHERE email = ?");
        $update->bind_param("ss", $newHashed, $memberEmail);
        if ($update->execute()) {
            $success = "Password for {$memberEmail} updated successfully!";
        } else {
            $error = "Something went wrong. Please try again.";
        }
        $update->close();
    } else {
        $error = "No member found with that email.";
    }

    $stmt->close();
    $conn->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Change Member 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 Member 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 enters member email and new password -->
        <form method="POST">
            <div class="mb-3">
                <label for="email">Member's Email</label>
                <input type="email" id="email" name="email" 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>
