var app = getApp();
var $ = require('../../js/util.js');
Page({
  data: {
    info: [],
    mobile: '',
    invite_key: 0,
    param: {
      unique: 1,
      type: 1
    },
    web_name:'',
    web_logo:'',
    open_register:'',
  },
  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);
  },
  check_invite:function(){
    app.HCerror('提示', '请输入推广码', false);
  },
  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);
  },
  checkboxChange: function (e) {
    var self = this;
    self.setData({
      tiaokuan: e.detail.value
    })
  },
  gotoLogin:function(){
    app.HCredirectTo('../login/index');
  },
  binddesc: function (event) {
    this.setData({
      invite_code: event.detail.value
    })
  }
})