export default {
  methods: {
    getData() {
      return this.data;
    },
    setData(data, options) {
      this.data = data;
      this.store.states.changeDataSet = null;
      this.$nextTick(() => {
        this.initData();
        this.store.handleFilters();
        if (options && options.initScrollStatus) {
          this.store.setScrollStatus(0, 0);
        }
        if (data.length === 0) {
          this.store.setScrollStatus(0, 0);
        }
      });
    },
    // 获取编辑行sid
    getChangeSid() {
      const { states } = this.store;
      return states.editedSidSet;
    },
    // emit 编辑行
    emitEdited() {
      const { states } = this.store;
      setTimeout(() => {
        const chagneData = this.isChanged ? this.getChangeData() : [];
        this.$emit('editing', false, chagneData);
        this.isChanged = false;
        states.changeDataSet = JSON.parse(JSON.stringify(states.data));
      }, 100);
    },
    getChangeData() {
      return this.store.states.changeData;
    },
    getErrorRows() {
      const { states } = this.store;
      const errorRowsIndex = states.dataStatusList.map((item, index) => {
        if (item.errors.length > 0) {
          return index;
        }
        return null;
      });
      const errorRows = this.data.map((d, index) => {
        if (errorRowsIndex.includes(index)) {
          return {
            data: d,
            index,
          };
        }
        return null;
      }).filter((i) => i);
      return errorRows;
    },
    addItem(item, type = 'push', selectCellXY = { x: 0, y: 0 }) {
      if (this.$refs.fixedTheader.checkedCurrent) {
        this.$refs.fixedTheader.checkedCurrent = false;
        this.$refs.fixedTheader.selectCurrent();
      }
      if (this.$refs.fixedTheader.checkedAll) {
        this.$refs.fixedTheader.checkedAll = false;
        this.$refs.fixedTheader.selectAll();
      }
      const { states } = this.store;
      states.addRowType = type;
      if (type === 'push') {
        this.data.push(item);
        states.dataStatusList.push({
          checked: false,
          errors: [],
        });
      } else if (type === 'unshift') {
        this.data.unshift(item);
        states.dataStatusList.unshift({
          checked: false,
          errors: [],
        });
      }
      // 选中新增行的第一个单元格为编辑中
      if (selectCellXY) {
        this.selectCell({ button: 0 }, selectCellXY.x, selectCellXY.y);
      } else if (states.columns[0].type === 'selection') {
        this.selectCell({ button: 0 }, 1, 0);
      } else {
        this.selectCell({ button: 0 }, 0, 0);
      }
      setTimeout(() => {
        states.selector.selectedXArr = [0, states.columns.length];
        states.selector.selectedYArr = [0, 0];
        this.selectUp();
        this.setEditing();
      }, 100);
    },
    removeItems(key, values) {
      const { states } = this.store;
      if (key && values instanceof Array) {
        values.forEach((value) => {
          const dIndex = this.data.findIndex((d) => d[key] === value);
          states.dataStatusList.splice(dIndex, 1);
          this.data.splice(dIndex, 1);
        });
      }
    },
  },
};
