// pages/member/address/myinfo/myinfo.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    goodsId: '',
    optionsId: '',
    total: '',
    tag: '',
    explain_title: '',
    explain_content: '',
    form: {
      member_name: '',
      member_card: '',
    },
    activityId: "",
    leader_id:''//拼购团长id
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.goodsId) {
      this.setData({
        goodsId: options.goodsId
      });
    }
    if (options.optionsId) {
      this.setData({
        optionsId: options.optionsId
      });
    }
    if (options.total) {
      this.setData({
        total: options.total
      });
    }
    if (options.tag) {
      this.setData({
        tag: options.tag
      });
    }
    if (options.activityId) {
      this.setData({
        activityId: options.activityId
      });
    }
    if (options.leader_id) {
      this.setData({
        leader_id: options.leader_id
      });
    }
    this._getexplain();
    this._getMember();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  _getexplain() {
    let that = this;
    let urlStr = app.getNetAddresss("from.div-from.explain");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            explain_title: res.data.explain_title,
            explain_content: res.data.explain_content
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  _getMember() {
    let that = this;
    let urlStr = app.getNetAddresss("from.div-from.getMemberInfo");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            'form.member_name': res.data.realname,
            'form.member_card': res.data.idcard
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //真实姓名值绑定
  memberNameinp(e) {
    let val = e.detail.value;
    this.setData({
      'form.member_name': val
    });
  },
  //身份证号码值绑定
  membercardinp(e) {
    let val = e.detail.value;
    this.setData({
      'form.member_card': val
    });
  },
  saveInfo() {
    let that = this;

    if (app._isTextEmpty(this.data.form.member_name)) {
      wx.showToast({
        title: '请填写姓名！',
        icon: 'none'
      });
      return;
    }
    if (app._isTextEmpty(this.data.form.member_card)) {
      wx.showToast({
        title: '请填写身份证号！',
        icon: 'none'
      });
      return;
    }

    var _goodsId = this.data.goodsId;
    var _optionsId = this.data.optionsId;
    var _total = this.data.total;
    var submitActionTag = this.data.tag;
    let urlStr = app.getNetAddresss("from.div-from.updateMemberInfo");
    urlStr += '&member_name=' + this.data.form.member_name;
    urlStr += '&member_card=' + this.data.form.member_card;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (submitActionTag == 'cart') {
            that.addCartReq(_goodsId, _optionsId, _total);
            return;
          }
          if (submitActionTag == 'groupBuy_open'){
            //拼购开团
            wx.navigateTo({
              url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-2' + '&optionsId=0'+'&goodsId=' + _goodsId + '&total=1'+ '&type=groupBuy_open'+ '&at_id='+that.data.activityId
            });
            return;
          }
          if (submitActionTag == 'groupBuy_join'){
            //拼购参团
            wx.navigateTo({
              url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&optionsId=0'+'&goodsId=' + _goodsId + '&total=1'+ '&type=groupBuy_join'+ '&at_id='+this.data.activityId + '&leader_id='+this.data.leader_id
            });
            return;
          }
          if (submitActionTag == 'group'){
            //拼团
            wx.navigateTo({
              url: "/packageA/detail_v2/detail_v2?id=" + that.data.activityId + '&name=group'
            });
            return;
          }
          if (submitActionTag == 'delta'){
            //返回上一页
            wx.navigateBack({
              delta: 1
            });
            return;
          }

          wx.navigateTo({
            url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=' + submitActionTag + '&goodsId=' + _goodsId + '&optionsId=' +
							_optionsId + '&total=' + _total
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
});
