<?php
session_start();
require_once('Connections/conexionabm.php');

error_reporting(E_ALL ^ E_DEPRECATED);

if (!isset($_SESSION['cuentaveces'])) {
	$_SESSION['cuentaveces'] = 0;
}

$_SESSION['cia'] = 'SGI';

$maquina	= get_current_user();
$username	= get_current_user();

if (!empty($_SERVER['HTTP_CLIENT_IP']))
	$ip = $_SERVER['HTTP_CLIENT_IP'];

else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
	$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

else if (!empty($_SERVER['HTTP_X_FORWARDED']))
	$ip = $_SERVER['HTTP_X_FORWARDED'];

else if (!empty($_SERVER['HTTP_FORWARDED_FOR']))
	$ip = $_SERVER['HTTP_FORWARDED_FOR'];

else if (!empty($_SERVER['HTTP_FORWARDED']))
	$ip = $_SERVER['HTTP_FORWARDED'];

else if (!empty($_SERVER['REMOTE_ADDR']))
	$ip = $_SERVER['REMOTE_ADDR'];

$_SESSION['ip'] = $ip;
$_SESSION['maquina'] = $maquina;
//$_SESSION['piepagina'] = 'Inversiones y Prestamos';


if (!function_exists("GetSQLValueString")) {
	function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
	{
		if (PHP_VERSION < 6) {
			$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
		}

		$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_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;
	}
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
	$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['usuario'])) {
	$usuario 	= $_POST['usuario'];
	$contrasena	= $_POST['contrasena'];
	$existe		= 0;

	$sqlSelect = "SELECT * FROM tblusuario WHERE strusuario='$usuario' AND strpassword='$contrasena'";
	if ($resultSelect = $conn->query($sqlSelect)) {                            
		while ($row_consulta = $resultSelect->fetch_assoc()) { 
			$existe 					= 1;
			$_SESSION['id_usuario'] 	= $row_consulta['idUsuario'];
			$_SESSION['strusuario'] 	= $row_consulta['strusuario'];		
			$_SESSION['strpassword'] 	= $row_consulta['strpassword'];		
			$_SESSION['nombreuser'] 	= $row_consulta['strnombre'];
			$_SESSION['apellidouser'] 	= $row_consulta['strapellido'];	
			$_SESSION['medico'] 		= $row_consulta['strnombre'].' '.$row_consulta['strapellido'];
			$_SESSION['niveluser'] 		= $row_consulta['intnivel'];	
			$_SESSION['superv']			= $row_consulta['superv'];	
			$_SESSION['logopq']			= $row_consulta['logopq'];	
			$status						= $row_consulta['status'];	
			$_SESSION['titulo1']		= 'SGI';	
			$_SESSION['sigla']			= $row_consulta['sigla'];	

			if ($status == 0) {
				echo '<script language = javascript>
					self.location = "alerta_usuario_desabilitado"
				</script>';
			}
			if ($status == 1) {
				echo '<script language = javascript>
					self.location = "index_admin"
				</script>';
			}			
		}
	}

	if ($existe == 0) {
		echo '<script language = javascript>
			self.location = "alerta_usuario_no_existe"
		</script>'; 
	}
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SGI</title>
<style>
    body {
        font-family: 'Roboto', sans-serif;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        background: linear-gradient(to bottom, #038978, #F7F4F0);
        margin: 0;
        overflow: hidden;
    }

    .login-container {
        background: #fff;
        padding-top: 6px;
        padding-bottom:20px;
        border-radius: 10px;
        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
        animation: slideUp 0.6s ease-out;
        width: 700px;
    }

    @keyframes slideUp {
        from {
            transform: translateY(100%);
            opacity: 0;
        }
        to {
            transform: translateY(0);
            opacity: 1;
        }
    }

    .login-box {
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .login-box img.logo {
        margin-bottom: 0.5rem;
    }

    .input-group {
        margin-bottom: 1.5rem;
        text-align: left;
        position: relative;
        width: 50%;
        display: flex;
        align-items: center;
    }

    .input-group input {
        flex: 2;
        padding: 0.5rem;
        border: none;
        border-bottom: 2px solid #ddd;
        outline: none;
        transition: border-color 0.3s;
        width: 50%;
    }

    .input-group input:focus {
        border-color: #3498db;
    }

    .toggle-password {
        cursor: pointer;
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
    }

    button {
        padding: 0.75rem 2rem;
        background: #48A5AA; /*#0080C0; #58B847;*/
        color: #fff;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        transition: background 0.3s;
        width: 50%;
        height:36px;
    }

    button:hover {
        background: #3A8583;
    }
</style>
</head>
<body>
<form class="form" id="form1" name="form1" method="post" action="<?php echo $loginFormAction; ?>">
<div class="login-container">
    <div class="login-box">
        <img src="images/logo_digesett.jpg" class="logo" height="100px" width="auto">
        <span style="text-align:center; font-size:22px; font-weight:bold; color:#666; font-family:Arial">Sistema de Gestión de Investigaciones (SGI)</span>
        <br/>
        <span style="text-align:center; font-size:20px; font-weight:normal; color:#666">Inicio de Sesión</span>
        
        <br/>
        <div class="input-group">
            <input type="text" id="usuario" name="usuario" style="text-align:center; font-size:12px" placeholder="Nombre de Usuario" required>
        </div>
        <div class="input-group">
            <input type="password" id="contrasena" name="contrasena" style="text-align:center; font-size:12px" placeholder="Contraseña" required>
            <span class="toggle-password">👁️</span>
        </div>
        <br/>
        <button type="submit">Ingresar</button>
    </div>
</div>
</form>
<script>
    document.querySelector('.toggle-password').addEventListener('click', function (e) {
        const passwordInput = document.getElementById('password');
        if (passwordInput.type === 'password') {
            passwordInput.type = 'text';

            e.target.textContent = '🙈';
        } else {
            passwordInput.type = 'password';
            e.target.textContent = '👁️';
        }
    });
</script>
</body>
</html>
