import { defineStore } from "pinia";
import { transformData, validateColor } from "@/utils";
import { getCustomCorpInfo, getCorpInfo, getCorpMemberData, getRolesByRoleId } from "@/api/corp";

const THEME_COLOR = "#007aff"; // 默认主题色

const memberStore = defineStore("member", {
  state: () => {
    return {
      logined: "unlogin",
      memberInfo: {},
      corpInfo: {},
      userId: "",
      corpId: "",
      suite_token: "",
      myExternalcontact: [],
      myExternalcontactLoading: false,
      wxContact: {}, //微信好友 非企微员工
      teams: [],
      currentTeam: [],
      isAdmin: false,
    };
  },
  getters: {
    isHangzheng(state) {
      // 杭州整形医院
      return state.corpInfo && state.corpInfo.corpId === "wpLgjyawAAJ69XjD39GMOXp2EsYHYb3w";
    },
    corpPic(state) {
      return state.corpInfo && state.corpInfo.corpPic ? state.corpInfo.corpPic : "";
    },
    customLogo(state) {
      return state.corpInfo && state.corpInfo.customLogo ? state.corpInfo.customLogo : '';
    },
    themeColor(state) {
      return state.corpInfo && validateColor(state.corpInfo.themeColor) ? state.corpInfo.themeColor : THEME_COLOR;
    },
    customerSourceList(state) {
      return state.corpInfo && Array.isArray(state.corpInfo.customerSourceList) ? transformData(state.corpInfo.customerSourceList) : [];
    },
    menus(state) {
      return state.memberInfo && Array.isArray(state.memberInfo.menu) ? state.memberInfo.menu : [];
    },
    intentedProjectList(state) {
      return state.corpInfo && Array.isArray(state.corpInfo.intentedProjectList)
        ? state.corpInfo.intentedProjectList.map((i) => ({
            label: i,
            value: i,
          }))
        : [];
    },
    roleIds(state) {
      return state.memberInfo && Array.isArray(state.memberInfo.roleIds) ? state.memberInfo.roleIds : [];
    },
    baseInfo(state) {
      const fixed = [{ label: "联系电话", prop: "assistantByMobile" }];
      const rows =
        state.corpInfo && state.corpInfo.corpFileds && Array.isArray(state.corpInfo.corpFileds.baseInfo)
          ? state.corpInfo.corpFileds.baseInfo.map(({ name: label, required, title: prop }) => ({
              label,
              prop,
              required,
            }))
          : [];
      return [...rows, ...fixed];
    },
    healthInfo(state) {
      return state.corpInfo && state.corpInfo.corpFileds && Array.isArray(state.corpInfo.corpFileds.healthInfo)
        ? state.corpInfo.corpFileds.healthInfo.map(({ name: label, required, title: prop }) => ({
            label,
            prop,
            required,
          }))
        : [];
    },
    viceInfo(state) {
      const fixed = [{ label: "客户阶段", prop: "customerStage", required: true }];
      const fixedTail = [{ label: "备注", prop: "notes" }];
      const rows =
        state.corpInfo && state.corpInfo.corpFileds && Array.isArray(state.corpInfo.corpFileds.viceInfo)
          ? state.corpInfo.corpFileds.viceInfo.map(({ name: label, required, title: prop }) => ({
              label,
              prop,
              required,
            }))
          : [];
      return [...fixed, ...rows, ...fixedTail];
    },
    tagList(state) {
      const tags = state.corpInfo && Array.isArray(state.corpInfo.tags) ? state.corpInfo.tags : [];
      return tags.reduce((list, item) => {
        if (Array.isArray(item.options)) {
          list.push(...item.options);
        }
        return list;
      }, []);
    },
    intentedProjectList(state) {
      return state.corpInfo && Array.isArray(state.corpInfo.intentedProjectList)
        ? state.corpInfo.intentedProjectList.map((i) => ({
            label: i,
            value: i,
          }))
        : [];
    },
  },
  actions: {
    async reloadCorpInfo(fields) {
      if (!fields || fields.length === 0) return;
      const { success, data } = await getCustomCorpInfo({ corpId: this.corpInfo.corpId, fields });
      if (success && data.data && data.data.tags && Array.isArray(data.data.tags)) {
        fields.forEach((i) => {
          if (data.data && data.data[i]) {
            this.corpInfo[i] = data.data[i];
          }
        });
      }
    },
    changeCorpInfo({ key, value }) {
      if (key) this.corpInfo[key] = value;
    },
    setLogined(status) {
      this.logined = status;
    },
    async getMemberInfo() {
      const { data, success } = await getCorpMemberData();
      if (success) {
        this.memberInfo = data.data;
        return data.data;
      } else {
        return {};
      }
    },
    async getCorpInfo() {
      const { success, data } = await getCorpInfo();
      if (success) {
        this.corpInfo = data.data[0];
        return data.data[0];
      }
    },
    // 判断是否为超级管理员
    async judgmentIsAdmin() {
      let { roleIds = [] } = this.memberInfo;
      let { data: list } = await getRolesByRoleId(roleIds);
      if (list.data && list.data.length > 0) {
        this.isAdmin = list.data.some((i) => i.roleId === "admin");
      }
    },
  },
});
export { memberStore };
