import { getDict } from "@/utils/dictionary";
import { toDate } from "@/utils/date";
export default {
    data() {
        return {
            page: 1,
            total: 10,
            pageSize: 10,
            tableData: [],
            searchInfo: {
            }
        }
    },
    watch: {
        "searchInfo.id"(val) {
            if (val && val.length < 3) return;
            this.getTableData()
        },
        'searchInfo.stage'() {
            this.getTableData();
        },
        "searchInfo.supply"() {
            this.getTableData()
        },
        "searchInfo.type"() {
            this.getTableData()
        },
        "searchInfo.confirm"() {
            this.getTableData()
        },
        "searchInfo.self_employed"() {
            this.getTableData()
        },
        "searchInfo.keyword"(val) {
            let lastnum = val[val.length - 1]
            if (val.length == 1) return;
            if (lastnum <= "z" && lastnum >= "A") return;
            this.getTableData()
        },
        "searchInfo.fromDate"(val) {
            if (!val || searchInfo.toDate) return;
            if (val > this.searchInfo.toDate) return;
            this.getTableData()
        },
        "searchInfo.saleman"(val) {
            let lastnum = val[val.length - 1]
            if (val.length == 1) return;
            if (lastnum <= "z" && lastnum >= "A") return;
            this.getTableData()
        },
        "searchInfo.toDate"() {
            if (!val || searchInfo.fromDate) return;
            if (val < this.searchInfo.fromDate) return;
            this.getTableData()
        }
    },
    methods: {
        filterDict(value, type) {
            const rowLabel = this[type + "Options"] && this[type + "Options"].filter(item => item.value == value)
            return rowLabel && rowLabel[0] && rowLabel[0].label
        },
        async getDict(type) {
            const dicts = await getDict(type)
            this[type + "Options"] = dicts
            return dicts
        },
        handleSizeChange(val) {
            this.pageSize = val
            this.getTableData()
        },
        handlerUpdate(index, row) {
            this.tableData[index] = row;
        },
        handleCurrentChange(val) {
            this.page = val
            this.getTableData()
        },
        async getTableData(page = this.page, pageSize = this.pageSize) {
            let param = { page, pageSize, ...this.searchInfo }
            if( this.searchInfo.id) param.id = +this.searchInfo.id||0
            if(this.searchInfo.fromDate) param.fromDate = toDate(this.searchInfo.fromDate)
            if(this.searchInfo.toDate) param.toDate = toDate(this.searchInfo.toDate)
            const table = await this.listApi(param)
            if (table.code == 200) {
                this.tableData = table.data.list
                this.total = table.data.total
                this.page = table.data.page
                this.pageSize = table.data.pageSize
            }
        }
    }
}