const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		phone:'',
		bCode:'',
		showPicker:false,
		cityId:'',
		cityValue:'请选择城市',
    cityList:[],
		columns:[],
		username:'',
		userInfo:{},
	},
  onCancel: function () {
    this.setData({
      showPicker: false,
    });
  },
  onConfirm: function (event) {
    this.setData({
      cityId: this.data.cityList[event.detail.index].id,
      cityValue:event.detail.value,
      showPicker: false,
		});
	
	},
	showPicker(){
		this.setData({
      showPicker: true,
    });
	},
	nameInput(e){
		this.setData({
			username:e.detail.value
		})
	 },
	 getAllCity(){
		util.request(api.GetAllCity, {}, 'get').then(res => {
      if (res.errno === 0) {
				let list=res.data;
				let columns=list.map(item=>{return item.cityName});
        this.setData({
				 cityList:list,
				 columns:columns
        })
      
      } else {
        util.showErrorToast(res.errmsg);
      }
    })
	 },
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		 this.getAllCity();
		 this.setData({
			 phone:app.globalData.recommend.phone,
			 bCode:app.globalData.recommend.bCode
		 })
	},
	confirmRegister(){
		if(this.data.cityValue=='请选择城市'){
			wx.showToast({
				title: '请选择城市',
				icon: 'none',
				duration: 2000
			});
		}else if(this.data.username==''){
			wx.showToast({
				title: '请输入姓名',
				icon: 'none',
				duration: 2000
			})
		}else{
			console.log(1);
		
			wx.getUserProfile({
				desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success: (res) => {
					console.log(res);
					if (res.userInfo == null) {  
						wx.showToast({
							title: '未授权，无法完善信息',
							icon: 'success',
							duration: 3000
						});
						return;
					}else{
						this.setData({
							userInfo: res.userInfo,
						});
						this.registerReferrer();
					}   
				
				}
			})

		}
	},
	registerReferrer(){
		util.request(api.RegisterReferrer, {
			"applyCode": this.data.bCode,
			"avatar":this.data.userInfo.avatarUrl,
			"cityId": this.data.cityId,
			"customerName":this.data.username,
			"gender": this.data.userInfo.gender,
			"nickname":this.data.userInfo.nickName,
			"openId": app.globalData.recommend.openId,
			"phone":this.data.phone,
			"ruserid": app.globalData.rId,
			"sessionKey": app.globalData.recommend.sessionKey,
			"unionId": app.globalData.recommend.unionId
		}, "POST")
		.then((res)=> {
			if (res.errno == 0) {
			
			 //存储用户信息  
					app.globalData.hasLogin = true;
					wx.setStorageSync('userInfo', res.data.userInfo);
					wx.setStorageSync('token', res.data.token);
					app.globalData.shareImage=res.data.sharImage;
					// wx.navigateTo({
					// 	url: '../steward/steward',
					// })
					wx.switchTab({
						url: '/pages/index/index',
					})
	
			} else {
				util.showErrorToast(res.errmsg);
			}
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

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

	},

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

	},

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

	},

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

	},

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

	},

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

	}
})