import { getTypeCode, getUserFormRole, getRegion } from '@/api/dictionary.js'
import { strFirstUpper, treeChildrenEmpty, treeLabelValue } from '@/utils/index.js'
import constantData from './constantData.js'

export default {
  state: {
    noConstant: ['regionList'],
    tree: [],
    noCache: [],
    params: ['roleUserList'],
    constant: {
      ...constantData, // 前端维护的常量数据
      regionList: [],
      roleUserList: [], // 角色下用户
      projectInitiationStatus: [],
      flowableTime: [],
      code: [],
      c_supplier_status: [],
      repaymentMethod: [], // 回款方式
      transferNoticeMethod: [], // 转让通知方式
      serviceScope: [], // 服务范围
      repaymentAdjustment: [], // 还款期限调整方式
      paymentMethodForInterestManagementFee: [], // 还款方式
      currencyType: [], // 币种
      incormeExpenditureType: [], // 收支类型
      idType: [], // 身份类别
      certType: [], // 责任人身份标识类型、配偶证件类型
      quotaDisburseArlpType: [], // 还款责任人类型
      collateralType: [], // 抵押物种类
      collateralIdType: [], // 抵押物识别号类型
      pledgeType: [], // 质押物种类
      invoice_status: [], // 发票管理列表状态
      invoiceType: [], // 发票类型
      amountKind: [], // 金额种类
      salesContractSettleCycleMethod: [], // 结算周期方式
      oaBillPaymentType: [], // 退款管理-付款类型
      principalPaymentMethod: [], // 授信决议 - 本金支付方式
      projectInitiationStatus: [], // 融资管理 - 业务状态
    }
  },
  mutations: {
    SEY_DICT(state, { type, data }) {
      state.constant[type] = data
    }
  },
  actions: {
    getConstant({ dispatch, state }, name) {
      const events = []
      const isConstant = name => {
        if (state.noConstant.includes(name)) {
          events.push(dispatch('get' + strFirstUpper(name), name))
        } else if (state.tree.includes(name)) {
          events.push(dispatch('getTreeDict', name))
        } else if (typeof (name) === 'object' && state.params.includes(name.type)) {
          events.push(dispatch('get' + strFirstUpper(name.type), name))
        } else {
          events.push(dispatch('getDict', name))
        }
      }
      if (Array.isArray(name)) {
        for (const item of name) {
          var type = typeof (item) === 'object' ? item.type : item
          if (!state.noCache.includes(type)) {
            const constantType = Object.prototype.toString.call(state.constant[type])
            if (constantType === '[object Array]') {
              if (state.constant[type]?.length) continue
            } else if (constantType === '[object Object]') {
              if (Object.keys(state.constant[type]).length) continue
            } else if (constantType === '[object String]') {
              if (state.constant[type]) continue
            }
          }
          isConstant(item)
        }
      } else {
        var typeStr = typeof (name) === 'object' ? name.type : name
        if (!state.noCache.includes(typeStr)) {
          if (!state.noCache.includes(typeStr)) {
            const typeStr = Object.prototype.toString.call(state.constant[typeStr])
            if (typeStr === '[object Array]') {
              if (state.constant[typeStr].length) return
            } else if (typeStr === '[object Object]') {
              if (Object.keys(state.constant[typeStr]).length) return
            } else if (typeStr === '[object String]') {
              if (state.constant[typeStr]) return
            }
          }
        }
        isConstant(name)
      }
      return Promise.all(events)
    },
    getDict({ commit }, type) {
      return new Promise((resolve, reject) => {
        getTypeCode({ typeCode: type }).then(({ data }) => {
          commit('SEY_DICT', { type, data })
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    getRoleUserList({ commit }, params) {
      return new Promise((resolve, reject) => {
        getUserFormRole(params.query).then(({ data }) => {
          commit('SEY_DICT', { type: params.type, data })
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    getRegionList({ commit }, type) {
      return new Promise((resolve, reject) => {
        getRegion().then(({ data }) => {
          const list = treeChildrenEmpty(data)
          treeLabelValue(list, { value: 'deptCode', label: 'name' })
          commit('SEY_DICT', { type, data: list })
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}
