/**
 * @Author: 仲春阳
 * @Date: 2023/14/04 14:48:09
 * @LastEditors: 仲春阳
 * @Description: 表格列表方法封装混入
 **/
import request from '@/utils/request'

export default {
    data() {
        return {
            // 公共区域的高度 包含分页器  若有偏差请自行计算
            publicHeight: 0,
            tableHeight: '',
            store: {}
        }
    },
    mounted() {
        // 筛选框的高度
        let selectFormHeight = document.getElementById('select-form') || {}
        this.publicHeight = this.publicHeight + selectFormHeight.offsetHeight || 0
        this.tableHeight = window.sessionStorage.getItem('appheight') - 110
    },
    methods: {
        handleSizeChange(e) {
            this.store.params.pageSize = e
            this.store.params.pageNum = 1
            this.store.search()
        },
        handleCurrentChange(e) {
            this.store.params.pageNum = e

            this.store.search()
        },
        dataSource(url) {
            return {
                url,
                data: [],
                loading: true,
                total: 0,
                params: {
                    pageNum: 1,
                    currentPage: 1,
                    pageSize: 20,
                    searchBeginTime: '',
                    searchEndTime: '',
                    searchState: '',
                    searchValue: ''
                },
                loadCallback: null,
                search: () => {
                    this.currentPage = this.pageNum
                    request.post(this.store.url, this.store.params).then(res => {
                        this.store.data = res.data.records
                        this.store.total = res.data.total
                        this.store.loading = false
                        this.store.loadCallback && this.store.loadCallback()
                        this.$nextTick(() => {
                            this.$refs.table && this.$refs.table.doLayout && this.$refs.table.doLayout()
                        })
                    })
                },
                reload: () => {
                    this.store.params.pageNum = 1
                    this.store.search()
                },
                export: call => {
                    let params = JSON.parse(JSON.stringify(this.store.params))
                    params.pageNum = 1
                    params.pageSize = this.store.total
                    request.post(this.store.url, params).then(res => {
                        call && call(res.data.records)
                    })
                }
            }
        }
    }
}
