<template>
  <section class="admin-login">
    <!--Importar componente de header para login -->
    <Header />
    <div>
      <h1>Hackaton UNAM-AWS</h1>

      <div class="conteiner ">
        <div class="admin-campos">
          <i class="fa fa-user"></i>
          <input
            type="email"
            placeholder="Usuario"
            v-model="usuario"
            @keyup.enter="entrar()"
          />
        </div>

        <div class="admin-campos">
          <i class="fa fa-lock"></i>
          <input
            type="password"
            placeholder="Contraseña"
            v-model="password"
            @keyup.enter="entrar()"
          />
        </div>

        <div class="entrar">
          <button
            class="button"
            id="boton_entrar"
            v-if="validar_ambos()"
            @click="entrar()"
          >
            Entrar
          </button>
          <p class="botton_falso" v-else>Entrar</p>
        </div>
      </div>
      <!--Importar componente de footer para login en esta ocacion yo no lo ocuper por eso esta comentado -->
      <!-- <Footer /> -->
    </div>
  </section>
</template>

<script>
import axios from "axios";
import config from "../config/config.js";
import swal from "sweetalert";
import Header from "./../view/headerAdmin.vue";

export default {
  components: {
    //HelloWorld
    Header
  },
  data() {
    return {
      usuario: "",
      password: "",
      error: false,
      mal: null,
      resp: null
    };
  },
  methods: {
    malo() {
      return this.error;
    },
    validar_ambos() {
      if (this.usuario != "" && this.password != "") return true;
      else return false;
    },
    entrar() {
      const data = {
        usuario: this.usuario,
        password: this.password
      };

      axios
        .post(`${config.api}/login`, data)
        .then(async respose => {
          this.resp = respose.data;
          if (respose.data.err == false) {
            await swal(`Bienvenido ${this.usuario}`, "", "success");

            window.localStorage.setItem("id_usuario", respose.data.id_usuario);
            this.$router.push("/Registros");
          } else {
            this.error = true;
            await swal(" Usuario o Contraseña incorrecta.", "", "error");
          }
        })
        .catch(err => {
          swal(" Usuario o Contraseña incorrecta.", "", "error");
          this.resp = err;
          this.error = true;
        });
    }
  },
  beforeCreate() {
    localStorage.clear();
  }
};
</script>

<style scoped>
.button {
  background-color: #1b3d70; /* Green */
  border: none;
  color: white;
  padding: 1rem 7rem;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;

  cursor: pointer;
  font-size: 1.5rem;
  border-radius: 1rem;
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.botton_falso {
  background-color: #ccc; /* Green */
  border: none;
  color: white;
  padding: 1rem 7rem;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  cursor: pointer;
  font-size: 1.5rem;
  border-radius: 1rem;
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.button:hover {
  background-color: #bb8800; /* Green */
  color: white;
}
h1 {
  margin-top: 2.5rem;
  margin-bottom: 3.5rem;
  font-size: 1.5rem;
}

.admin-campos {
  width: 20rem;
  margin: 0 auto 1rem auto;
  position: relative;
}

.admin-campos input[type="email"],
.admin-campos input[type="password"] {
  border-bottom: black 0.2rem solid;
  border-left: white;
  border-right: white;
  border-top: white;
  display: block;
  font-size: 1rem;
  margin: 0 auto 2rem auto;
  padding: 1rem 0 1rem 2rem;
  width: 85%;
}

.admin-campos input::placeholder,
.admin-campos input::placeholder {
  color: rgb(75, 71, 71);
}

.admin-campos i {
  color: black;
  font-size: 1.5rem;
  left: 1.75rem;
  position: absolute;
  top: 0.75rem;
}

.entrar {
  display: flex;
  justify-content: center;
  margin-top: 5rem;
}

@media screen and (min-width: 1024px) {
  h1 {
    margin-top: 3rem;
    margin-bottom: 4rem;
    font-size: 2.5rem;
  }
  .admin-login img {
    height: 25rem;
  }

  .admin-campos {
    width: 40rem;
  }

  .admin-campos input[type="email"],
  .admin-campos input[type="password"] {
    font-size: 1.5rem;
    padding: 1rem 0 1rem 4rem;
  }

  .admin-campos i {
    font-size: 2rem;
    left: 3.5rem;
  }
}
</style>
