<template>
  <div class="bg-white h-screen">
    <router-view v-slot="{ Component }">
      <keep-alive>
        <component :is="Component" />
      </keep-alive>
    </router-view>
  </div>
</template>

<script lang="ts">
import {
  userChangePwd,
  userCheckPhone,
  userLogin,
  userRegister,
} from "@/service/UserManagementService";
import { eCheckPhoneStatus } from "@/service/UserManagementService/types";
import { eSmsType, sendVerifyCode } from "@/service/VerificationService";
import { dispatchLoginAction } from "@/store/chatStore";
import { eNewRoleRelatedToBackEnd, eRole } from "@/types/roleType";
import transformRole from "@/utils/transformRole";
import { setUserMsg } from "@/utils/userMsg";
import { defineComponent } from "vue";
import { eAccountType } from "./Login/types";

export default defineComponent({
  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, smsType?: eSmsType) {
      return sendVerifyCode({
        phone: phone,
        smsType: smsType !== undefined ? smsType : eSmsType.registerOrLogin,
      });
    },
    async accountStatusCheckFunc(phone: string) {
      const ret = await userCheckPhone(phone);
      if (ret && ret.code === 200) {
        if (
          ret.data === eCheckPhoneStatus.staff ||
          ret.data === eCheckPhoneStatus.user
        ) {
          return eAccountType.REG_PDSET;
        } else if (ret.data === eCheckPhoneStatus.no_reg) {
          return eAccountType.NO_REG;
        }
      }
    },
    async registerFunc(phone: string, code: string) {
      const ret = await userRegister({
        phone,
        verify_code: code,
      });
      if (ret.code === 200) {
        const { accessToken, role, phone } = ret.data;
        setUserMsg({
          token: accessToken,
          role:
            role === eNewRoleRelatedToBackEnd.ACM ? eRole.staff : eRole.user,
          role2: transformRole(role),
          userInfo: {
            phone: phone,
          },
        });
        dispatchLoginAction("login");
        this.$router.push({
          name: "Home",
        });
      } else {
        throw new Error(ret.msg);
      }
    },
    async loginByCodeFunc(phone: string, code: string) {
      const ret = await userLogin({
        is_admin: false,
        use_pwd: false,
        verify_code: code,
        phone: phone,
      });
      if (ret.code === 200) {
        const { accessToken, phone, role } = ret.data;
        setUserMsg({
          token: accessToken,
          role2: transformRole(role),
          role:
            eNewRoleRelatedToBackEnd.ACM === role ? eRole.staff : eRole.user,
          userInfo: {
            phone,
          },
        });
        dispatchLoginAction("login");
        this.$router.push({
          name: "Home",
        });
      }
    },
    async loginByPwdFunc(
      phone: string,
      pwd: string,
      config = { gohome: true }
    ) {
      const ret = await userLogin({
        is_admin: false,
        use_pwd: true,
        pwd,
        phone: phone,
      });
      if (ret.code === 200) {
        const { accessToken, phone, role } = ret.data;
        setUserMsg({
          token: accessToken,
          role2: transformRole(role),
          role:
            eNewRoleRelatedToBackEnd.ACM === role ? eRole.staff : eRole.user,
          userInfo: {
            phone,
          },
        });
        dispatchLoginAction("login");
        config.gohome &&
          this.$router.push({
            name: "Home",
          });
      } else {
        throw new Error(ret.msg);
      }
    },
    async setPwdFunc(phone: string, code: string, pwd: string) {
      const ret = await userChangePwd({
        new_pwd: pwd,
        phone,
        verify_code: code,
      });
      if (ret.code !== 200) {
        throw new Error(ret.msg);
      }
      await this.loginByPwdFunc(phone, pwd, { gohome: true });
    },
    async modifyPwdFunc(phone: string, originalPwd: string, newPwd: string) {
      console.log(phone, originalPwd, newPwd);
    },
  },
});
</script>

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