<?php
    include("conexioncrud.php");
    $email = "";
    $password = "";
    $email = $_POST['email']; //es el nombre de usuario
    $password = $_POST['password'];
    $error = "";
    $success = "";
    $error2 = "";
    if (isset($_POST['submit'])) {
      //VERIFICAR SQL, EN ESTE CASO EL ESTADO, SI EXISTE EN LA BD
        $consulta="SELECT * FROM usuario WHERE usuario='$email' AND estado = 'Activo'";
        $resultado=mysqli_query($con, $consulta);
        $row = mysqli_fetch_array($resultado);
        if ($email == $row['usuario']) {
            if ($password == $row['contrasena']) {
                $filas=mysqli_num_rows($resultado); //contador de filas.
                $rol= $row['ROL'];
                session_start();
                $_SESSION['id_usuarioo'] = $row['id_usuario'];
                $_SESSION['rol'] = $row['rol'];

                $error = "";
                $success = "Bienvenido!";
                if ($row['rol'] == "Administrador") {
                    header("Location:dashboard.php");
                }
                
               
            }else{
                $error2 = "Error! ";
                $error = "Contraseña Inválida!";
                
                $success = "";
            }
        }else{
            $error2 = "Error! ";
            $error = "Usuario Inválido!";
            $success = "";
        }
    }
?>

<!doctype html>
<html lang="en" class="light-theme">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- loader-->
  <link href="assets/css/pace.min.css" rel="stylesheet" />
  <script src="assets/js/pace.min.js"></script>

  <!--plugins-->
  <link href="assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />

  <!-- CSS Files -->
  <link href="assets/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/css/bootstrap-extended.css" rel="stylesheet">
  <link href="assets/css/style.css" rel="stylesheet">
  <link href="assets/css/icons.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">

  <title>SISTEMA WEB PARA VOTACIONES</title>
</head>

<body class="bg-white">

  <!--start wrapper-->
  <div class="wrapper">
    <div class="">
      <div class="row g-0 m-0">
        <div class="col-xl-6 col-lg-12">
          <div class="login-cover-wrapper">
            <div class="card shadow-none">
              <div class="card-body">
                <div class="text-center">
                  <h4>¡Bienvenido!</h4>
                  <p>Ingrese usuario y contraseña</p>
                </div>
                <div class="text-danger text-center">
                    <strong><?php echo $success; ?></strong> <?php echo $error2; echo $error; ?>
                </div>
                <form class="form-body row g-3" method="post">
                  <div class="col-12">
                    <label for="email" class="form-label">Usuario</label>
                    <input type="text" class="form-control" name="email" id="email">
                  </div>
                  <div class="col-12">
                    <label for="password" class="form-label">Contraseña</label>
                    <input type="password" class="form-control" name="password" id="password">
                  </div>
                  <!--<div class="col-12 col-lg-6">
                    <div class="form-check form-switch">
                      <input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckRemember">
                      <label class="form-check-label" for="flexSwitchCheckRemember">Remember Me</label>
                    </div>
                  </div>-->
                  <div class="col-12 col-lg-6 text-center">
                    <a href="#">¿Olvidaste la contraseña?</a>
                  </div>
                  <div class="col-12 col-lg-12">
                    <div class="d-grid">
                      <button type="submit" name="submit" class="btn btn-primary">Ingresar</button>
                    </div>
                  </div>
                  <!--<div class="col-12 col-lg-12">
                    <div class="position-relative border-bottom my-3">
                      <div class="position-absolute seperator translate-middle-y">or continue with</div>
                    </div>
                  </div>
                  <div class="col-12 col-lg-12">
                    <div class="social-login d-flex flex-row align-items-center justify-content-center gap-2 my-2">
                      <a href="javascript:;" class=""><img src="assets/images/icons/facebook.png" alt=""></a>
                      <a href="javascript:;" class=""><img src="assets/images/icons/apple-black-logo.png" alt=""></a>
                      <a href="javascript:;" class=""><img src="assets/images/icons/google.png" alt=""></a>
                    </div>
                  </div>
                  <div class="col-12 col-lg-12 text-center">
                    <p class="mb-0">Don't have an account? <a href="authentication-sign-up-cover.html">Sign up</a></p>
                  </div>-->
                </form>
              </div>
              <div style="text-align: center;">
            <!-- Enlace que redirige al dashboard -->
            <a href="#">
              <img src="renovacion_nacional.png" style="width: 240px; height: 80px;" class="logo-text" alt="Logo">
            </a>
          </div>
            </div>
            
          </div>

        </div>
        <div class="col-xl-6 col-lg-12">
          <div class="position-fixed top-0 h-100 d-xl-block d-none login-cover-img">
          </div>
        </div>
      </div>
      <!--end row-->
    </div>
  </div>
  <!--end wrapper-->


</body>

</html>