// index.js
let myPro = require("../../../utils/wxRequest.js");
let util = require("../../../utils/util.js");

Page({
    data: {
		phone: null,
		code: null,
		codeLoading: false,
		codeBtnName: "获取验证码",
		delyTime: 60, // 两次发验证码间隔时长/秒
		loading: false // 全局loading
    },
    onLoad: function (options) {
        // Do some initialize when page load.
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		that.setData({
			userInfo: getApp().globalData.userInfo
		});
		// console.log('用户信息',that.data.userInfo)
		// 店铺
		wx.getStorage({
			key: "store",
			success: function(res){
				that.setData({
					store: JSON.parse(res.data)
				})
			}
		});
    },
    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
    },
	// 获取输入信息 手机号
	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,
        });
    },
	// 手机号验证码登录
    phoneLogin() {
        let that = this;
        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, // 手机号验证码
			openid: getApp().globalData.openid,
			nickname: getApp().globalData.userInfo.nickname,
			thumb: getApp().globalData.userInfo.thumb,
			sex: getApp().globalData.userInfo.sex,
			store_id: that.data.store ? that.data.store.id : 1 // 门店id
		};
		myPro.wxRequest("user/mini-login","POST",params).then(res=>{
			// 存下token
			getApp().globalData.token = res.result.token;
			wx.setStorage({
				key: "token",
				data: res.result.token
			});
			// 存下角色
			if(res.result.user.store){
				getApp().globalData.role = 1 // 有店铺,即为门店
			};
			wx.reLaunch({
				url: '/pages/index/index'
			});
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
    },
});
