/**version-1.0*/
import {
  treeScolly
} from '../../utils/global.js';

let tMixins = {};
let configUtil = {
  treeScolly
};
tMixins = {
  props: ['beforeConfirm', 'beforeCancel', 'beforeClose', 'labelKey'],
  data() {
    return {
      checkRows: [],
      fieldKey: 'id',
      defaultTableRefName: "table-m1"
    }
  },
  methods: {
    getCheckLabel(row,key){
      return row[this.labelKey || key];
    },
    initSetting(opt) {
      if (this.multi != null) {
        if (this.multi === false || this.multi == 'false') {
          this.selectMulti = false;
        } else if (this.multi === true || this.multi == 'true') {
          this.selectMulti = true;
        }
      }

      if (this.$attrs.fieldKey) {
        this.fieldKey = this.$attrs.fieldKey;
      }

      if (this.$attrs.tableRef) {
        this.defaultTableRefName = this.$attrs.tableRef;
      }
      let checkRows;
      if (this.selectMulti) {
        checkRows = this.rows || [];
      } else {
        checkRows = (this.rows && this.rows.length > 0) ? [this.rows[0]] : [];
      }
      this.checkRows = this.$baseLodash.cloneDeep(checkRows);
      if (!opt || opt.treeScolly !== false) {
        setTimeout(() => {
          configUtil.treeScolly({
            target: this,
            type: "selectDialog"
          });
        }, 200);
      }
    },
    addDataTable(obj) {
      let fieldKey = this.fieldKey;
      let selectMulti = this.selectMulti;
      let row = obj.row;
      let that = this;
      let $table1 = obj.$table;

      if (selectMulti) {
        let rows = this.checkRows;
        if (obj.checked) {
          let flag = rows.some(item => {
            return row[fieldKey] == item[fieldKey];
          });
          if (!flag) {
            rows.push(obj.row);
          }
        } else {
          let rowid = obj.row[fieldKey];
          let delIndex;
          for (var i = 0; i < rows.length; i++) {
            if (rows[i][fieldKey] == rowid) {
              delIndex = i;
              break;
            }
          }
          if (delIndex != null) rows.splice(delIndex, 1);
        }
      } else {
        $table1.clearCheckboxRow();
        $table1.setCheckboxRow(obj.row, true);
        this.checkRows = [];
        this.checkRows.push(obj.row);
      }

      let checkStrictly = $table1.$options.propsData?.checkboxConfig?.checkStrictly;
      if(checkStrictly){
        if (obj.checked) {

          // let ay = $table1.getTableData().fullData;
          // let aRows = this.findAllRows(ay);
          let aRows = $table1.getTableData().visibleData;

          let cRows = $table1.getCheckboxRecords();
          if (aRows.length == cRows.length) {
            $table1.setAllCheckboxRow(true);
          } else {
            $table1.setAllCheckboxRow(false);
          }
        } else {
          $table1.setAllCheckboxRow(false);
        }
      }

    },
    clearTable1Select(index) {
      let fieldKey = this.fieldKey;
      let selectMulti = this.selectMulti;
      let row = this.checkRows[index];
      var rowid = row[fieldKey];
      let $grid1 = this.$refs[this.defaultTableRefName];

      // let rows = $grid1.getTableData().fullData;
      let rows = $grid1.getTableData().visibleData;
      let row2 = this.findTreeRowById(rows, rowid);
      if (selectMulti) {
        if (row2) $grid1.setCheckboxRow(row2, false);
      } else {
        $grid1.clearCheckboxRow();
      }
      this.checkRows.splice(index, 1);
      let checkStrictly = $grid1.$options.propsData?.checkboxConfig?.checkStrictly;
      if(checkStrictly){
        if (row2) $grid1.setAllCheckboxRow(false);
      }
    },
    checkAll(res) {
      let fieldKey = this.fieldKey;
      var $grid = res.$grid;
      // let tableData = $grid.getTableData().fullData;
      // let allRows = this.findAllRows(tableData);
      let tableData = $grid.getTableData().visibleData;
      let allRows = $grid.getTableData().visibleData;
      $grid.setCheckboxRow(allRows, res.checked);
      allRows.forEach(lineData => {
        let rowid = lineData[fieldKey];
        let tableData2 = this.checkRows;
        let row2;
        for (var i = 0; i < tableData2.length; i++) {
          let lineData2 = tableData2[i];
          if (lineData2[fieldKey] == rowid) {
            row2 = tableData2[i];
          }
        }
        if (res.checked) {
          if (!row2) {
            this.checkRows.push(lineData);
          }
        } else {
          if (row2) {
            let delIndex;
            let checkRows = this.checkRows;
            for (var i = 0; i < checkRows.length; i++) {
              if (checkRows[i][fieldKey] == rowid) {
                delIndex = i;
                break;
              }
            }
            if (delIndex != null) checkRows.splice(delIndex, 1);
          }
        }
      })
    },
    clearChecked() {
      let $grid1 = this.$refs[this.defaultTableRefName];
      $grid1.clearCheckboxRow();
      this.checkRows = [];
    },
    _clearChecked() {
      this.clearChecked();
    },
    findTreeRowById(rows, rowid) {
      let fieldKey = this.fieldKey;
      for (let i = 0; i < rows.length; i++) {
        let row = rows[i];
        if (row[fieldKey] == rowid) {
          return row;
        }
        if (row.children) {
          let mRow = this.findTreeRowById(row.children, rowid);
          if (mRow) {
            return mRow;
          }
        }
      }
      return null;
    },
    findAllRows(rows) {
      let newRows = [];
      for (let i = 0; i < rows.length; i++) {
        let row = rows[i];
        newRows.push(row);
        if (row.children) {
          let cRows = this.findAllRows(row.children);
          if (cRows) {
            newRows = newRows.concat(cRows);
          }
        }
      }
      return newRows;
    },
    searchEvent(option) {
      let target = this.defaultTableRefName;
      let formKey = "formData";
      if (option && !option.preventDefault) {
        if (option.target) {
          target = option.target;
        }
        if (option.preHandle) {
          option.preHandle();
        }
      }
      this.$refs[target].commitProxy('reload');
    },
    resetEvent(option) {
      let target = this.defaultTableRefName;
      let formKey = "formData";
      if (option && !option.preventDefault) {
        if (option.target) {
          target = option.target;
        }
        if (option.preHandle) {
          option.preHandle();
        } else {
          if (option.formKey) {
            formKey = option.formKey
          }
          this[formKey] = {};
        }

      }
      this.$refs[target].commitProxy('reload');
    },
    dialogClose() {
      this.showDialog = false;
      let rows = this.checkRows;
      this.$emit('close', rows);
      this.$emit('update:visiable', false);
    },
    dialogPrimary() {
      let rows = this.checkRows;
      if (this.beforeConfirm && this.beforeConfirm(rows, this.dialogClose) === false) {
        return false;
      }
      this.$emit('confirm', rows);
      this.dialogClose();
    },
    handleBeforeClose(hide) {
      this.dialogCancel();
    },
    dialogCancel() {
      let rows = this.checkRows;
      if (this.beforeCancel && this.beforeCancel(rows, this.dialogClose) === false) {
        return false;
      }
      this.$emit('cancel', rows);
      this.dialogClose();
    },
    checkWithSubmit(obj) {
      if (!this.selectMulti) {
        let $table = obj.$table;
        var rows = $table.getCheckboxRecords(true);
        if (rows && rows.length) {
          this.dialogPrimary();
        }
      }
    }
  }
};
export const selectDialogMixins = tMixins;
