// packageF/debt/vipIndex/vipIndex.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: [],
        RenewShow: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.setNavigationBarTitle({
            title: 'VIP',
        })
        this.getData();
        this.getUser();
        this.setData({
            plugin_setting:wx.getStorageSync('yz_basic_info').plugin_setting.debt_shop || {}
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    toOrder() {
        wx.navigateTo({
            url: '/packageA/member/myOrder_v2/myOrder_v2?member_type=3',
        })
    },
    toDetail() {
        wx.navigateTo({
            url: '/packageF/debt/purchaseDetails/purchaseDetails?isvip=true',
        })
    },
    toBindDetail() {
        wx.navigateTo({
            url: '/packageF/debt/vipBindRecord/vipBindRecord',
        })
    },
    // 申请vip
    applyTap(){
        wx.navigateTo({
            url: '/packageF/debt/debtapply/debtapply?type=3',
        })
    },
    renewTap() {
        let renewUserId = this.data.datainfo.vip_id
        this.setData({
            RenewShow: true,
            renewUserId
        })
        this.getGoodsCount();
    },
    toDetailVip() {
        wx.navigateTo({
            url: '/packageA/detail_v2/detail_v2?id=' + this.data.vip_goods_id,
        })
    },
    toShop(){
        wx.navigateTo({
          url: '/packageF/debt/debtor/debtorShop/debtorShop?kid='+this.data.datainfo.debtor_id,
        })  
    },
    removeTap() {
        let that = this;
        wx.showModal({
            content: '是否确认解除绑定',
            success(res) {
                if (res.confirm) {
                    that.removePost();
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    removePost() {
        let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.apply.deleteBindApply');
        app._getNetWork({
            url: urlStr,
            success: (resdata) => {
                let res = resdata.data
                if (res.result == 1) {
                    wx.showToast({
                      title: res.msg,
                      icon:'none',
                      duration:1000
                    })
                    this.getData();
                    this.getUser();
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    getGoodsCount() {
        let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.applyData');
        app._getNetWork({
            url: urlStr,
            success: (resdata) => {
                let res = resdata.data
                if (res.result == 1) {
                    this.setData({
                        vip_goods_id: res.data.vip_goods_id,
                        vip_goods_count: res.data.vip_goods_count
                    })
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    // 续约按钮接口
    renewPost() {
        if (this.data.goodsTotal > this.data.vip_goods_count) {
            wx.showToast({
                icon: 'none',
                title: '拥有商品数量不足',
                duration: 1500
            });
            return
        };
        let json = {
            id: this.data.renewUserId,
            count: this.data.goodsTotal
        }
        let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.rechargeDebtMemberTime');
        app._getNetWork({
            url: urlStr,
            data: json,
            success: (resdata) => {
                let res = resdata.data
                if (res.result == 1) {
                    this.setData({
                        RenewShow: false
                    })
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1000,
                        success: function () {
                            this.getData();
                            this.getUser();
                        }
                    });
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    getData() {
        let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.vip.baseData');
        app._getNetWork({
            url: urlStr,
            success: (resdata) => {
                var res = resdata.data;
                if (res.result == 1) {
                    this.setData({
                        datainfo: res.data
                    })
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    getUser() {
        let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.getDebtData');
        app._getNetWork({
            url: urlStr,
            data: {
                member_type: 3
            },
            success: (resdata) => {
                var res = resdata.data;
                if (res.result == 1) {
                    this.setData({
                        userInfo: res.data
                    })
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    }
})