let util = require('../../../utils/util.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newcount: 5,
    vipcount: 5
  },

  freshvipprice() {
    this.setData({
      vipprice: parseFloat(this.data.vipp.currentPrice * this.data.vipcount).toFixed(2),
      vipall: parseFloat(this.data.vipp.originalPrice * this.data.vipcount).toFixed(2)
    })
  },
  setvipcount(e) {
    this.setData({
      vipcount: e.detail.value
    })
    this.freshvipprice()
  },
  vipbuynow() {
    if (this.data.vipcount === 0 || this.data.vipcount === '0' || this.data.vipcount === '') {
      util.showToast('请输入正确人数')
      return
    }
    wx.navigateTo({
      url: '/pages/myinfo/pay/pay?num=' + this.data.vipcount + '&year=1&pid=' + this.data.vipp.id + '&name=新增记账人数&currentPrice=' + this.data.vipp.currentPrice + '&originalPrice=' + this.data.vipp.originalPrice
    })
  },
  gettips() {
    util.query('/memberAndService/wxapi/getInfo', {}, function(e) {
      that.setData({
        fntitle: e.data.title,
        fncontent: e.data.content.replace('\\n', '\n'),
        fetitle: e.data.freeTitle,
        fecontent: e.data.freeContent.replace('\\n', '\n')
      })
    })
  },

  freshnewprice() {
    this.setData({
      newprice: parseFloat(this.data.newp.currentPrice * this.data.newcount).toFixed(2),
      newall: parseFloat(this.data.newp.originalPrice * this.data.newcount).toFixed(2)
    })
  },
  tcbuy(e) {
    wx.showModal({
      title: '确认',
      content: '是否确定购买套餐',
      success(res) {
        if (res.confirm) {

          util.query("memberOrder/wxapi/create", {
            packageYears: e.currentTarget.dataset.year,
            memberPackage: {
              id: e.currentTarget.dataset.id
            }
          }, function(d) {
            if (d.code == 10000) {
              wx.requestPayment({
                'timeStamp': d.data.timeStamp + "",
                'nonceStr': d.data.nonceStr,
                'package': d.data.packageValue,
                'signType': 'MD5',
                'paySign': d.data.paySign,
                'success': function(res) {
                  console.log(res);
                  if (res.errMsg == "requestPayment:ok") {
                    util.showToast('支付成功')
                    that.freshmyinfo()
                    setTimeout(function() {

                      wx.switchTab({
                        url: '/pages/myinfo/myinfo/myinfo',
                      })
                    }, 2000)
                  }
                },
                'fail': function(res) {
                  util.showToast('支付失败')

                },
                'completev': function(res) {
                  // if (res.requestPayment == "cancel") {
                  //   common.showToast('支付取消1')

                  // }
                }
              })
            } else {

            }
          }, "POST")
        } else if (res.cancel) {

        }
      }
    })
  },
  freshmyinfo() {
    util.query('userinfo/wxapi/findbyid', {
        id: this.data.user.id
      },
      function(e) {
        wx.setStorage({
          key: 'userinfo',
          data: e.data,
        })
      })
  },
  setnewcount(e) {
    this.setData({
      newcount: e.detail.value
    })
    this.freshnewprice()
  },
  newbuynow() {
    if (this.data.newcount === 0 || this.data.newcount === '0' || this.data.newcount === '') {
      util.showToast('请输入正确人数')
      return
    }
    wx.navigateTo({
      url: '/pages/myinfo/pay/pay?num=' + this.data.newcount + '&year=1&pid=' + this.data.newp.id + '&name=新用户购买&currentPrice=' + this.data.newp.currentPrice + '&originalPrice=' + this.data.newp.originalPrice
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    util.checklogin(
      function() {
        var ui = wx.getStorageSync('userinfo')
        if (ui) {
          ui.validityTime = ui.validityTime ? ui.validityTime.split(' ')[0] : ''
          // ui.isvip = 2
          that.setData({
            user: ui
          })
          that.getList()
          that.gettips()
        }
      }
    )

  },
  getList() {
    util.query('memberPackage/wxapi/listpage', {}, function(res) {
      if (res.code === 10000) {
        var newp, vipp, ps = []
        res.data.content.forEach(function(e) {
          if (e.packageType === 2) {
            if (e.forNewMember === 1) {
              newp = e
            } else {
              vipp = e
            }
          } else {
            ps.push(e)
          }
        })
        that.setData({
          data: ps,
          newp: newp,
          vipp: vipp
        })
        that.freshnewprice()
        that.freshvipprice()
      }
    })

  },
  pay(e) {


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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})