const _Http = getApp().globalData.http;
/* {
            name: '促销订单'
        },  */
Component({
    data: {
        types: [{
            name: '标准订单'
        }, {
            name: '项目订单'
        }, {
            name: '工具订单'
        }],
        actionShow: false,
    },
    methods: {
        /* 挑选新增订单类型 */
        onSelect() {
            this.setData({
                actionShow: true
            })
        },
        /* 选择订单类型 */
        selectType({
            detail
        }) {
            switch (detail.name) {
                case '标准订单':
                    wx.navigateTo({
                        url: '/packageA/orderForm/add/add?type=标准订单',
                    })
                    break;
                case '项目订单':
                    /* 前去挑选合同 */
                    wx.navigateTo({
                        url: `/select/contract/index?params=${JSON.stringify({
                            "id": 20230103150802,
                            "content": {
                                nocache: true,
                                "pageNumber": 1,
                                "pageTotal": 1,
                                "total": null,
                                "pageSize": 20,
                                "where": {
                                    "condition": ""
                                }
                            }
                        })}&radio=false`
                    })
                    getApp().globalData.handleSelect = this.addProjectOrder.bind(this);
                    break;
                case '促销订单':
                    /* 前去挑选合同 */
                    wx.navigateTo({
                        url: `/select/activity/index?params=${JSON.stringify({
                            "id": 20220103140003,
                            "content": {
                                "version": 1,
                                "nocache": true,
                                "pageNumber": 1,
                                "pageTotal": 1,
                                "where": {
                                    "condition": ""
                                },
                                "sort": []
                            }
                        })}&radio=false`
                    })
                    getApp().globalData.handleSelect = this.addActivityOrder.bind(this);
                    break;
                case '工具订单':
                    wx.navigateTo({
                        url: '/packageA/orderForm/add/add?type=工具订单',
                    })
                    break;
            }
            this.onCancel();
        },
        /* 取消选择订单类型 */
        onCancel() {
            this.setData({
                actionShow: false
            })
        },
        /* 创建项目订单 */
        addProjectOrder({
            item
        }) {
            wx.showModal({
                title: '提示',
                content: `是否确认创建“${item.billno}”相关“${item.projectname}”项目订单?`,
                complete: (res) => {
                    if (res.confirm) _Http.basic({
                        "id": 20230103141402,
                        "content": {
                            "sys_enterpriseid": item.sys_enterpriseid, //订货企业id，可不传，默认取当前账号的
                            "sa_contractid": item.sa_contractid, //合同ID，
                            "sa_projectid": item.sa_projectid, //工程项目表ID，
                        },
                    }).then(res => {
                        console.log("新建项目订单", res)
                        if (res.msg != '成功') return wx.showToast({
                            title: res.msg,
                            icon: "none",
                            mask: true
                        })
                        if (res.msg == '成功') setTimeout(() => {
                            wx.redirectTo({
                                url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
                            });
                            let page = getCurrentPages().find(v => v.__route__ == 'packageA/orderForm/index');
                            if (page) page.getList(true);
                        }, 500)
                    })
                }
            })
        },
        addActivityOrder({
            item
        }) {
            wx.showModal({
                title: '提示',
                content: `是否确定创建“${item.promname}”促销订单`,
                complete: (s) => {
                    if (s) _Http.basic({
                        "id": 20221108111402,
                        content: {
                            sa_orderid: 0,
                            sa_accountclassid: item.sa_accountclassid,
                            rec_contactsid: 0,
                            pay_enterpriseid: 0,
                            sa_contractid: 0,
                            sa_projectid: 0,
                            sa_promotionid: item.sa_promotionid,
                            "sa_brandid": item.sa_brandid, //品牌ID
                            "type": '促销订单', //订单类型
                            "tradefield": item.tradefield, //必选
                        }
                    }).then(res => {
                        console.log("创建促销订单", res);
                        wx.showToast({
                            title: res.msg != '成功' ? res.msg : '创建成功',
                            icon: "none",
                            mask: true
                        });
                        if (res.msg == '成功') setTimeout(() => {
                            wx.redirectTo({
                                url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
                            });
                            let page = getCurrentPages().find(v => v.__route__ == 'packageA/orderForm/index');
                            if (page) page.getList(true);
                        }, 500)
                    })
                }
            })
        },
    }
})