/**version-1.0*/
let mixins = {};
mixins = {
  components: {},
  data() {
    return {}
  },
  mounted() {

  },
  methods: {
    editRowEvent(obj) {
      if (this.hasEditingRow(obj.$table, 1)) {
        return false;
      }
      let row = obj.row;
      obj.$table.editCloneRow = this.$baseLodash.cloneDeep(obj.row);
      obj.$table.setActiveRow(row)
    },
    saveRowEvent(obj) {
      let that = this;
      let $grid = obj.$table.$xegrid;
      let done = () => {
        this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
          this._saveData(obj);
        });
      }
      let originOption = $grid.originOption;
      $grid.clearActived().then(() => {
        $grid.setActiveRow(obj.row);
        if (originOption.editValidate) {
          originOption.editValidate(obj.row, done)
        } else {
          done();
        }
      })
    },
    _saveData(obj) {
      let that = this;
      let $grid = obj.$table.$xegrid;
      let originOption = $grid.originOption;
      let formData = commonDataUtil.handleForm(obj.row);
      that.$http({
        url: originOption.editSaveUrl || '',
        method: 'post',
        data: formData,
        isLoading: true,
        success: res => {
          that.$message({
            type: 'success',
            message: '保存成功!'
          });
          if (obj.row.id == res.objx.id) {
            $grid.clearActived().then(() => {
              Object.assign(obj.row, res.objx);
            });
          } else {
            $grid.remove(obj.row);
            $grid.insertAt(res.objx);
          }
          delete obj.$table.editCloneRow
        }
      })
    },
    findAllRows(rows,grid) {
      let newRows = [];
      let childrenField = grid.treeConfig?.childrenField;
      if (!childrenField) {
        return rows;
      }
      for (let i = 0; i < rows.length; i++) {
        let row = rows[i];
        newRows.push(row);
        if (row[childrenField]) {
          let cRows = this.findAllRows(row[childrenField],grid);
          if (cRows) {
            newRows = newRows.concat(cRows);
          }
        }
      }
      return newRows;
    },
    hasEditingRow($grid, flag) {

      let obj = $grid.getTableData();
      let fullData = obj.fullData;
      let tableData = obj.tableData;

      if (fullData.length > 0 && flag !== 1) {
        let rows = this.findAllRows(fullData,$grid);
        for (let i = 0; i < rows.length; i++) {
          let row = rows[i];
          if (!this.hasSaveRow(row)) {
            return true;
          }
        }
      } else if (tableData.length > 0) {
        let rows = this.findAllRows(tableData,$grid);
        for (let i = 0; i < rows.length; i++) {
          let row = rows[i];
          if (!this.hasSaveRow(row)) {
            return true;
          }
        }
      }
    },
    cancleRowEvent(obj) {
      if (this.hasEditingRow(obj.$table, 1)) {
        return false;
      }
      let row = obj.row;
      let $table = obj.$table;
      /*let parentField = $table.treeConfig.parentField;
      if (!row[parentField]) {
          $table.clearActived()
              .then(() => {
                  // $grid.revertData(obj.$table.editCloneRow);
              })
      } else {
          $table.clearActived()
              .then(() => {
                  Object.assign(row, $table.editCloneRow)
              });
      }*/
      $table.clearActived().then(() => {
        Object.assign(row, $table.editCloneRow)
        delete obj.$table.editCloneRow
      });

    },
    removeRowEvent(obj) {
      const $grid = obj.$table;
      $grid.remove(obj.row);
    },
    insertTopRow($grid) {
      if (this.hasEditingRow($grid, 1)) {
        return false;
      }
      let parentField = $grid.treeConfig.parentField;
      let newRow = {};
      let originOption = $grid.originOption;
      let editDefaultRow;
      if (originOption.editDefaultRow) {
        if (typeof originOption.editDefaultRow == "function") {
          editDefaultRow = originOption.editDefaultRow({}) || {};
        } else {
          editDefaultRow = originOption.editDefaultRow || {};
        }
      }
      Object.assign(newRow, editDefaultRow);
      newRow.id = "row_" + (new Date()).valueOf() + Math.floor(Math.random() * 1000000);
      newRow[parentField] = '';

      $grid.insert(newRow).then(({
                                   row
                                 }) => $grid.setActiveRow(row));
    },
    insertChildEvent(obj) {
      if (this.hasEditingRow(obj.$table, 1)) {
        return false;
      }
      let $grid = obj.$table.$xegrid;
      let parentField = $grid.treeConfig.parentField;
      let currRow = obj.row;

      let newRow = {};
      let originOption = $grid.originOption;
      let editDefaultRow;
      if (originOption.editDefaultRow) {
        if (typeof originOption.editDefaultRow == "function") {
          editDefaultRow = originOption.editDefaultRow(obj) || {};
        } else {
          editDefaultRow = originOption.editDefaultRow || {};
        }
      }
      Object.assign(newRow, editDefaultRow);
      newRow.id = "row_" + (new Date()).valueOf() + Math.floor(Math.random() * 1000000);
      newRow[parentField] = currRow.id;
      $grid.setTreeExpand(obj.row, true).then(() => {
        $grid.insertAt(newRow).then(({
                                       row
                                     }) => {
          $grid.setTreeExpand(obj.row, true).then(() => {
            $grid.setActiveRow(row);
          });
        });
      });
    },
    hasSaveRow(row) {
      if (!row || !row.id || row.id.toString().indexOf("row_") == 0) {
        return false;
      } else {
        return true;
      }
    }
  }
}

export const tableTreeMixins = mixins;
