import { defineStore } from 'pinia';

export const subTableStore = defineStore({
  id: 'subTable',
  state: () => ({
    tableDta: [],
    groupData: {},
    oldDefaultGroupData: {},
    toSetGroupData: false,
    instanceInfo: {},
  }),
  getters: {
    getTableData(state) {
      return state.tableDta;
    },
    getGroupData(state) {
      return state.groupData;
    },
    getOldDefaultGroupData(state) {
      return state.oldDefaultGroupData;
    },
    getToSetGroupData(state) {
      return state.toSetGroupData;
    },
    getInstance(state) {
      return state.instanceInfo;
    },
  },
  actions: {
    setTableData(data) {
      this.tableDta = data;
    },
    setToSetGroupData() {
      this.toSetGroupData = true;
    },
    setSingleData(parentFileId, data) {
      this.tableDta.forEach((item, i) => {
        if (item.parentFileId == parentFileId) {
          item.child = data;
        }
      });
    },
    setGroupData(data) {
      for (const key2 in data) {
        if (data[key2] != undefined) {
          this.groupData[key2] = data[key2];
        }
      }
    },
    setOldDefaultGroupData(data) {
      for (const key2 in data) {
        if (data[key2] != undefined) {
          this.oldDefaultGroupData[key2] = data[key2];
        }
      }
    },
    setOneGroupData(key, data) {
      this.groupData[key] = data;
    },
    clearGoupData() {
      this.groupData = {};
      this.toSetGroupData = false;
    },
    clearGroupDataKeyList(keyList) {
      keyList.forEach((key) => {
        this.groupData[key] = '';
      });
    },
    setInstance(data) {
      this.instanceInfo = data;
    },
  },
});
