let modules = {};
import commonMixin from "../../../mixins/mobile/common/index";
import PopupManager from 'element-ui/src/utils/popup/popup-manager.js'

let pageSize = 50
modules = {
  mixins: [commonMixin],
  data() {
    return {
      formData: {},
      pageSize: pageSize,
      page: {
        current: 1,
        size: pageSize,
        total: 0,
        records: [],
      },
      checkList: [],
      checkAll: false,
      selectMulti: true,
      checkRows: [],
      fieldKey: 'id',
      popStatus: false,
      options: {},
      zIndex: PopupManager.nextZIndex()
    }
  },
  methods: {
    initSetting(options) {
      this.options = options;
      if (options.pageSize) {
        this.pageSize = options.pageSize;
        this.page.size = options.pageSize;
      }
      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;
        }
      }
      let checkRows;
      if (this.selectMulti) {
        checkRows = this.rows || [];
      } else {
        checkRows = (this.rows && this.rows.length > 0) ? [this.rows[0]] : [];
      }
      let fieldKey = this.fieldKey;
      this.checkList = checkRows.map(item => item[fieldKey]);
      this.checkRows = this.$baseLodash.cloneDeep(checkRows);
    },
    isChecked(row) {
      let fieldKey = this.fieldKey;
      return this.checkList.includes(row[fieldKey]);
    },
    searchEvent() {
      this.loadData();
    },
    resetEvent() {
      this.resetFormData();
      this.loadData();
    },
    resetFormData() {
      this.formData = {};
    },
    loadData(config) {
      let current = config?.current || 1
      let size = config?.size || this.pageSize;
      let defaultPage = {
        current: 1,
        pages: 1,
        size: this.pageSize,
        total: 0,
        records: [],
      };

      this.$http({
        url: this.options.url,
        method: `post`,
        data: {
          ...this.options.param(),
          current: current,
          size: size,
        },
        isLoading: true,
        success: res => {
          if (this.options.pageEnabled !== false) {
            let page = res.objx ? res.objx : defaultPage;
            if (config?.more) {
              let records = [...this.page.records, ...page.records];
              page.records = records;
            }
            this.page = page;
          } else {
            let records = res.objx || [];
            let page = {
              ...defaultPage,
              records: records,
              total: records.length
            }
            this.page = page;
          }

        }
      });
    },
    changePageNew({currentPage, pageSize}) {
      this.loadData({
        current: currentPage,
        size: pageSize,
        more: true
      });
    },
    dialogClose() {
      this.$emit('update:visiable', false);
    },
    dialogPrimary() {
      let rows = this.checkRows;
      this.$emit('confirm', rows);
      this.$emit('update:visiable', false);
    },
    changeCheck(row) {
      let fieldKey = this.fieldKey;
      let checkList = this.checkList;
      let checkRows = this.checkRows;
      let checked = !checkList.includes(row[fieldKey]);
      if (checked) {
        if (!this.selectMulti) {
          checkList.splice(0, checkList.length);
          checkRows.splice(0, checkRows.length);
        }
        if (!checkList.includes(row[fieldKey])) {
          checkList.push(row[fieldKey]);
          checkRows.push(row);
        }
      } else {
        if (checkList.includes(row[fieldKey])) {
          let checkIndex = checkList.indexOf(row[fieldKey]);
          checkList.splice(checkIndex, 1);
          checkRows.splice(checkIndex, 1);
        }
      }
    },
    confirmSearchDialog() {
      this.popStatus = false
      this.searchEvent();
    },
    resetSearchDialog() {
      this.resetFormData();
    },
    handleScroll(event) {
      const {scrollTop, clientHeight, scrollHeight} = event.target;
      let h = scrollHeight - (scrollTop + clientHeight);
      if (h < 0.6) {
        // 滚动条到达底部，触发懒加载
        this.loadMore();
      }
    },
    loadMore() {
      if (this.page.pages > this.page.current) {
        this.changePageNew({
          current: this.page.current + 1,
          size: this.page.size,
        })
      }
    }
  }
}
export default modules;
