const _Http = getApp().globalData.http,
    currency = require("../../../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
Page({
    data: {
        list: [],
        content: {
            nocache: true,
            "sa_salesforecastprojectid": 0,
            "sa_salesforecastbillid": 0,
            "pageSize": 20,
            "where": {
                "condition": ""
            }
        },
        disabled: false
    },
    onLoad(options) {
        let {
            periodend,
            periodstart,
            sa_salesforecastbillid
        } = getCurrentPages()[getCurrentPages().length - 2].data.detail;
        this.setData({
            "content.sa_salesforecastprojectid": options.id,
            sa_orderid: options.sa_orderid,
            disabled: options.disabled == 'true',
            "content.sa_salesforecastbillid": sa_salesforecastbillid,
            periodend,
            periodstart,
            unoutamount: options.unoutamount
        });
        this.getList(true)
        getApp().globalData.Language.getLanguagePackage(this, '订单明细');
    },
    getList(init = false) {
        let content = this.data.content;
        if (init.detail != undefined) init = init.detail;
        if (init) {
            content.pageNumber = 1;
            content.pageTotal = 1;
        }
        if (content.pageNumber > content.pageTotal) return;
        _Http.basic({
            "id": 20230705145004,
            content
        }).then(res => {
            console.log("订单行列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            this.selectComponent("#ListBox").setHeight(".total", this);
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            content.pageNumber = res.pageNumber + 1;
            content.pageTotal = res.pageTotal;
            let list = res.data.map(v => {
                v.showmarketprice = CNY(v.marketprice)
                v.showdefaultprice = CNY(v.defaultprice)
                v.showdefaultamount = CNY(v.defaultamount)
                v.showprice = CNY(v.price)
                v.showamount = CNY(v.amount)
                return v
            })
            this.setData({
                list: res.pageNumber == 1 ? list : this.data.list.concat(list)
            })
        })
    },
    startSearch({
        detail
    }) {
        this.data.content.where.condition = detail;
        this.getList(true)
    },
    onClear() {
        this.data.content.where.condition = '';
        this.getList(true)
    },
    toAdd() {
        getApp().globalData.handleSelect = this.onInsert.bind(this);
        wx.navigateTo({
            url: `/packageA/invoiceforecast/modules/orderForm/select?ids=${
                JSON.stringify({
                    sa_salesforecastprojectid:this.data.content.sa_salesforecastprojectid,
                    sa_orderid:this.data.sa_orderid
                })
            }`,
        })
    },
    updated() {
        let content = JSON.parse(JSON.stringify(this.data.content));
        content.pageSize = (content.pageNumber - 1) * content.pageSize;
        content.pageNumber = 1;
        _Http.basic({
            "id": 20230705145004,
            content
        }).then(res => {
            console.log("订单行列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            this.selectComponent("#ListBox").setHeight(".total", this);
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            let list = res.data.map(v => {
                v.showmarketprice = CNY(v.marketprice)
                v.showdefaultprice = CNY(v.defaultprice)
                v.showdefaultamount = CNY(v.defaultamount)
                v.showprice = CNY(v.price)
                v.showamount = CNY(v.amount)
                return v
            })
            this.setData({
                list
            })
        })
    },
    onInsert(list) {
        let that = this;
        wx.showModal({
            title: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.joint([{
                t: 1,
                v: '是否确认添加',
                r: " "
            }, {
                v: list.length,
                r: " "
            }, {
                t: 1,
                v: '件产品',
                r: "?"
            }]),
            cancelText: getApp().globalData.Language.getMapText('取消'),
            confirmText: getApp().globalData.Language.getMapText('确定'),
            complete: ({
                confirm
            }) => {
                if (confirm) that.handleChange(list).then(res => {
                    if (res.code != '1') return;
                    this.updated();
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 800)
                })
            }
        })
    },
    handleChange(list) {
        return new Promise((resolve, reject) => {
            _Http.basic({
                "id": 20230705145204,
                "content": {
                    "sa_salesforecastbillid": this.data.content.sa_salesforecastbillid,
                    "sa_salesforecastprojectid": this.data.content.sa_salesforecastprojectid,
                    "salesforecast": list.map(v => {
                        return {
                            "sa_salesforecastid": v.sa_salesforecastid || 0,
                            "sa_orderitemsid": v.sa_orderitemsid,
                            "deliverydate": v.plandeliverydate || v.deliverydate || ''
                        }
                    })
                },
            }).then(res => {
                console.log("设置产品", res)
                resolve(res)
                if (res.code != '1') wx.showToast({
                    title: res.msg,
                    icon: "none",
                    mask: true
                });
            })
        })
    },
    pickerOnChange(e) {
        let value = e.detail.value,
            index = e.currentTarget.dataset.index;
        if (this.data.list[index].deliverydate == value) return;
        this.data.list[index].plandeliverydate = value;
        this.handleChange([this.data.list[index]]).then(res => {
            if (res.code == '1') {
                getApp().globalData.Language.showToast('设置成功')
                this.setData({
                    [`list[${index}].plandeliverydate`]: value
                })
            }
        })
    },
    deleteItem({
        detail
    }) {
        let that = this;
        wx.showModal({
            title: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.getMapText('是否确定删除') + `${detail.itemname}`,
            cancelText: getApp().globalData.Language.getMapText('取消'),
            confirmText: getApp().globalData.Language.getMapText('确定'),
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20230705145404,
                    "content": {
                        "sa_salesforecastprojectid": this.data.content.sa_salesforecastprojectid,
                        "sa_salesforecastbillid": that.data.content.sa_salesforecastbillid,
                        "sa_salesforecastids": [detail.sa_salesforecastid]
                    },
                }).then(res => {
                    console.log("删除订单行", res)
                    wx.showToast({
                        title: res.code == '1' ? getApp().globalData.Language.getMapText('删除成功') : res.msg,
                        icon: "none"
                    })
                    if (res.code == '1') that.updated();
                })
            }
        })
    },
    onUnload() {
        getCurrentPages()[getCurrentPages().length - 2].partialRenewal(true)
    }
})