<template>
  <AuthShell>
    <form class="auth-form" @submit.prevent="onSubmit">
      <div class="auth-form__body">
        <AuthHeading title="تغییر رمز عبور" subtitle="در این قسمت رمز عبور جدید خود را وارد کنید" />

        <TextField
          v-model="form.phone"
          name="phone"
          label="شماره موبایل"
          :disabled="!!(user?.phone || user?.phone)"
        >
          <template #appendIcon>
            <SvgIcon name="phone" :size="22" color="var(--color-thd-gray)" />
          </template>
        </TextField>

        <PasswordField
          v-model="form.password"
          name="password"
          label="رمز عبور"
          :error="errors.password"
          @blur="validateAt('password', form.password)"
        />

        <PasswordField
          v-model="form.passwordConfirmation"
          name="passwordConfirmation"
          label="تکرار رمز عبور"
          :error="errors.passwordConfirmation"
          @blur="validateAt('passwordConfirmation', form.passwordConfirmation)"
        />

        <BaseButton
          type="submit"
          text="تایید رمز عبور"
          :loading="resetMutation.isPending.value"
          custom-class="reset__submit"
        >
          <template #appendIcon>
            <SvgIcon name="arrow-left" :size="22" color="#fff" />
          </template>
        </BaseButton>
      </div>

      <div class="auth-form__footer">
        <AuthCopyright />
      </div>
    </form>
  </AuthShell>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import useYup from '@/composables/useYup'
import useAuth from '@/composables/useAuth'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import TextField from '@/components/form/TextField.vue'
import { resetPasswordSchema } from '@/features/auth/schema'
import PasswordField from '@/components/form/PasswordField.vue'
import AuthShell from '@/features/auth/components/AuthShell.vue'
import { useResetPasswordMutation } from '@/services/query/auth'
import AuthHeading from '@/features/auth/components/AuthHeading.vue'
import AuthCopyright from '@/features/auth/components/AuthCopyright.vue'
import { usePasswordReset } from '@/features/auth/composables/usePasswordReset'

const { user } = useAuth()
const { phone: resetPhone, token: resetToken, clearResetTicket } = usePasswordReset()
const router = useRouter()

const schema = resetPasswordSchema

const form = ref({
  phone: resetPhone.value || user.value?.phone || '',
  password: '',
  passwordConfirmation: '',
})
const { validate, validateAt, errors } = useYup(schema, () => form.value)

const resetMutation = useResetPasswordMutation()

const onSubmit = async () => {
  const { isValid, payload } = await validate(form.value)
  if (!isValid) return
  await resetMutation.mutateAsync({
    phone: form.value.phone,
    token: resetToken.value,
    password: payload.password,
    passwordConfirmation: payload.passwordConfirmation,
  })
  clearResetTicket()
  router.push({ name: 'login' })
}
</script>

<style lang="scss" scoped>
.auth-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex-grow: 1;

  &__body {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex-grow: 1;
    width: 100%;
    padding: 1rem 0;

    @media (min-width: 768px) {
      justify-content: center;
    }
  }

  &__footer {
    margin-bottom: 1rem;
    text-align: center;
    width: 100%;
  }
}

.reset__submit {
  margin-top: 1.5rem;
  width: 100%;
}
</style>
