const _Http = getApp().globalData.http,
    file = require("../../../../utils/FormatTheAttachment");
Component({
    properties: {
        disabled: {
            type: Boolean
        }
    },
    data: {
        sa_invoiceapplyid: 0,
        "content": {
            nocache: true,
            "pageNumber": 1,
            pageTotal: 1,
            total: null
        }
    },
    methods: {
        /* 获取产品列表 */
        getList(id, init) {
            let content = this.data.content;
            content.sa_invoiceapplyid = id;
            if (init) content.pageNumber = 1;
            _Http.basic({
                "id": "20221217091303",
                "version": 1,
                content
            }).then(res => {
                console.log("订单行列表", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
                res.data = res.data.map(value => {
                    if (value.attinfos.length != 0) {
                        value.attinfos = file.fileList(value.attinfos)
                        let image = value.attinfos.find(v => v.fileType == "image");
                        value.cover = image ? image.cover : "";
                    }
                    return value;
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    "content.pageNumber": res.pageNumber + 1,
                    "content.pageSize": res.pageSize,
                    "content.pageTotal": res.pageTotal,
                    "content.total": res.total,
                    sa_invoiceapplyid: id
                })
            })
        },
        /* 去添加订单行 */
        addProduct() {
            let detail = getCurrentPages().find(v => v.__route__ == 'packageA/invoice/detail').data.detail;
            wx.navigateTo({
                url: `/select/orderFormLine/index?params=${JSON.stringify({
          "id": "20221217085103",
          "version":1,
          "content": {
              nocache:true,
              "sys_enterpriseid":detail.sys_enterpriseid,
              "sa_invoiceapplyid":detail.sa_invoiceapplyid,
              "where":{
                "condition":"",
                "sonum":"",
                "begindate":"",
                "enddate":""
              }
          }
        })}`
            });
            getApp().globalData.handleSelect = this.handleSelect.bind(this);
        },
        /* 处理新增订单行 */
        handleSelect(detail) {
            let that = this;
            wx.showModal({
                title: '提示',
                content: `是否确认添加${detail.result.length}条订单行?`,
                complete: (res) => {
                    if (res.confirm) _Http.basic({
                        "id": "20221217091203",
                        "version": 1,
                        "content": {
                            "sa_invoiceapplyid": that.data.sa_invoiceapplyid,
                            "iteminfos": detail.list.map(v => {
                                return {
                                    "sa_invoiceapply_orderid": 0,
                                    "saorderid": v.sa_orderid,
                                    "sa_orderitemsid": v.sa_orderitemsid,
                                    "price": v.price
                                }
                            })
                        }
                    }).then(s => {
                        console.log('新增订单行', s)
                        wx.showToast({
                            title: s.msg == '成功' ? '添加成功' : s.msg,
                            icon: "none"
                        });
                        if (s.msg == '成功') setTimeout(() => {
                            that.getList(that.data.sa_invoiceapplyid, true);
                            wx.navigateBack();
                            // that.updateThePrice();
                        }, 300)
                    })
                }
            });
        },
        /* 修改开票金额数量 */
        changeProduct({
            detail
        }) {
            let invoiceaqty = detail.invoiceaqty;
            detail.invoiceaqty = 0;
            _Http.basic({
                "id": "20221217091203",
                "version": 1,
                "content": {
                    "sa_invoiceapplyid": this.data.sa_invoiceapplyid,
                    "iteminfos": [detail]
                }
            }).then(res => {
                console.log("修改订单行", res)
                let i = this.data.list.findIndex(v => v.sa_orderitemsid == detail.sa_orderitemsid)
                if (res.msg != '成功') {
                    wx.showToast({
                        title: res.msg,
                        icon: "none"
                    });
                    if (i != -1) this.setData({
                        [`list[${i}].invoiceamount`]: this.data.list[i].invoiceamount,
                        [`list[${i}].invoiceaqty`]: this.data.list[i].invoiceaqty
                    })
                } else {
                    if (i != -1) this.setData({
                        [`list[${i}].invoiceamount`]: detail.invoiceamount,
                        [`list[${i}].invoiceaqty`]: invoiceaqty
                    })
                }
            })
        },
        /* 删除订单行 */
        deleteItem({
            detail
        }) {
            console.log(detail)
            let that = this;
            wx.showModal({
                title: '提示',
                content: `是否确认删除“${detail.itemname}”`,
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": "20221217091403",
                        "version": 1,
                        "content": {
                            "sa_invoiceapply_orderids": [detail.sa_invoiceapply_orderid]
                        }
                    }).then(res => {
                        console.log('删除订单行', res)
                        wx.showToast({
                            title: res.msg == '成功' ? '删除成功' : res.msg,
                            icon: "none"
                        });
                        if (res.msg == '成功') that.setData({
                            list: that.data.list.filter(v => v.sa_orderitemsid != detail.sa_orderitemsid)
                        })
                    })
                }
            })
        },
    }
})