// pages/memberCenter/menberCenter2.js
const api = require('../../api/index')
const pay = require('../../utils/pay')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    localImagePath:app.globalData.localImagePath,
    imgHttp:app.globalData.imghttp,
    userinfo:{},
    vipType:[],
    chooseVip:null,
    tableData:[],//会员权益
    tableHead:[
      { key: 'functionName', name: '功能类型' },
      { key: '123', name: '非会员' },
      { key: '456', name: '会员' },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserinfo()
    this.queryAllVip()
    this.getVipRights()
  },

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

  },

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

  },

  /**
   * 页面功能
   */
   //获取所有会员
   queryAllVip(){
    var that = this
    api.queryAllVip().then(res=>{
      if(res.code == 200){
       this.setData(
         {
          vipType:res.data.data
         }
       )
      }
    })
  },
   //获取会员权益
   getVipRights(){
    var that = this
    api.getVipRights().then(res=>{
      if(res.code == 200){
        var data = res.data.data
        var tableData = []
        for(var i = 0;i<data.length;i++){
          var obj = {
            functionName:data[i].functionName
          }
          var right = data[i].vipRightsVOList
          for(var j = 0;j<that.data.tableHead.length;j++){
            var head = that.data.tableHead[j]
            if(head.key == 'functionName'){
              continue
            }
            var obj2 = right.find(item=>{
              return item.vipUuid == head.key
            })
            if(obj2){
              obj[head.key] = obj2.description
            }else{
              obj[head.key] = ''
            }
          }
          tableData.push(obj)
        }
        that.setData(
          {
            tableData:tableData
          }
        )
      }
    })
  },
  //获取用户信息
  getUserinfo(){
    api.selectPerson().then(res=>{
      if(res.code == 200){
        this.setData(
          {
            userinfo:res.data
          }
        )
      }
    })
  },
  //选择会员类型
  chooseVip(e){
    var {item} = e.currentTarget.dataset
    this.setData(
      {
        chooseVip:item
      }
    )
  },
  //开通会员
  successPay(e){
    this.getUserinfo()
    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 2]
    currentPage.getUserinfo()
  },
  openVip(){
    if(!this.data.chooseVip){
      api.notify(
        {
          type:'warning',
          message:'请先选择需要开通会员的类型',
          // top:wx.getStorageSync('height')
        }
      )
      return
    }
    var data ={
      title:'开通会员',
      type:'-1',
      goods:[
        {
          id:this.data.chooseVip.vipUuid,
          name:this.data.chooseVip.name,
          price:this.data.chooseVip.finalPrice,
          num:1
        }
      ],
      servicePrice:0.00,
      price:this.data.chooseVip.finalPrice,
      discount:null,
    }
    pay.openPay(data)
    return 
    if(!this.data.chooseVip){
      api.notify(
        {
          type:'warning',
          message:'请先选择需要开通会员的类型',
          top:wx.getStorageSync('height')
        }
      )
      return
    }
    var params = {
      goodType:1,
      goodUuid:this.data.choseVip.vipUuid
    }
    wx.showLoading({
      title: '加载中',
    })
    pay.payMoney(params).then(res=>{
      if(res.errMsg == 'requestPayment:ok'){
        api.notify(
          {
            type:'success',
            message:'开通会员成功',
            top:wx.getStorageSync('height')
          }
        )
        this.getUserinfo()
        var pages = getCurrentPages()
        var currentPage = pages[pages.length - 2]
        currentPage.getUserinfo()
      }
    }).catch(error=>{

    })
  
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})