import { http } from '/nerv-lib/saas';
import { permission } from '/@/api/origanizemanage';
import { appConfigStore } from '/nerv-lib/saas/store/modules/app-config';
import { authorizationService } from '/nerv-base/store/modules/authorization-service';
import { get } from 'lodash-es';
import { computed, ref, toRef } from 'vue';

const ORGID = sessionStorage.getItem('ORGID') ? Number(sessionStorage.getItem('ORGID')) : '';
const isAdmin = sessionStorage.getItem('ISADMIN')
  ? Boolean(sessionStorage.getItem('ISADMIN'))
  : false;
const selectDefaultValue = ref(ORGID);
const selectDefaultDisabled = ref(isAdmin);
const transform = (data, map) => {
  return Object.keys(map).reduce((pre, cur) => {
    pre[cur] = data[map[cur]];
    return pre;
  }, {});
};

export const appConfig = {
  projectType: 'web',
  baseApi: '/api',
  projectName: '济阳站_AI智能BAS系统',
  enablePermissions: false,
  // themeColor: '#eee',
  siderPosition: 'left',
  baseHeader: '/parkingManage',
  baseRouter: '/parkingManage/parkingLotManage',
  themeConfig: {
    bgImageUrl: `${import.meta.env.VITE_PUBLIC_PATH}/asset/image/login/background.png`,
    logoLessUrl: `${import.meta.env.VITE_PUBLIC_PATH}/asset/image/headerIcon.png`,
    logoUrl: `${import.meta.env.VITE_PUBLIC_PATH}/asset/image/headerIcon.png`,
  },
  // userCustomRouterGuard: (to, from, next, whiteNameList, authorizationStore, appConfig) => {
  //   console.log({ to, from, next, whiteNameList, authorizationStore, appConfig }, 'routeConfig');
  //   next();
  // },

  // customUpdatePwd: () => import('/@/view/updatePassword/updatePassword.vue'),

  headerSlotConfig: {
    component: 'NsSelectApi',
    componentProps: {
      style: { width: '200px', color: '#2778FF', marginRight: '10px' },
      api: '/carbon-smart/user/login/logInInfo',
      size: 'large',
      defaultValue: selectDefaultValue,
      disabled: selectDefaultDisabled,
      // autoSelectFirst: true,
      placeholder: '请选择',
      onSelect: async (cur, record) => {
        console.log(cur, record);
        const configStore = appConfigStore();
        const useAuthorization = authorizationService();
        sessionStorage.setItem('ORGID', record.orgId);
        const res = await configStore.userResource({ data: record });
        useAuthorization.updateUserResource(res.data);
        window.location.reload();
        // value.value = cur;
      },
      resultField: 'data.linkList',
      labelField: 'orgName',
      valueField: 'orgId',
      immediate: true,
      dropdownReload: true,
    },
  },
  timeout: 60 * 1000,
  userLoginApi: (params) => {
    return http.post('/carbon-smart/user/login', { ...params });
  },
  userResourceApi: (params) => {
    const { orgId } = get(params, 'data') || { orgId: '' };
    const finalId = orgId || ORGID;
    // 解决初始化登录select无初始值的问题
    selectDefaultValue.value = finalId;

    const userInfo = JSON.parse(sessionStorage.getItem(import.meta.env.VITE_PUBLIC_PATH)!);
    const ownOrgInfo = userInfo.linkList?.filter(({ orgId: id }) => id === finalId)[0];

    return http.post('/carbon-smart/user/login/logInPermission', ownOrgInfo).then((res) => {
      return res;
    });
  },
  userInfoApi: () => {
    return http.post('/carbon-smart/user/login/logInInfo').then((res) => {
      const info = res.data;
      const trD = transform(info, {
        accountCode: 'userId',
        accountName: 'accountNo',
        accountRealName: 'accountNo',
        orgId: 'orgId',
        orgName: 'orgName',
        projectId: 'projectId',
        linkList: 'linkList',
        permissionVos: 'permissionVos',
        adminFlag: 'adminFlag',
      });
      sessionStorage.setItem('ORGID', info.orgId);
      sessionStorage.setItem('ISADMIN', trD?.adminFlag === '1');
      selectDefaultDisabled.value = info?.adminFlag === '1';
      selectDefaultValue.value = info.orgId;
      return { data: { ...trD } };
    });
  },

  useHistoryTag: false,
  // 修改密码配置
  updatePassWordInfo: {
    title: '修改密码',
    subtitle: 'huaxing平台',
    api: '/carbon-smart/api/user/update/password',
  },
  // headerBellInfo: {
  //   isShow: true,
  //   api: '/api/web/objs/bulletin/readCount',
  //   toRouterName: 'NoticeManageIndex',
  // },
  resourceInfo: {
    api: permission.add,
  },
};
