<?php
session_start();
include '../db_connect.php'; 

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = $_POST['username'];
    $pass = $_POST['password'];

    $con = dbConnect();

    // Use prepared statements to prevent SQL injection
    $sql = "SELECT * FROM admin_details WHERE username = ?";
    $stmt = $con->prepare($sql);
    $stmt->bind_param("s", $user);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();

        if ($pass === $row['password']) {
            $_SESSION['username'] = $user;
            $_SESSION['securitykey'] = 'JccK1Ide+KiYnXcuTNM3Wg==';
            header("Location: dashboard.php"); 
            exit();
        } else {
            header("Location: ../index.php?error=❌ ভুল পাসওয়ার্ড!&user=" . urlencode($user));
            exit();
        }
    } else {
        header("Location: ../index.php?error=❌ কোনো ইউজার পাওয়া যায়নি!");
        exit();
    }

    $stmt->close();
    $con->close();
} else {
    header("Location: ../index.php");
    exit();
}
?>