// index.js
let myPro = require("../../../utils/wxRequest.js");
let util = require("../../../utils/util.js");
Page({
    data: {
		userInfo: null,
		showPhonePop: false,
		codeBtnName: "获取验证码",
		delyTime: 60, // 单位s
		codeLoading: false,
		showBirthPop: false,
		maxDate: new Date().getTime(),
    },
    onLoad: function (options) {
        // Do some initialize when page load.
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		// 生日的可选最小值 当前年份前50年
		let minYear = new Date().getFullYear() - 50;
		let str = minYear+'-'+'01-01'
		that.setData({
			minDate: new Date(str).getTime()
		});
		// 获取用户信息
		that.getUrserInfo();
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
		
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 更改性别 弃用
	onChangeSex(event){
		let that = this;
		that.setData({
			sex: event.detail
		})
	},
	// 获取用户信息
	getUrserInfo(){
		let that = this;
		myPro.wxRequest("user/v2/userinfo","GET",{}).then(res=>{
			that.setData({
				userInfo: res.result,
			});
			// 用户之前存过生日
			if(res.result.birthday){
				let birthStr = res.result.birthday;
				that.setData({
					currentBirth: new Date(birthStr.replace(/-/g, "/")).getTime()
				});
			}else{
				that.setData({
					currentBirth: new Date().getTime()
				});
			}
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: 'none'
			});
		});
	},
	// 打开更改手机号弹层
	onShowChangePhone(){
		let that = this;
		that.setData({
			showPhonePop: true
		});
	},
	// 关闭
	onCloseChangePhone(){
		let that = this;
		that.setData({
			showPhonePop: false
		});
	},
	// 获取输入信息 手机号
	getPhone: function (event) {
        let that = this;
        that.setData({
            phone: event.detail,
        });
    },
	// 发送验证码
	sendCode(){
		let that = this;
		if(!that.data.phone || !(/^1[3456789]\d{9}$/.test(that.data.phone))){
			wx.showToast({
				title: "手机号有误,请重新填写",
				icon: "none"
			});
			return;
		};
		let params = {
			phone: that.data.phone
		};
		myPro.wxRequest("user/send/verify-code","POST",params).then(res=>{
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			that.setData({
				codeBtnName: that.data.delyTime +
					"s",
				delyTime: that.data.delyTime,
				codeLoading: true
			});
			let s = setInterval(function() {
				let dely1 = parseInt(that.data.delyTime) -
					1;
				// console.log(dely1);
				that.setData({
					codeBtnName: dely1 +
						"s",
					delyTime: dely1
				});
				if (dely1 == 0) {
					clearInterval(s);
					that.setData({
						codeBtnName: "获取验证码",
						delyTime: that.data.delyTime,
						codeLoading: false
					});
				}
			}, 1000);
		}).catch(err=>{
			that.setData({
				codeLoading: false
			});
			console.log('报错信息');
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	},
	// 获取输入信息 验证码
    getPhoneCode: function (event) {
        let that = this;
        that.setData({
            code: event.detail,
        });
    },
	// 更改手机号
	onConfirmChangePhone(){
		let that = this;
		// return;
		if(!that.data.phone || !(/^1[3456789]\d{9}$/.test(that.data.phone))){
			wx.showToast({
				title: "手机号有误,请重新填写",
				icon: "none"
			});
			return;
		};
		if (!that.data.code) {
            wx.showToast({
                title: "请填写验证码",
                icon: "none",
            });
            return;
        };
		let params = {
			phone: that.data.phone,
			code: that.data.code
		};
		myPro.wxRequest("user/v2/editphone","POST",params).then(res=>{
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			that.setData({
				showPhonePop: false
			});
			// 更新用户信息
			that.getUrserInfo();
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	},
	// 打开生日弹层
	onShowBirthPop(){
		let that = this;
		that.setData({
			showBirthPop: true
		});
	},
	// 关闭生日弹层
	closeBirthPop(){
		let that = this;
		that.setData({
			showBirthPop: false
		});
	},
	// 选择生日
	confirmBirthPop(event){
		console.log('event===',event)
		let that = this;
		let selectTime = new Date(event.detail);
		let birthStr = util.formatTime(selectTime,1);
		// 修改生日
		let params = {
			birthday: birthStr
		};
		myPro.wxRequest("user/v2/editBirthday","POST",params).then(res=>{
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			that.setData({
				showBirthPop: false,
			});
			// 更新用户信息
			that.getUrserInfo();
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		})
	},
});
