<?php include('./config/db.php'); ?>
<?php
session_start();
if($_POST){
    $txtUsuario=(isset($_POST['txtUsuario']))?$_POST['txtUsuario']:"";
    $txtPassword=(isset($_POST['txtPassword']))?$_POST['txtPassword']:"";
    $txtPassword = base64_encode($localBase."".$txtPassword);
    try{
        $sql=$conexion->prepare("SELECT * FROM usuarios WHERE run=:run AND contrasenna=:pass and activo=1;");  
        $sql->bindParam(":run",$txtUsuario);
        $sql->bindParam(":pass",$txtPassword);
        $sql->execute();
        if($sql->rowCount() > 0){
            $usuario = $sql->fetch(PDO::FETCH_LAZY);
            $_SESSION['usuario'] = "ok";
            $_SESSION['nomUsuario'] = $usuario['nombres'] . " " . $usuario['apellidos'];
            $_SESSION['id'] = $usuario['id'];
            $_SESSION['perfil'] = $usuario['perfil'];
            echo "<script>location.href='index.php?pagina=1';</script>";
            $conexion=null;
        }else{
            $mensaje = "Error: El usuario y/o contraseña incorrectos.";
        }
    }
    catch(Exception $err){
        $mensaje = "Ha ocurrido un error al procesar el ingreso";
    }
}
?>
<!doctype html>
<html lang="en">
    <head>
        <title>Acceso Administrador</title>
        <link rel="icon" type="image/png" href="./img/favicon.png" sizes="16x16">
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="./css/bootstrap.min.css" />
        <link rel="stylesheet" href="./css/bootstrap-icons.css" />
    </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-md-4">
            </div>
            <div class="col-md-4">
                <br/><br/>
                <div class="card">
                    <div class="card-header">
                        <h6>
                            Sistema de prevenci&oacute;n de riesgos
                        <h6/>
                    </div>
                    <div class="card-body">
                        <?php if(isset($mensaje)) {?>
                            <div class='alert alert-danger' role="alert">
                                <?php echo $mensaje; ?>
                            </div>
                        <?php }?>
                        <form method="POST">
                            <div class = "form-group">
                                <div class="text-center">
                                    <img src="./img/login.jpg" width="60%" height="70%" class="responsive"/>
                                </div>
                            </div>
                            <br>
                            <div class = "input-group flex-nowrap">
                                <span class="input-group-text" id="addon-wrapping">
                                    <i class="bi bi-person-fill" style="font-size: 1rem; color: #6F6D6D;"></i>
                                </span>
                                <div class="form-floating">
                                    <input type="text" class="form-control" name="txtUsuario" id="floatingInput" 
                                    placeholder="Ingrese usuario">
                                    <label for="floatingInput">Ingrese usuario</label>
                                </div>
                            </div>

                            <div class="input-group flex-nowrap">
                                <span class="input-group-text" id="addon-wrapping">
                                    <i class="bi bi-key-fill" style="font-size: 1rem; color: #6F6D6D;"></i>
                                </span>
                                <div class="form-floating">
                                    <input type="password" class="form-control" name="txtPassword" id="txtPassword" 
                                    placeholder="Ingrese contraseña">
                                    <label for="floatingInput">Ingrese contraseña</label>
                                </div>
                            </div>
                            <br/>
                            <button type="submit" class="btn btn-outline-success">Ingresar</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
  </body>
</html>
