<template>
  <div class="bg-font-black">
    <router-view></router-view>
  </div>
</template>

<script lang="ts">
import { token, userMsg } from "@/util/userInfoUtils";
import Vue from "vue";
import { eAccountType } from "./Login/types";

export default Vue.extend({
  provide(): object {
    return {
      sendSmsFunc: this.sendSmsFunc,
      accountStatusCheckFunc: this.accountStatusCheckFunc,
      registerFunc: this.registerFunc,
      loginByCodeFunc: this.loginByCodeFunc,
      loginByPwdFunc: undefined,
      setPwdFunc: this.setPwdFunc,
      modifyPwdFunc: this.modifyPwdFunc,
    };
  },
  methods: {
    async sendSmsFunc(phone: string) {
      await this.$service.userService.getVerificationCode(phone, "1");
    },
    async accountStatusCheckFunc(phone: string) {
      const isRegisterd = await this.$service.userService.isRegisterd(phone);
      if (!isRegisterd) {
        return eAccountType.NO_REG;
      } else {
        const isPwdSet = await this.$service.userService.isPwdSet(phone);
        return isPwdSet ? eAccountType.REG_PDSET : eAccountType.REG_NOPD;
      }
    },
    async registerFunc(phone: string, code: string) {
      let ret = await this.$service.userService.loginAndSign({
        codetype: "sms",
        telephone: phone,
        verificationCode: code,
      });
      if (ret.Authorization) {
        const { Authorization, user } = ret;
        token.setToken(Authorization);
        userMsg.setUserMsg(user);
        this.$router.push({
          name: "Home",
        });
        if((user as any).authStatus == 1 && (user as any).authType == 0) {
          localStorage.setItem('isCertificate','true')
          localStorage.setItem('authInfo',JSON.stringify((user as any).authPerson))
        }
      }else if(typeof ret == 'string'){
        this.$toast(ret)
      }
    },
    async loginByCodeFunc(phone: string, code: string) {
      return this.registerFunc(phone, code);
    },
    async loginByPwdFunc(phone: string, pwd: string) {
      console.log(phone,pwd);
      
    },
    async setPwdFunc(phone: string, code: string, pwd: string) {
       console.log(phone,code,pwd);
      this.$router.push({
        name: "Home",
      });
      return 1;
    },
    async modifyPwdFunc(phone: string, originalPwd: string, newPwd: string) {
      console.log(phone,originalPwd,newPwd);
      
    },
  },
});
</script>

<style lang="less">
</style>
