// pages/memberCenter/memberCenter.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:{},
    equity:[
      {
        icon:'/static/svg/down.png',
        label:'权益一'
      },
      {
        icon:'/static/svg/会员权益 (1).png',
        label:'权益二'
      },
      {
        icon:'/static/svg/会员权益.png',
        label:'权益三'
      },
      {
        icon:'/static/svg/会员级别处理.png',
        label:'权益四'
      },
    ],
    grades:[
      {
        background_icon:'/static/svg/B级会员背景图标.svg',
        background_color:'#3f395c',
        grade:'B级会员',
        money:'365'
      },
      {
        background_icon:'/static/svg/A级会员背景图标.svg',
        background_color:'#ff0101',
        grade:'A级会员',
        money:'520'
      },
      {
        background_icon:'/static/svg/S级会员背景图标.svg',
        background_color:'#000000',
        grade:'S级会员',
        money:'888'
      },
    ],
    choseVip:null,
    tableData:[],//会员权益
    tableHead:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserinfo()
  //   var thead= [
  //     { key: 'orderNum', name: '订单编号' },
  //     { key: 'machNum', name: '机器编号' },
  //     { key: 'planeNum', name: '计划生产数量' },
  //     { key: 'planeEndTime', name: '计划结束时间' },
  //     { key: 'productName', name: '产品名称' }
  // ]
  // var data= [
  //     { orderNum: '01234dsnfbujhcdsvfkudshcvjbsdkcbsdjkchbsdjkhb', machNum: '23', planeNum: '20', planeEndTime: '2022-08-08', productName: '8W0 864 733-B9_PA' },
  //     { orderNum: '31234', machNum: '23', planeNum: '20', planeEndTime: '2022-08-08', productName: '8W0 864 733-B9_PA' },
  //     { orderNum: '13455', machNum: '23', planeNum: '20', planeEndTime: '2022-08-08', productName: '8W0 864 733-B9_PA' },
  //     { orderNum: '47867', machNum: '23', planeNum: '20', planeEndTime: '2022-08-08', productName: '8W0 864 733-B9_PA' },
  //     { orderNum: '34565', machNum: '23', planeNum: '20', planeEndTime: '2022-08-08', productName: '8W0 864 733-B9_PA' },
  //     { orderNum: '76809', machNum: '23', planeNum: '20', planeEndTime: '2022-08-08', productName: '8W0 864 733-B9_PA' },
  //     { orderNum: '01034', machNum: '23', planeNum: '20', planeEndTime: '2022-08-08', productName: '8W0 864 733-B9_PA' }
  // ]
  // this.setData(
  //   {
  //     tableHead:thead,
  //     tableData:data
  //   }
  // )

    this.queryAllVip()
    
  },

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

  },

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

  },

  /**
   * 页面功能
   */
  //获取所有会员
   queryAllVip(){
    var that = this
    api.queryAllVip().then(res=>{
      if(res.code == 200){
        console.log(res)
        var data = res.data.data
        var grades = []
        var tableHead = [
          {
            key:"functionName",
            name:''
          }
        ]
        data.forEach(item => {
          var config = JSON.parse(item.configParameter)
          var obj =  {
            background_icon:config.background_icon,
            background_color:config.background_color,
            grade:item.name,
            money:item.price,
            vipUuid:item.vipUuid
          }
          tableHead.push(
            {
              key:item.vipUuid,
              name:item.name
            }
          )
          grades.push(obj)
        })
        that.setData(
          {
            grades:grades,
            tableHead:tableHead,
            choseVip:null
          }
        )
        that.getVipRights()
        console.log(that.data.grades)
      }
    })
  },
  //获取会员权益
  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
          }
        )
        console.log(that.data.tableHead,that.data.tableData)
      }
    })
  },
  //获取用户信息
  getUserinfo(){
    api.selectPerson().then(res=>{
      if(res.code == 200){
        this.setData(
          {
            userinfo:res.data
          }
        )
      }
    })
  },
  //选择开通会员类型
  chooseVip(e){
    var {item} = e.currentTarget.dataset
    this.setData(
      {
        choseVip:item
      }
    )
  },
  //开通会员
  openVip(){
    if(!this.data.choseVip){
      api.notify(
        {
          type:'warning',
          message:'请先选择需要开通会员的类型',
          top:wx.getStorageSync('height')
        }
      )
    }
    var params = {
      goodType:1,
      goodUuid:this.data.choseVip.vipUuid
    }
    wx.showLoading({
      title: '加载中',
    })
    pay.payMoney(params).then(res=>{
      console.log(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() {

  }
})