// packagecard/paypage/paypage.js
const app = getApp()
const utils = require('../../utils/util')
const RSA = require('../../utils/rsa')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nodes: [{
            name: 'div',
            children: [{
                type: 'text',
                text: '&nbsp;'
            }]
        }],
        inviteCode: '', //邀请码
        dispatchCode: '', //人员码
        price: 0, //价格
        orderNumber: '00001',
        orderDate: '2020-20-20',
        orderGoodsName: '',
        hintMsg: '', //提示信息
        isInput: true, //是否是手动输入金额
        isChecked: false, //勾选用户协议
        isShowTreaty: false, //是否显示协议
        treatyStr: '', //协议内容
        isDisplayTreaty: false, //是否需要勾选用户协议
        isShowHint: false, //是否显示提示内容
        type: 1, //1.支付总20   2.支付单个  
        isShowUseDeal: false, //使用协议
        isShowServiceTreaty: false, //会员服务协议
        isShowIntimacyTreaty: false, //隐私政策
        typeList: [], //开通的类型列表
        selTypeIndex: 0, //当前选中的类型列表
        selTypeId: '', //当前选中的类型id
        selType: '', //选中的类型type
        isShowContrast: false, //是否显示版本对比
        contrastId: '', //版本对比图片ID
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            orderGoodsName: '会员服务费',
            hintMsg: '您本次支付的费用为会员服务费,本次支付成功后可以使用免费模板来创建名片',
            isShowHint: true
        })
        this.getPayTypeList()
    },
    //获取开通类型列表
    getPayTypeList() {
        var _self = this
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get(app.urls.getMemberTypeList, {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                if (res.data.length > 0) {
                    res.data.sort((a, b) => {
                        return b.cardChargeRoleStatus - a.cardChargeRoleStatus
                    })
                    _self.setData({
                        typeList: res.data,
                        selTypeIndex: 0,
                        selTypeId: res.data[0].cardChargeRoleId,
                        price: res.data[0].cardChargeRolePrice,
                        orderGoodsName: res.data[0].cardChargeRoleName,
                        selType: res.data[0].cardChargeRoleType
                    })
                }
            })
            .catch(err => {

            })
    },
    //获取总使用权限的订单预支付信息
    getUsePayOrder() {
        wx.showLoading({
            title: '加载中...',
        })
    },
    changeCheck(e) {
        this.setData({
            isChecked: !this.data.isChecked
        })
    },
    inputWatch(e) {
        this.setData({
            inviteCode: e.detail.value
        })
    },
    //显示协议
    showTreaty() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get(app.urls.getTreaty, {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                console.log(res.data)
                wx.hideLoading({})
                this.setData({
                    treatyStr: res.data,
                    isShowTreaty: true
                })
            })
            .catch(err => {

            })
    },
    onClose() {
        this.setData({
            isShowUseDeal: false, //使用协议
            isShowServiceTreaty: false, //会员服务协议
            isShowIntimacyTreaty: false, //隐私政策
            isShowContrast: false //版本对比
        })
    },
    //提交支付
    doPay() {
        var _self = this
        if (_self.data.isChecked) {
            _self.wxPay()
        } else {
            wx.showToast({
                title: '请勾选使用协议',
                icon: 'error',
                success(res) {
                    setTimeout(() => {
                        _self.useDeal()
                    }, 800)
                }
            })
        }
    },

    //微信支付
    wxPay() {
        wx.showLoading({
            title: '支付中...',
        })
        if (this.data.type == 1) {
            this.doPayUseOrder()
        } else if (this.data.type == 2) {
            this.doPaySingleOrder()
        }
    },
    //支付单一名片
    doPaySingleOrder() {

    },
    //选中开通的
    chooseType(e) {
        var item = e.currentTarget.dataset.item
        var idx = e.currentTarget.dataset.idx
        var _self = this
        if (item.cardChargeRoleStatus == 1) {
            _self.setData({
                selTypeIndex: idx,
                selTypeId: item.cardChargeRoleId,
                price: item.cardChargeRolePrice,
                orderGoodsName: item.cardChargeRoleName,
                selType: item.cardChargeRoleType
            })
        }
    },
    //支付总的金额
    doPayUseOrder() {
        var _self = this
        app.http.post(app.urls.getCardPayState.format({
                mode: _self.data.selType
            }), {
                header: {
                    token: app.globalData.token
                },
                data: {
                    cardChargeInvitationUserId: _self.data.inviteCode != '' ? _self.data.inviteCode : app.globalData.showUserId,
                    cardChargeRole: _self.data.selTypeId
                }
            })
            .then(res => {
                wx.hideLoading({})
                if (res.statusCode == 200) {
                    var tempId = res.data.prepayId
                    wx.requestPayment({
                        nonceStr: res.data.nonceStr,
                        package: 'prepay_id=' + res.data.prepayId,
                        paySign: res.data.paySign,
                        signType: 'RSA',
                        timeStamp: res.data.timestamp,
                        success(res) {
                            if (res.errMsg == 'requestPayment:ok') {
                                //支付成功
                                wx.redirectTo({
                                    url: '/pages/mine/order/ordersuccess?type=' + 2 + '&msg=支付成功&fMsg=去创建名片&price=' + _self.data.price + '&prepayId=' + tempId,
                                })
                            }
                        },
                        fail(err) {
                            if (err.errMsg == 'requestPayment:fail cancel') {
                                //支付取消
                                wx.showToast({
                                    title: '您取消了支付',
                                    icon: 'error'
                                })
                            } else {
                                wx.showToast({
                                    title: '支付失败,请稍后重试',
                                    icon: 'none'
                                })
                            }
                        }
                    })
                }
            })
            .catch(err => {
                // wx.showToast({
                //     title: '获取订单失败',
                //     icon: 'err'
                // })
            })
    },
    //用户协议
    useDeal() {
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            treatyStr: ''
        })
        app.http.get(app.urls.getTreaty.format({
                key: 'cardCreateTemplateUse'
            }), {})
            .then(res => {
                wx.hideLoading({})
                this.setData({
                    treatyStr: res.data.data,
                    isShowUseDeal: true
                })
            })
            .catch(err => {

            })
    },
    //会员服务协议
    serviceDeal() {
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            treatyStr: ''
        })
        app.http.get(app.urls.getTreaty.format({
                key: 'cardCreateTreaty'
            }), {})
            .then(res => {
                wx.hideLoading({})
                this.setData({
                    treatyStr: res.data.data,
                    isShowUseDeal: true
                })
            })
            .catch(err => {

            })
    },
    //隐私政策
    useIntimacy() {
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            treatyStr: ''
        })
        app.http.get(app.urls.getTreaty.format({
                key: 'privacyPolicy'
            }), {})
            .then(res => {
                wx.hideLoading({})
                this.setData({
                    treatyStr: res.data.data,
                    isShowIntimacyTreaty: true
                })
            })
            .catch(err => {

            })
    },
    //显示版本对比
    showContrast() {
        var _self = this
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get(app.urls.getVersionContrast, {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                _self.setData({
                    contrastId: app.urls.baseImgUrl + res.data.data,
                    isShowContrast: true,
                })
            })
            .catch(err => {})
    }
})