let modules = {};
import commonMixin from "../../../mixins/mobile/common/index";

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: {}
    }
  },
  methods: {
    initSetting(options) {
      this.options = options;
      if (options.pageSize) {
        this.pageSize = options.pageSize;
        this.page.size = options.pageSize;
      }
    },
    searchEvent() {
      this.loadData();
    },
    resetEvent() {
      this.resetFormData();
      this.loadData();
    },
    resetFormData() {
      this.formData = {};
    },
    loadData(config) {
      let current = config?.current || 1
      let size = config?.size || this.pageSize;
      this.checkList = [];
      this.checkAll = false;
      let defaultPage = {
        current: 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 => {
          let page = res.objx ? res.objx : defaultPage;
          if (config?.more) {
            let records = [...this.page.records, ...page.records];
            page.records = records;
          }
          this.page = page;
        }
      });
    },
    changePageNew({current, size}) {
      this.loadData({
        current: current,
        size: size,
        more: true
      });
    },
    openEditDialog(id) {
      this.copyId = 0;
      this.dataId = !id || typeof id == 'object' ? 0 : id;
      this.activeName = 'first';
      this.showViewDialog = true;
      this.$openEditView('showEdit');
    },
    closeViewDialog() {
      this.showEdit = false;
      this.showViewDialog = false;
    },
    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;
