var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    info: [],
    other: [],
    user: [],
    card_code:{},
    card_status: 0,
    card_color:'Color060',
    is_show:true,
    show_q:false,
    is_getUserProfile:false,
    first: 1
  },
  onLoad: function (options) {//接收上个页面传的参数
    var self=this;
    app.globalData.is_key_first=2;
    if (options.invite_key) {
      app.globalData.invite_key = options.invite_key;
    }
    if (wx.getUserProfile){
      self.setData({
        is_getUserProfile:true
      })
    }
  },
  onShow: function () {
    var self = this;
    app.getUserInfo(function (ret) {
      if (ret) {
        self.setData({
          info: ret,
        });
      }
      self.getIndex();
    });

  },
  getPhoneNumber: function (e) {
    if (e.detail.errMsg =='getPhoneNumber:ok'){//同意授权
      var data = new Object;
      data.iv = e.detail.iv;
      data.encryptedData = e.detail.encryptedData;
      this.get_card_sn(data);
    }
  },
  get_card_sn:function(data){
    var self = this;
    var action = 'c=Member&a=get_card_sn';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', data, function (ret) {
      //console.log(ret);
      var arr = new Array;
      arr.push(ret.data);
      app.HCaddCard(arr,function(ret){
        if(ret.errMsg=="addCard:ok"){
          var code = ret.cardList[0].code;
          self.save_card_code(code);
        }
      });
    }, true);
  },
  save_card_code: function (code) {
    var self = this;
    var action = 'c=Member&a=save_card_code';
    app.showLoading('加载中...');
    var param = new Object;
    param.code = code;
    app.ajaxRequest(action, 'POST', param, function (ret) {
      //console.log(ret);
      if (ret.status == 1) {
        self.getIndex();
      }else{
        if (!ret.msg){
          ret.msg = '参数错误';
        }
        app.HCerror('提示',ret.msg,false);
      }
    }, true);
  },
  active_card_sn:function(){
    var arr = new Array;
    var self = this;
    var cartList = self.data.card_code;
    if (cartList){
      arr.push(cartList);
      app.HCopenCard(arr,function(ret){
        self.getIndex();
      });
    }
  },
  //查看我的小店
  goTomyshop: function () {
    // app.HCnavigateTo('../little_shop/little_shop');
    this.get_user('../../../smallshop/pages/xiao_shop/xiao_shop');
  },
  getIndex: function () {
    var self = this;
    var action = 'c=Member&a=index';
    var first = self.data.first;
    if (first == 1) {
      app.showLoading('加载中...');
    }
    app.ajaxRequest(action, 'GET', '', function (ret) {
      first++;
      //self.to_sign();//执行签到
      if (ret.status == 1) {
        if(ret.info==''){
          app.HCswitchTab('../../index/index');
        }
        if(ret.info.header_img==''){
          ret.info.header_img = self.data.info.avatarUrl;
        }
        if(ret.info.username==''){
          ret.info.username = self.data.info.nickName;
        }
        self.setData({
          other: ret.data,
          user: ret.info,
          info:ret.info,
          card_code: ret.card_code,
          card_status: ret.card_status,
          card_color: ret.card_color,
          first: first
        });
      }
    }, true);
  },
  to_sign: function () {
    if (app.globalData.is_sign == 1)return;
    var self = this;
    var action = 'c=Member&a=to_sign';
    app.ajaxRequest(action, 'GET', '', function (ret) {
      if (ret.status == 1) {
        app.HCtoast('签到成功！');
        app.globalData.is_sign = 1;
        self.getIndex();
      }else{
        if (!ret.msg) ret.msg = '参数错误';
        // app.HCerror('提示', ret.msg,false);
      }
    }, true);
  },
  //110新加
  goTomyfocus: function () {
    if (this.data.user != '') {
      this.get_user('../focus_list/focus_list');
    }
  },
  //end
  goToBalance: function (event) {
    this.get_user('../balance/balance');  
  },
  goToIntegral: function (event) {
    this.get_user('../integral/list/list?invite_key=' + app.globalData.init_invite_key);
  
  },
  goToCoupon: function (event) {
    this.get_user('../coupon/list/list?invite_key=' + app.globalData.init_invite_key);
  },
  goToCollect: function (event) {
    if (this.data.user != '') {
      this.get_user('../collect/index');
    }
  },
  goToOrder: function (event) {
    if (this.data.user != '') {
      this.get_user('../order/index?class=' + event.currentTarget.dataset.class);
    }
  },
  goToServiceOrder: function (event) {
    if (this.data.user != '') {
      this.get_user('../../order/service_index/service_index?class=' + event.currentTarget.dataset.class + '&invite_key=' + app.globalData.init_invite_key);
    }
  },
  goToMoney: function (event) {
    if (this.data.user != '') {
      this.get_user('../money/list/list');
    }
  },
  goToAddress: function () {
    this.get_user('../address/address');
  },
  goTomyinfo: function () {
    this.get_user('../myinfo/myinfo');
  },
  gotoSign:function(){
    this.get_user('../member_sign/index');
  },
  goToMemberInvite:function(){
    this.get_user('../member_invite/index/index');
     // app.HCnavigateTo('../member_invite/member_poster/member_poster');
  },
  goToProLevel:function(){
    this.get_user('../../../smallshop/pages/pro_level/pro_level/pro_level');
  },
  goToGroup:function(){
    this.get_user('../../../spellgroup/pages/order_group/index?class=0');
  },
  //二维码弹窗
  get_code:function(){
    this.setData({
      is_show:false
    })
  },
  //取消二维码弹窗
  lookconfirm: function (event) {
    this.setData({
      is_show: true
    })
  },
  //获取用户信息授权
  bindGetUserInfo: function (e) {
    var self = this;
    if (wx.getUserProfile){
      wx.getUserProfile({
        desc:'用于完善会员资料',
        success:function(res){
          app.HCsetStorage('profile_Users', res.userInfo);
          app.HCsetStorage('profile_iv', res.iv);
          app.HCsetStorage('profile_encryptedData', res.encryptedData);
          app.getUserInfo(function (ret) {  
            self.setData({
              show_q: false,
              info: ret,
            })
            if ($.is_empty(ret.mobile) && e.detail.userInfo) {
              app.HCnavigateTo('../../login_accredit/login_accredit');
            }
          })
        }
      })
    }else{
      app.getUserInfo(function (ret) {
        self.setData({
          show_q: false,
          info: ret,
        })
        if ($.is_empty(ret.mobile) && e.detail.userInfo) {
          app.HCnavigateTo('../../login_accredit/login_accredit');
        }
      })
    }
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
      param: {},
      hasMore: false,
      dataNull: true
    });
    this.getIndex();
  },
  onShareAppMessage: function () {
    return {
      title: '汉潮社交电商',
      desc: '汉潮社交电商欢迎你!',
      path: '/' + $.getCurrentPageUrl() + '?invite_key=' + app.globalData.init_invite_key
    }
  },
  get_user: function (url) {
    var self = this;
    app.getUserInfo(function (ret) {
      if ($.is_empty(ret.username)) {
        self.setData({
          show_q: true
        })
      } else {
        self.setData({
          info: ret,
        });
        var user_info = ret;
        if ($.is_empty(user_info.mobile)) {
          app.HCnavigateTo('../../login_accredit/login_accredit');
          return;
        } else {
          app.HCnavigateTo(url);
        }
      }
    });
  },
  hide: function () {
    var self = this;
    self.setData({
      show_q: false,
    })
  }
})