<template>
  <ie-page bg-color="#F6F8FA">
    <ie-navbar title="手机号验证" />
    <view class="h-20"></view>
    <view class="px-46 py-50 bg-white">
      <ie-input v-model="form.phone" type="number" :maxlength="11" placeholder="请输入手机号" />
      <ie-input custom-class="mt-28" type="number" :maxlength="6" v-model="form.password" placeholder="请输入验证码">
        <ie-sms :phone="form.phone" :sms-api-type="EnumSmsApiType.NO_VALIDATION_NO_TOKEN" @send="handleSendSuccess" />
      </ie-input>
    </view>
    <ie-safe-toolbar :height="84" :shadow="false">
      <view class="px-46 pt-24">
        <ie-button type="primary" @click="handleSubmit">下一步</ie-button>
      </view>
    </ie-safe-toolbar>
  </ie-page>
</template>

<script lang="ts" setup>
import { verifyCard } from '@/api/modules/user';
import { useTransferPage } from '@/hooks/useTransferPage';
import { EnumBindScene, EnumSmsApiType } from '@/common/enum';
import { validateSms } from '@/api/modules/system';
import { validatePhone } from '@/hooks/useValidation';
import { useAppConfig } from '@/hooks/useAppConfig';
import { getUserInfo, getUserInfoWithToken, login } from '@/api/modules/login';
import { useUserStore } from '@/store/userStore';
const { prevData, transferTo } = useTransferPage();
const { isSmsCaptchaEnable } = useAppConfig();

const form = ref({
  phone: '',
  password: '',
  uuid: '',
  code: '', //图形验证码
});
const handleSendSuccess = (_phone: string, _code: string, _uuid: string) => {
  console.log('短信发送成功', _phone, _code, _uuid);
  form.value.code = _code;
  form.value.uuid = _uuid;
}
/**
 * 通过登录接口，检查手机号是否已经注册用户
 */
const checkHasReg = async () => {
  const params = {
    mobile: form.value.phone,
    code: form.value.code,
    uuid: form.value.uuid,
    password: form.value.password,
  };
  const res = await login(params);
  if (res.data) {
    const { code, message } = res.data;
    if (code === 101) {
      console.log('用户不存在')
      console.log(params)
      const submitInfo = {
        scene: EnumBindScene.REGISTER_BIND,
        userInfo: {},
        cardInfo: prevData.value.cardInfo,
        registerInfo: {
          ...params,
          // 此时注册信息需要替换为卡信息
          username: prevData.value.registerInfo.username,
          password: prevData.value.registerInfo.password,
        }
      };
      // 账户不存在，执行注册操作
      transferTo('/pagesSystem/pages/bind-profile/bind-profile', {
        data: submitInfo
      });
    }
  } else {
    // 账户已存在，执行绑卡操作
    if (res.token) {
      // 查询用户信息
      const { data } = await getUserInfoWithToken(res.token);
      console.log('用户已存在，用户信息：', data)
      // 判断用户是否是 vip
      if (!!data.cardId) {
        uni.$ie.showToast('该手机号已绑定其他账号，请使用其他手机号');
        return;
      }
      // 不是 vip 则可以绑定
      const submitInfo = {
        scene: EnumBindScene.LOGIN_BIND,
        token: res.token,
        userInfo: data,
        cardInfo: prevData.value.cardInfo,
        registerInfo: {
          ...params,
          // 此时注册信息需要替换为卡信息
          username: prevData.value.registerInfo.username,
          password: prevData.value.registerInfo.password,
        }
      };
      console.log('提交信息：', submitInfo)
      transferTo('/pagesSystem/pages/bind-profile/bind-profile', {
        data: submitInfo
      });
    }
  }
}
const handleSubmit = async () => {
  const { phone, password, uuid } = form.value;
  if (!phone || phone.trim() === '') {
    uni.$ie.showToast('请输入手机号');
    return;
  }
  if (!validatePhone(phone)) {
    uni.$ie.showToast('请输入正确的手机号');
    return;
  }
  // 开启了图形验证
  if (isSmsCaptchaEnable.value) {
    if (!uuid) {
      uni.$ie.showToast('请输入正确的验证码');
      return;
    }
  }
  if (!password || password.trim() === '') {
    uni.$ie.showToast('请输入验证码');
    return;
  }
  checkHasReg();
}
onLoad(() => {
  console.log('收到上个页面的数据', prevData.value)
});
</script>

<style lang="scss" scoped></style>
