<template>
  <form @submit.prevent="handleReset()" class="form">
    <div class="form__top">
      <h2 class="form__title">{{ t('forms.reset.title') }}</h2>
      <p class="form__subtitle">{{ t('forms.reset.subtitle') }}</p>
    </div>
    <div class="form__main">
      <ui-input
          :type="'email'"
          :placeholder="t('fields.email')"
          :label="t('fields.email')"
          :rules="[isEmail]"
          v-model="email"
          :inputMode="'email'"
      />
      <ui-button
          :type="'submit'"
          class="form__button"
          :isDisabled="!isFormValid"
          :isLoading="loading"
      >
        {{ t('buttons.sendLink') }}
      </ui-button>
      <ui-link
          @click="redirectTo('sign-in')"
      >
        <icon name="material-symbols:arrow-left-alt" size="20" />
        {{ t('forms.reset.backToLogin') }}
      </ui-link>
    </div>
  </form>
</template>

<script setup lang="ts">
import {useValidators} from '@composables/rules';
import {usePasswordReset} from '@composables/auth';
import {useProjectConfig} from "@composables/config";

const { t } = useI18n();
const appStore = useAppStore();
const localePath = useLocalePath();

const { uiConfig } = useProjectConfig();
const { isEmail } = useValidators();

const email = ref<string>('');

const redirectTo = (to) => {
  if (uiConfig.value.isAuthModals) {
    appStore.setActiveAuthState(to);
  } else {
    navigateTo(localePath(`/auth/${to}`));
  }
};

const isFormValid = computed(() => {
  return (
    isEmail(email.value) === true
  );
});

const { resetPassword, loading } = usePasswordReset();

async function handleReset() {
  await resetPassword(email.value);
}
</script>

<style lang="scss" scoped>
.form {
  width: 450px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;

  &__top {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }

  &__title {
    color: $primary;
    font-family: "Playfair Display", sans-serif;
    font-size: 30px;
    font-weight: 700;
    letter-spacing: -0.5px;
  }

  &__subtitle {
    text-align: center;
    color: $text;
    font-size: 16px;
    font-weight: 400;
    letter-spacing: -0.5px;
  }

  &__main {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 24px;

    &-block {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 10px;
    }
  }
}
</style>