var app = getApp();
var $ = require('../../js/util.js');
Page({
  data: {
    info: [],
    mobile: '',
    invite_key: 0,
    param: {
      unique: 1,
      type: 1
    },
    tiaokuan:0,
    sendmsg: "sendmsg",
    getmsg: "获取验证码",
  },
  onLoad: function (options) {//接收上个页面传的参数
    var self = this;
    var invite_key = 0;
    if (options.invite_key && options.invite_key != app.globalData.init_invite_key) {
      app.globalData.invite_key = invite_key = options.invite_key;
      self.setData({
        invite_key: options.invite_key
      })
    } else {
      invite_key = app.globalData.invite_key;
      if (invite_key) {
        self.setData({
          invite_key: invite_key
        })
      }
    }
    self.get_invite_mobile(invite_key);
  },
  onShow: function () {
    var self = this;
    app.getUserInfo(function (ret) {
      if (ret.username != ' ') {
        self.setData({
          info: ret,
        });
      } else {
        self.setData({
          show_q: true
        })
      }
    },true);
  
  },
  get_invite_mobile(invite_key) {
    var self = this;
    var action = 'c=Member&a=get_invite_mobile';
    app.showLoading('加载中...');
    var data = new Object;
    data.invite_key = invite_key;
    //console.log(data);
    app.ajaxRequest(action, 'POST', data, function (ret) {
      //console.log(ret);
      if (ret.web_name) {
        self.setData({
          web_name: ret.web_name
        })
      }
      if (ret.web_logo) {
        self.setData({
          web_logo: ret.web_logo
        })
      }
      if (ret.open_register) {
        self.setData({
          open_register: ret.open_register
        })
      }
      if (ret.status == 1 && ret.data) {
        self.setData({
          invite_code: ret.data
        })
      } else {
        self.setData({
          invite_key: 0,
        })
      }
    }, true);
  },
  getPhoneNumber: function (e) {
    var self=this;
    if (e.detail.errMsg =='getPhoneNumber:ok'){//同意授权
      var data = new Object;
      data.iv = e.detail.iv;
      data.encryptedData = e.detail.encryptedData;
      data.tiaokuan = self.data.tiaokuan;
      this.get_card_sn(data);
    }
  },
  get_card_sn:function(data){
    var self = this;
    var action = 'c=Member&a=save_user_mobile';
    data.ref_mobile = self.data.invite_code;
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', data, function (ret) {
      //console.log(ret);
      if(ret.status==1){
        var user_info = app.globalData.userInfo;
        user_info.mobile = ret.data.mobile;
        if ($.is_empty(user_info.username)) {
          user_info.username = ret.data.mobile;
        }
        app.globalData.userInfo = user_info;
        app.HCnavigateBack();
        
      }else{
        app.HCerror('提示', ret.msg, false);
      }
     
    }, true);
  },
  GotoContent:function(event){
    app.HCnavigateTo('../article/cont/cont?id=' + event.currentTarget.dataset.gid);
  },
  get_mobile: function (e) {
    var self = this;
    self.setData({
      mobile: e.detail.value
    })
  },
  sendMessage: function () {
    var self = this;
    var timer = 1;

    var param = self.data.param;
    param.mobile = self.data.mobile;
    var action = 'c=Ajax&a=sendsms';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'GET', $.ObjectToStr(self.data.param), function (ret) {
      if (ret.status == 1) {
        if (timer == 1) {
          timer = 0
          var time = 120
          self.setData({
            sendmsg: "sendmsgafter",
          })
          var inter = setInterval(function () {
            self.setData({
              getmsg: time + "s后重新发送",
            })
            time--
            if (time < 0) {
              timer = 1
              clearInterval(inter)
              self.setData({
                sendmsg: "sendmsg",
                getmsg: "获取验证码",
              })
            }
          }, 1000)
        }
        app.HCtoast('发送成功！');
      } else {
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
  save_cart_sn: function (event) {
    var self = this;
    var value = event.detail.value;
    value.id = self.data.info.id;
    value.invite_key = self.data.invite_key;
    value.tiaokuan = self.data.tiaokuan;
    value.invite_code = self.data.invite_code;
    var action = 'c=Member&a=save_user_mobile_input';
    if (self.data.tiaokuan == 0) {
      app.HCerror('提示', "请查看用户服务协议并选择", false); return;
    }
    if (self.data.invite_code == 0) {
      app.HCerror('提示', "请查看用户服务协议并选择", false); return;
    }
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', value, function (ret) {
      //console.log(ret);
      if (ret.status == 1) {
        var user_info = app.globalData.userInfo;
        user_info.mobile = ret.data.mobile;
        if ($.is_empty(user_info.username)) {
          user_info.username = ret.data.mobile;
        }
        app.globalData.userInfo = user_info;
        app.HCnavigateBack();

      } else {
        app.HCerror('提示', ret.msg, false);
      }

    }, true);
  },
  checkboxChange:function(e){
    var self=this;
    self.setData({
      tiaokuan: e.detail.value
    })
  },
  binddesc: function (event) {
    this.setData({
      invite_code: event.detail.value
    })
  }
})