// pages/CardChooseMembership/CardChooseMembership.js
var app = getApp();
const DATA_URL = "plugin.universal-card.api.home.get-goods";
Page({
  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    nickname: "",
    thumb: "",
    level_name: "",
    priceArr: [],
    isOpen: false,
    currentIndex: 0,
    option_id: 0,
    goods_id: 0,
    date: "",
    levelId: "",
    goodsId: ""
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.goods_id) {
      this.setData({
        goodsId: options.goods_id
      });
    }
    if (options.level_id) {
      this.setData({
        levelId: options.level_id
      });
    }
    this.initData();
    this.getData();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  initData() {
    this.setData({
      nickname: "",
      thumb: "",
      level_name: "",
      priceArr: [],
      isOpen: false,
      currentIndex: 0,
      option_id: 0,
      goods_id: 0
    });
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss(DATA_URL);
    urlStr += '&goods_id=' + this.data.goodsId;
    urlStr += '&level_id=' + this.data.levelId;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isOpen: app._isTextEmpty(res.data.expire_date) ? false : true,
            nickname: res.data.nickname,
            thumb: res.data.thumb,
            level_name: res.data.level_name,
            priceArr: res.data.level_date,
            goods_id: res.data.goods_id,
            option_id: res.data.level_date[0].option_id,
            date: res.data.expire_date
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  toggle(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
      currentIndex: index,
      option_id: item.option_id
    });
  },
  open() {
    //普通商品
    wx.navigateTo({
      url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&goodsId=' + this.data.goods_id +
				'&optionsId=' + this.data.option_id + '&total=1'
    });
  }
});
