import type { FormInstance, FormRules } from 'element-plus'
import { registerApi } from '@/api/model/user'
import { useUserStore } from '@/stores/modules/user'

const { isLoginModalOpen, closeLoginModal } = useLoginModal()

// const userStore = useUserStore()
// const { login } = userStore

const loginForm = ref<any>({
  mail: '',
  code: '',
})
const emailStep = ref<number>(0)
const seconds = ref<number>(0)
const selectedAccount = ref<string>('')
const errorCodeTxt = ref<string>('')
const isEmailGoogle = ref<string>('')
export default function useRegister() {
  const nextStep = () => {
    emailStep.value++
  }
  const backStep = () => {
    if (emailStep.value > 0)
      emailStep.value--
  }
  //   const register = async () => {
  //     try {
  //       const form = {
  //         ...params.value,
  //         purchaseCategory: params.value.purchaseCategory.join(','),
  //       }
  //       await registerApi(form)
  //       nextStep()
  //     }
  //     catch (error) {
  //       console.log(error)
  //     }
  //   }

  /**
   * 二选一选择邮箱--下一步
   */
  const onSelectEmail = () => {
    selectedAccount.value = ''
    loginForm.value.mail = ''
    nextStep()
  }
  /**
   * 选择曾经登陆过的账号--下一步
   */
  const onSelectAccount = (item: any) => {
    selectedAccount.value = item.email
    loginForm.value.mail = item.email
    emailStep.value = 1
  }
  /**
   * dao: 倒计时60秒
   */
  const countSeconds = () => {
    seconds.value = 60
    const timer = setInterval(() => {
      seconds.value--
      if (seconds.value === 0)
        clearInterval(timer)
    }, 1000)
  }
  /**
   * 邮箱--发送验证码
   */
  const getMailCode = async () => {
    try {
      if (seconds.value > 0)
        return
      // 获取验证码
      //   const res = await registerApi(loginForm.value.mail)
      countSeconds()
    }
    catch (error) {
      console.error('Error sending email code:', error)
    }
  }
  /**
   * 邮箱--验证是否存在--是否与已存在google的邮箱--下一步
   * @returns
   */
  async function sendEmail(formEl: FormInstance | undefined) {
    if (!formEl)
      return
    await formEl.validate(async (valid, fields) => {
      if (valid) {
      // 验证已存在google的邮箱

        isEmailGoogle.value = 'google' // 模拟google邮箱验证
        nextStep()
      }
      else { console.log('error submit!', fields) }
    })
  }
  /**
   * 邮箱--验证码--完成注册
   * @returns
   */
  async function finishCode(formEl: FormInstance | undefined) {
    errorCodeTxt.value = ''
    if (!formEl)
      return
    await formEl.validate(async (valid, fields) => {
      if (valid)
        await handleLogin()

      else console.log('error submit!', fields)
    })
  }
  /**
   * 登录--提交
   */
  async function handleLogin() {
    try {
    //   await login(loginForm.value)
    //   closeLoginModal({ status: true, isFirstLogin: true })
      isLoginModalOpen.value = false
    }
    catch (err) {
      errorCodeTxt.value = 'The code is wrong or invalid.'
    }
  }
  return { emailStep, loginForm, selectedAccount, onSelectAccount, errorCodeTxt, seconds, isEmailGoogle, sendEmail, nextStep, onSelectEmail, finishCode, backStep, getMailCode }
}
