const _Http = getApp().globalData.http;

Page({
    data: {
        loading: true,
        sa_logisticsid: null,
        tabsActive: 0,
        tabsList: [{
            label: "详细信息",
            icon: "icon-tabchanpin"
        }, {
            label: "物流查询",
            icon: "icon-tabfujian1",
            model: "#Query"
        }, {
            label: "装箱明细",
            icon: "icon-tabcaozuojilu1",
            model: "#Binding"
        }]
    },
    onLoad(options) {
        this.setData({
            sa_logisticsid: options.id
        });
        this.getDetail(true);
    },
    /* 获取详情 */
    getDetail(init = false, show = true) {
        _Http.basic({
            "id": 20221204201804,
            "content": {
                nocache: true,
                "sa_logisticsid": this.data.sa_logisticsid
            }
        }, show).then(res => {
            console.log("出货详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setPreview(res.data[0]);
            this.setData({
                detail: res.data[0],
                loading: false
            });
            if (init) this.partialRenewal(true)
        })
    },
    /* 设置详情信息 */
    setPreview(data) {
        let list1 = [{
                label: "物流单号",
                value: data.logisticno
            }, {
                label: "状态",
                value: data.status == '审核' ? '待收货' : '已收货'
            }, {
                label: "收货人",
                value: data.name
            }, {
                label: "收货地址",
                value: data.address
            }, {
                label: "快递公司",
                value: data.logiscomp_enterprisename
            }, {
                label: "快递单号",
                value: data.logisticno
            }, {
                label: "运费状态",
                value: data.paytype
            }, {
                label: "运费",
                value: data.freightamount
            }, {
                label: "备注",
                value: data.remarks
            }, ],
            list2 = [{
                label: "创建人",
                value: data.createby
            }, {
                label: "创建时间",
                value: data.createdate
            }, {
                label: "审核人",
                value: data.checkby
            }, {
                label: "审核时间",
                value: data.checkdate
            }, {
                label: "收货人",
                value: data.receiveby
            }, {
                label: "收货时间",
                value: data.receivedate
            }];
        this.setData({
            list1,
            list2
        })
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //局部数据更新 tabs
    partialRenewal(init = false) {
        let model = this.data.tabsList[this.data.tabsActive].model;
        if (model) {
            let Component = this.selectComponent(model),
                {
                    total,
                    pageNumber,
                    pageTotal
                } = Component.data.content,
                id = this.data.detail.sa_logisticsid;
            // if (model == "#Binding") id = this.data.detail.sa_logistics_boxid;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber <= pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    /* 确认收货 */
    receiving() {
        let that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认收货?确认后不可更改',
            complete: (res) => {
                if (res.confirm) _Http.basic({
                    "id": "20221122133204",
                    "content": {
                        "sa_logisticsid": that.data.sa_logisticsid
                    }
                }).then(s => {
                    console.log("确认收货", s)
                    if (s.msg != '成功') return wx.showToast({
                        title: s.msg,
                        icon: "none",
                        mask: true
                    });
                    that.getDetail(true, false);
                    setTimeout(() => {
                        wx.showToast({
                            title: '确认收货成功',
                            icon: "none",
                            mask: true
                        });
                    }, 500);
                })
            }
        })
    },
    onReachBottom() {
        this.partialRenewal();
    },
    onUnload() {
        let page = getCurrentPages().find(v => v.__route__ == 'packageA/shipment/index');
        let content = JSON.parse(JSON.stringify(page.data.content));
        content.pageNumber = 1;
        content.pageSize = (page.data.content.pageNumber - 1) * page.data.content.pageSize;
        _Http.basic({
            "id": wx.getStorageSync('userrole') == '业务员' ? 20221122132804 : 20230111141104,
            content
        }).then(res => {
            console.log("更新订单列表", res)
            if (res.msg == '成功') page.setData({
                list: res.data
            })
        })
    },
})