<template>
  <form class="auth-form" @submit.prevent="onSubmit">
    <div class="auth-form__body">
      <AuthHeading title="ثبت نام" subtitle="در این قسمت اطلاعات خود را به صورت کامل وارد کنید" />

      <TextField
        v-model="form.firstName"
        name="firstName"
        label="نام"
        :error="errors.firstName"
        @blur="validateAt('firstName', form.firstName)"
      >
        <template #appendIcon>
          <SvgIcon name="user" :size="22" color="var(--color-thd-gray)" />
        </template>
      </TextField>

      <TextField
        v-model="form.lastName"
        name="lastName"
        label="نام خانوادگی"
        :error="errors.lastName"
        @blur="validateAt('lastName', form.lastName)"
      >
        <template #appendIcon>
          <SvgIcon name="user" :size="22" color="var(--color-thd-gray)" />
        </template>
      </TextField>

      <TextField
        v-model="form.phone"
        name="phone"
        label="شماره موبایل"
        inputmode="numeric"
        :convert-digits="true"
        :error="errors.phone"
        @blur="validateAt('phone', form.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="registerMutation.isPending.value"
        custom-class="signup-form__submit"
      >
        <template #appendIcon>
          <SvgIcon name="arrow-left" :size="22" color="#fff" />
        </template>
      </BaseButton>
    </div>

    <div class="signup-form__login-link">
      <SvgIcon name="user" :size="12" color="#A7A7A7" />
      <span>حساب کاربری دارم</span>
      <RouterLink :to="{ name: 'login' }">ورود</RouterLink>
    </div>

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

<script setup>
import { ref } from 'vue'
import { RouterLink } 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 { signupSchema } from '@/features/auth/schema'
import TextField from '@/components/form/TextField.vue'
import { useRegisterMutation } from '@/services/query/auth'
import PasswordField from '@/components/form/PasswordField.vue'
import AuthHeading from '@/features/auth/components/AuthHeading.vue'
import AuthCopyright from '@/features/auth/components/AuthCopyright.vue'

const emit = defineEmits(['next'])

const schema = signupSchema

const form = ref({
  firstName: '',
  lastName: '',
  phone: '',
  password: '',
  passwordConfirmation: '',
})
const { validate, validateAt, errors } = useYup(schema, () => form.value)

const registerMutation = useRegisterMutation()
const { savePhoneNumber } = useAuth()

const onSubmit = async () => {
  const { isValid, payload } = await validate(form.value)
  if (!isValid) return
  const name = [payload.firstName, payload.lastName].filter(Boolean).join(' ').trim()
  await registerMutation.mutateAsync({
    name,
    phone: payload.phone,
    password: payload.password,
    passwordConfirmation: payload.passwordConfirmation,
  })
  savePhoneNumber(payload.phone)
  emit('next', { phone: payload.phone })
}
</script>

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

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

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

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

    @media (min-width: 768px) {
      margin-top: 0;
    }
  }
}

.signup-form__submit {
  margin-top: 1.5rem;
  width: 100%;
}

.signup-form__login-link {
  margin: 0.5rem 0;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-family: var(--font-family-fa);
  font-size: 0.75rem;
  color: #a7a7a7;

  a {
    color: var(--color-sec-gray);
    text-decoration: underline;
    text-underline-offset: 4px;
    margin-inline-start: 0.5rem;
  }
}
</style>
