<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: this.loginByPwdFunc,
      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) {
      const ret = await this.$service.userService.loginAndSign({
        codetype: "sms",
        telephone: phone,
        verificationCode: code,
      });
      if (ret) {
        const { Authorization, user } = ret;
        token.setToken(Authorization);
        userMsg.setUserMsg(user);
        this.$router.push({
          name: "Home",
        });
      }
    },
    async loginByCodeFunc(phone: string, code: string) {
      return this.registerFunc(phone, code);
    },
    async loginByPwdFunc(phone: string, pwd: string) {},
    async setPwdFunc(phone: string, code: string, pwd: string) {
      this.$router.push({
        name: "Home",
      });
      return 1;
    },
    async modifyPwdFunc(phone: string, originalPwd: string, newPwd: string) {},
  },
});
</script>

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