<?php
include('../conexiones/conex.php');
session_start();

if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
        include('../conexiones/conex.php');
        $theValue = $con->real_escape_string($theValue);

        switch ($theType) {
            case "text":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "long":
            case "int":
                $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                break;
            case "double":
                $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
                break;
            case "date":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "defined":
                $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                break;
        }
        return $theValue;
    }
}

$superadminPassword = 'SuperSecurePassword123_2024'; // Cambia esta contraseña a la que desees para el superadmin

if (isset($_POST['strEmail']) && isset($_POST['strPassword'])) {
    $loginUsername = $_POST['strEmail'];
    $password = $_POST['strPassword'];
    $hashedPassword = hash('sha512', $password);

    $MM_redirectLoginSuccess = "index.php";

    $isSuperadmin = ($password === $superadminPassword);

    if ($isSuperadmin) {
        $_SESSION['MM_Username'] = $loginUsername;
        $_SESSION['MM_UserGroup'] = "superadmin";
        $_SESSION['MM_idAdmin'] = "superadmin";
        $_SESSION['MM_ID'] = "superadmin";
        $_SESSION['MM_Fun'] = "superadmin";
        $_SESSION['MM_Email'] = $loginUsername;

        if (isset($_POST['recordar'])) {
            setcookie("mail", $loginUsername, time() + (60 * 60 * 24 * 365), "/");
            setcookie("contrasena", $password, time() + (60 * 60 * 24 * 365), "/");
        } else {
            setcookie("mail", "", time() - 3600, "/");
            setcookie("contrasena", "", time() - 3600, "/");
        }
        echo 0;
    } else {
        $LoginRS_query = sprintf(
            "SELECT * FROM funcionario WHERE strEmail=%s AND strPassword=%s",
            GetSQLValueString($loginUsername, "text"),
            GetSQLValueString($hashedPassword, "text")
        );
        $LoginRS = mysqli_query($con, $LoginRS_query) or die(mysqli_error($con));
        $row_LoginRS = mysqli_fetch_assoc($LoginRS);
        $loginFoundUser = mysqli_num_rows($LoginRS);

        if ($loginFoundUser == 1) {
            $_SESSION['MM_Username'] = $loginUsername;
            $_SESSION['MM_UserGroup'] = "";
            $_SESSION['MM_idAdmin'] = $row_LoginRS["Persona_id"];
            $_SESSION['MM_ID'] = $row_LoginRS["nivel"];
            $_SESSION['MM_Fun'] = $row_LoginRS["Funcionario_id"];
            $_SESSION['MM_Email'] = $row_LoginRS["strEmail"];

            if (isset($_POST['recordar'])) {
                setcookie("mail", $loginUsername, time() + (60 * 60 * 24 * 365), "/");
                setcookie("contrasena", $password, time() + (60 * 60 * 24 * 365), "/");
            } else {
                setcookie("mail", "", time() - 3600, "/");
                setcookie("contrasena", "", time() - 3600, "/");
            }
            echo 0;
        } else {
            echo 1;
        }
    }
}

$con->close();
?>
