const _Http = getApp().globalData.http;
Page({
    data: {
        result: [],
        id: 20221124140102,
        "content": {
            nocache: true,
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            }
        },
    },
    onLoad(options) {
        if (options.sa_contractid) this.setData({
            "content.sa_contractid": options.sa_contractid,
        })
        if (options.sa_quotedpriceid) this.setData({
            "content.sa_quotedpriceid": options.sa_quotedpriceid,
            id: 20230219161403
        })
        this.getList();
    },
    toggle(e) {
        const {
            itemclassid
        } = e.currentTarget.dataset.item;
        let result = this.data.result;
        let index = result.findIndex(v => v == itemclassid);
        index == -1 ? result.push(itemclassid + "") : result.splice(index, 1);
        this.setData({
            result
        })
    },
    getList(init = false) {
        let content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;
        _Http.basic({
            "id": this.data.id,
            content
        }).then(res => {
            console.log("可添加类别", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setData({
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                "paging.pageNumber": res.pageNumber + 1,
                "paging.pageTotal": res.pageTotal,
                "paging.total": res.total,
            })
        })
    },
    submit() {
        let page = getCurrentPages().find(v => v.__route__ == 'packageA/contract/detail' || v.__route__ == 'packageA/offers/detail');
        if (page) page.selectComponent("#PiscountType").handleAdd(this.data.result.map(v => {
            return {
                "sa_contract_itemsaleclassid": 0,
                "sa_quotedprice_itemclassid": 0,
                "itemclassid": v,
                "discountrate": this.data.list.find(s => s.itemclassid == v).discountrate
            }
        }))
    },
    onReachBottom() {
        this.getList();
    },
})