import { defineStore } from 'pinia'

const useDictStore = defineStore('dict', {
  state: () => ({
    dict: new Array(),
  }),
  actions: {
    // 获取字典
    getDict(_key) {
      if (_key == null && _key == '') {
        return null
      }
      try {
        for (let i = 0; i < this.dict.length; i++) {
          if (this.dict[i].groupname == _key) {
            return this.dict[i].list
          }
        }
      } catch (e) {
        return null
      }
    },
    // pinia中是否有缓存字典
    getAllDict() {
      return this.dict.length
    },
    // 设置字典
    setDict(value) {
      this.dict = value
    },
    // 清空字典
    cleanDict() {
      this.dict = new Array()
    },
    // 初始字典
    initDict() {
      this.dict = new Array()
    },
  },
})

export default useDictStore
