import { defineStore } from 'pinia';
import { http } from '/nerv-lib/saas';
import { useApi, HttpRequestConfig } from '/nerv-lib/use/use-api';

interface AppConfig {
  projectType: string;
  projectName: string;
  baseApi: string;
  timeout: number;
  pagePermission: boolean;
  actionPermission: boolean;
  showProject: boolean;
  resourceName: string;
  useHistoryTag: boolean;
  siderPosition: string; //菜单位置
  userLoginApi: string; //登录接口
  userCustomRouterGuard?: Function; //使用自己的路由守卫
  userResourceApi: string; //获取资源接口
  userInfoApi: string; //登录详情接口
  enablePermissions: boolean; //是否开启权限
  updatePassWordInfo?: object; //修改密码配置
  headerBellInfo?: object;
  customInitMessageCount?: Function; // 自定义消息数量获取事件
  iframe?: false;
  dropOut?: Function; //退出登录
  defaultResource?: object; //默认权限
  resourceInfo?: resourceInfoModul; //权限提交配置
  strategyInfoApi?: string; // 获取系统密码策略
  themeConfig?: object;
  initThemeCoinfig?: boolean;
}
interface loginData {
  userName: string;
  password: string;
}
interface resourceInfoModul {
  application: object;
  token: string;
  api: string;
  dealReosurceList?: Function;
}

const { httpRequest } = useApi();
const requestConfig: HttpRequestConfig = { method: 'POST' };

export const appConfigStore = defineStore({
  id: 'appConfig',
  state(): AppConfig {
    return {
      projectType: 'web',
      baseApi: '/api',
      timeout: 15 * 1000,
      pagePermission: true,
      actionPermission: true,
      userLoginApi: '',
      siderPosition: 'top',
      userResourceApi: '',
      userInfoApi: '',
      resourceName: '',
      showProject: false,
      useHistoryTag: false,
      enablePermissions: false,
      updatePassWordInfo: {},
      dropOut: undefined,
      iframe: false,
      userCustomRouterGuard: undefined,
      defaultResource: undefined,
      headerBellInfo: {
        isShow: false,
        api: '',
        toRouterName: '',
      },
      themeConfig: {},
      initThemeCoinfig: false,
      resourceInfo: {
        application: {},
        api: '',
        token: '',
      },
    };
  },
  getters: {
    getThemeConfig: (state: any) => state.themeConfig,
    getHeaderBellInfo: (state: any) => state.headerBellInfo,
    getInitThemeCoinfig: (state: any) => state.initThemeCoinfig,
  },
  actions: {
    setInitThemeCoinfig(val: boolean) {
      this.initThemeCoinfig = val;
    },
    setUserInfo(val: any) {
      this.userBasicInfo = val;
    },
    setThemeConfig(val: any) {
      this.themeConfig = val;
    },
    setConfig(config: AppConfig) {
      Object.keys(config).forEach((key) => {
        this[key] = config[key];
      });
    },
    initDefaultResource(data: any) {
      this.defaultResource = data;
    },
    setParams(config: Object) {
      Object.keys(config).forEach((key) => {
        this[key] = config[key];
      });
    },
    userLogin(data: loginData) {
      if (this.userLoginApi) {
        console.log(data);
        return httpRequest({ api: this.userLoginApi, params: data, pathParams: {}, requestConfig });

        // return http.post(this.userLoginApi, data);
      } else {
        return null;
      }
    },
    userInfo() {
      if (this.userInfoApi) {
        return httpRequest({ api: this.userInfoApi, params: {}, pathParams: {}, requestConfig });
      } else {
        return { data: {} };
      }
    },
    userResource(params = {}) {
      if (this.userResourceApi) {
        return httpRequest({
          api: this.userResourceApi,
          params,
          pathParams: {},
          requestConfig,
        });
      } else {
        return { data: [] };
      }
    },
  },
});
