<template>
  <q-card class="sign-in-card relative-position">
    <q-form @submit="onSubmit" @reset="void null">
      <q-btn
        class="absolute-top-right z-top"
        flat
        dense
        color="black"
        icon="close"
        label=""
        @click="showLoginForm = false"
      />
      <q-card-section align="center" class="sign-in-form-title">
        Вход в Universo
      </q-card-section>

      <q-card-section class="text-field">
        <input
          class="text-field__input"
          type="text"
          name="login"
          placeholder="Укажите адрес электронной почты"
          v-model="email"
        />
      </q-card-section>
      <q-card-section>
        <input
          class="text-field__input"
          type="password"
          name="password"
          placeholder="Укажите пароль"
          v-model="password"
        />
      </q-card-section>
      <q-card-section align="center">
        <q-btn
          no-caps
          label="Войти"
          type="submit"
          class="sign-in-form-button"
          v-close-popup
        />
      </q-card-section>
      <q-card-section>
        <q-separator />
      </q-card-section>
      <q-card-section align="center">
        <q-btn
          flat
          no-caps
          label="Не удается войти?"
          class="sign-in-form-button2"
          to="/restarigo"
          v-close-popup
        />
        <q-btn
          flat
          no-caps
          label="Зарегистрировать аккаунт"
          class="sign-in-form-button2"
          to="/registrado"
          v-close-popup
        />
      </q-card-section>
    </q-form>
  </q-card>
</template>

<script>
import { useAuthStore } from "src/stores/auth";
import { mapWritableState, mapActions } from "pinia";
import { useUiStore } from "src/stores/UI";
export default {
  name: "signInDialog",
  setup() {
    return {};
  },
  data() {
    return {
      email: "",
      password: "",
    };
  },
  computed: {
    ...mapWritableState(useUiStore, ["showLoginForm"]),
  },
  methods: {
    ...mapActions(useAuthStore, ["login"]),
    ...mapActions(useAuthStore, ["getMe"]),

    onSubmit() {
      this.login(this.email, this.password)
        .then(() => {
          this.getMe()
            .then(() => {
              this.$router.push("/");
            })
            .catch(() => {});
        })
        .catch((err) => {
          if (err.message) {
            this.$q.notify({ message: err.message, type: "negative" });
          } else console.log(err);
        });
    },
  },
};
</script>
<style scoped>
*,
*::before,
*::after {
  box-sizing: border-box;
}
input[type="text"],
input[type="password"] {
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  margin: 0;
}

.text-field__input {
  display: block;
  width: 100%;
  height: calc(2.25rem + 2px);
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: rgba(0, 0, 0, 0.4);
  background-color: rgba(217, 217, 217, 0.8);
  background-clip: padding-box;
  border: 1px solid rgba(0, 0, 0, 0.4);
  border-radius: 0.25rem;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.text-field__input::placeholder {
  color: rgba(0, 0, 0, 0.4);
  opacity: 0.4;
}

.text-field__input:focus {
  color: rgba(0, 0, 0, 0.4);
  background-color: rgba(217, 217, 217, 0.8);
  border-color: rgba(0, 0, 0, 0.4);
  outline: 0;
  box-shadow: 0 0 0 0.2rem rgba(158, 158, 158, 0.25);
}
.sign-in-card {
  background: rgba(217, 217, 217, 0.8);
  clip-path: polygon(
    13px 0,
    100% 0,
    100% calc(100% - 13px),
    calc(100% - 13px) 100%,
    0 100%,
    0 13px
  );
}
.sign-in-form-title {
  font-style: normal;
  font-weight: 700;
  font-size: 16px;
  line-height: 19px;
}
.sign-in-form-button {
  font-style: normal;
  font-weight: 700;
  font-size: 14px;
  line-height: 16px;
  text-align: center;
  width: 100%;
  height: calc(2.25rem + 2px);
  padding: 0.375rem 0.75rem;
  border-radius: 10px;
  color: #ffffff;
  background: rgba(0, 104, 104, 0.8);
}

.sign-in-form-button2 {
  font-style: normal;
  font-weight: 700;
  font-size: 14px;
  line-height: 16px;
  text-align: center;
  color: rgba(0, 104, 104, 0.9);
}
</style>
