const _Http = getApp().globalData.http;
Page({
    data: {
        result: [],
        id: 20221124140102,
        "content": {
            nocache: true,
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            }
        },
    },
    onSearch({
        detail
    }) {
        this.data.content.where.condition = detail;
        this.getList(true);
    },
    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();
        getApp().globalData.Language.getLanguagePackage(this, '选择类别');
    },
    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.code != '1') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            res.data = res.data.map(v => {
                let arr = [{
                    v: '编号',
                    r: '：',
                    t: 1
                }, {
                    v: v.itemclassnum || ' --',
                    t: 1,
                    r: ' ',
                }]

                if (v.brandname) {
                    arr = arr.concat([{
                        v: '品牌',
                        r: '：',
                        t: 1
                    }, {
                        v: v.brandname || ' --',
                        t: 1,
                        r: ' ',
                    }, {
                        v: '默认折扣',
                        r: '：',
                        t: 1
                    }, {
                        v: ((v.discountrate || 1) * 100) + '%'
                    }])
                }
                v.introduce = getApp().globalData.Language.joint(arr)

                v.itemclassfullname = getApp().globalData.Language.getMapText(v.itemclassfullname)
                return v
            })
            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();
    },
})