let sys_payorderid = null,
    _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    pageNumber = 1,
    pageTotal = 1,
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();

Page({
    data: {
        loading: true
    },
    onLoad(options) {
        sys_payorderid = options.id;
        // sys_payincidence 1账号 2主体
        this.setData({
            sys_payincidence: wx.getStorageSync('siteP').sys_payincidence,
            sys_payswitch: wx.getStorageSync('siteP').sys_payswitch
        })
        this.getDetail();
        this.getList(true);
    },
    getDetail() {
        _Http.basic({
            "classname": "system.payorder.payorder",
            "method": "detail",
            "content": {
                nocache: true,
                sys_payorderid
            }
        }).then(res => {
            console.log("付费订单详情", res)
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                    mask: true
                });
                setTimeout(() => wx.navigateBack(), 1000)
                return;
            };
            let detail = res.data;
            detail.showAmount = CNY(detail.amount);
            switch (detail.ispaid) {
                case 0:
                    detail.color = "#FF3B30";
                    detail.status = '未付费';
                    break;
                case 1:
                    detail.color = '#52C41A';
                    detail.status = '已付费';
                    break;
                case 2:
                    detail.color = '#ddd';
                    detail.status = "新建";
                    break;
                case 3:
                    detail.color = '#ddd';
                    detail.status = "已取消";
                    break;
                case 4:
                    detail.color = '#ddd';
                    detail.status = "已退费";
                    break;
            };
            this.setData({
                detail,
                showroom: [{
                    label: "付费订单号",
                    value: detail.orderno
                }, {
                    label: "付费状态",
                    value: detail.status,
                    color: detail.color,
                }, {
                    label: "付费金额",
                    value: detail.showAmount,
                    color: '#FF3B30',
                }, {
                    label: "付费时间",
                    value: detail.paytime
                }, {
                    label: "付费版本",
                    value: detail.partitionname,
                    func: "viewDetails"
                }, {
                    label: "付费人",
                    value: detail.createby
                }, {
                    label: "付费渠道",
                    value: detail.paymode
                }, {
                    label: "备注",
                    value: detail.remarks
                }],
                loading: false
            });
        })
    },
    /* 获取列表 */
    getList(init = false) {
        if (init) {
            pageNumber = 1;
            pageTotal = 1;
        };
        if (pageNumber > pageTotal) return;
        _Http.basic({
            "classname": "system.payorder.payorder",
            "method": "detail_userlist",
            "content": {
                sys_payorderid,
                pageNumber,
                "where": {
                    "condition": ""
                }
            }
        }).then(res => {
            console.log("获取付费列表", res)
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                    mask: true
                });
                setTimeout(() => wx.navigateBack(), 1000)
                return;
            };
            pageNumber = res.pageNumber + 1;
            pageTotal = res.pageTotal;
            res.data = res.data.map(v => {
                v.discount = v.price == 0;
                v.price = CNY(v.price)
                return v
            })
            this.setData({
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),

                total: res.total
            })
        })
    },
    /* 支付 */
    payment() {
        let that = this;
        wx.login({
            success: (s) => {
                _Http.basic({
                    "classname": "system.payment.wechatpay",
                    "method": "createWechatOrder",
                    "content": {
                        "orderno": that.data.detail.orderno,
                        "wechat_code": s.code,
                        "trade_type": "JSAPI"
                    }
                }).then(res => {
                    console.log("获取支付信息", res)
                    if (res.data == '失败') return wx.showToast({
                        title: res.msg,
                        icon: "none",
                        mask: true
                    });
                    wx.requestPayment({
                        timeStamp: res.data.timeStamp,
                        nonceStr: res.data.nonceStr,
                        package: res.data.package,
                        signType: res.data.signType,
                        paySign: res.data.paySign,
                        success(re) {
                            console.log(re)
                            wx.showToast({
                                title: '支付成功',
                                mask: true
                            })
                            that.getDetail();
                        },
                        fail(err) {
                            console.error(err)
                            wx.showToast({
                                title: '已取消支付',
                                icon: "none",
                                mask: true
                            })
                        }
                    })
                })
            },
        })

    },
    /* 删除订单 */
    deleteOrder() {
        let that = this;
        wx.showModal({
            title: '提示',
            content: '是否确定删除该订单',
            complete: (s) => {
                if (s.confirm) _Http.basic({
                    "classname": "system.payorder.payorder",
                    "method": "delete",
                    "content": {
                        "sys_payorderid": that.data.detail.sys_payorderid
                    }
                }).then(res => {
                    console.log("删除订单", res)
                    wx.showToast({
                        title: res.msg == '成功' ? '删除成功' : res.msg,
                        icon: "none",
                        mask: true
                    });
                    if (res.msg == '成功') setTimeout(() => {
                        wx.navigateBack()
                    }, 500)
                })
            }
        })
    },
    /* 查看版本 */
    viewDetails() {
        this.selectComponent("#inventory").show(this.data.detail.systemapp)
    },
    onReachBottom() {
        this.getList();
    },
    onUnload() {
        let page = getCurrentPages().find(v => v.__route__ == 'pages/teams/index');
        if (!page) return;
        if (page.data.active.title == '付费订单') {
            let content = JSON.parse(JSON.stringify(page.data.content));
            content.pageSize = (content.pageNumber - 1) * content.pageSize;
            content.pageNumber = 1;
            _Http.basic({
                "classname": "system.payorder.payorder",
                "method": "list",
                content
            }).then(res => {
                console.log('更新列表数据', res)
                res.data = res.data.map(v => {
                    switch (v.ispaid) {
                        case 0:
                            v.color = "#FF3B30";
                            v.status = '未付费';
                            break;
                        case 1:
                            v.color = '#52C41A';
                            v.status = '已付费';
                            break;
                        case 2:
                            v.color = '#ddd';
                            v.status = "新建";
                            break;
                        case 3:
                            v.color = '#ddd';
                            v.status = "已取消";
                            break;
                        case 4:
                            v.color = '#ddd';
                            v.status = "已退费";
                            break;
                    };
                    v.amount = CNY(v.amount);
                    return v
                })
                if (res.msg == '成功') page.setData({
                    list: res.data
                })
            })
        }
    }
})