<template>
	<!-- login页面 -->
	<view>
		<view class="login" @touchmove.stop.prevent="moveHandle">
			<!-- <image class="images" :src="url"></image> -->
			<view class="backes"></view>
			<view class="title_top">德铭阳光在线-商家端</view>
			<view class="content">
				<view class="title">
					<view v-bind:class="[state==0 ? 'show' : '']" @click="changetab(0)">
						商家登录
					</view>
					<view v-bind:class="[state==1 ? 'show' : '']" @click="changetab(1)">
						商家达人
					</view>
                    <view v-bind:class="[state==2 ? 'show' : '']" @click="changetab(2)">
                    	平台达人
                    </view>
				</view>
				<view class="" v-if="state==0">
					<view class="labales">
						<image src="../../static/image/login/login(3).png"></image>
						<input v-model="zhanghao" type="text" @blur="getBlurVal" placeholder="请输入账号" />
					</view>
					<view class="labales">
						<image src="../../static/image/login/login(1).png"></image>
						<input v-model="mima" type="password" placeholder="请输入密码" />
					</view>
				</view>
				<view class="" v-else-if="state==1">
					<view class="labales">
						<image src="../../static/image/login/login(3).png"></image>
						<input v-model="zhanghaoA" type="text" @blur="getBlurVal1" placeholder="请输入账号" />
					</view>
					<view class="labales">
						<image src="../../static/image/login/login(1).png"></image>
						<input v-model="mimaA" type="password" placeholder="请输入密码" />
					</view>
				</view>
                <view v-else>
                    <view class="labales">
                    	<image src="../../static/image/login/login(3).png"></image>
                    	<input v-model="phone" type="number" maxlength="11" placeholder="请输入手机号" />
                    </view>
                    <view class="labales">
                    	<image src="../../static/image/login/safe.png"></image>
                    	<input v-model="code" type="number" placeholder="请输入验证码" />
                        <u-button class="get-code" :class=" is_sendcode ? 'stopClick' : '' " :disabled="is_sendcode" @click="getCode">
                            {{ is_sendcode ? time_count + "后可以重新发送" : "发送验证码" }}
                        </u-button>
                    </view>
                </view>
			</view>
			<!-- 服务协议 -->
			<view class="pact">
				<view class="pact-container">
					<u-checkbox v-model="consentPact" shape="circle" size="30" label-size="26" icon-size="12" active-color="#ff780f">
						<text>我已详细阅读并同意</text>
					</u-checkbox>
					<text class="pact-items" v-for="(item,index) in pactList" :key="index" @click="viewAgreement(index)"> {{ item.text }}</text>
				</view>
			</view>
			<!-- denglu QQ weixin -->
			<button @click="logins">{{ login }}</button>
			<!-- <u-button :custom-style="customStyle" ></u-button> -->
			<u-toast ref="uToast" />
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			list: [
				{
					checked: false,
					disabled: false
				}
			],
			value: '',
			login: '登录',
			show: false,
			zhanghao: '',
			mima: '',
			zhanghaoA: '',
			mimaA: '',
			url: '',
			phone: "",
			code: "",
			state:0,
			clickstate:false,
			// 时间
			timer: null,
			is_sendcode: false, // 60s
			time_count: 60,
			consentPact: true,
			pactList: [{
				"text": "《用户协议》"
			},{
				"text":"《隐私政策》"
			}]
		};
	},
	methods: {
		// 改变登陆类型
		changetab(num){
			this.state = num
		},
		mask_u() {
			this.show = !this.show;
		},
		// 获取验证码倒计时
		getCode() {
            if (!this.$u.test.mobile(this.phone)) {
                this.$refs.uToast.show({
                    title: '手机号码有误！',
                })
                return;
            }
            this.$u.api.getCode({
                member_mobile: this.phone,
            }).then(res => {
                console.log(res);
                if (res.errCode == 0) {
                    this.$refs.uToast.show({
                        title: res.message,
                        type: 'success',
                    })
                    this.getSendCode();
                } else {
                    this.$refs.uToast.show({
                        title: res.message,
                        type: "warning"
                    })
                }
            })
		},
        // 失去焦点获取数据
        getBlurVal(e) {
            // console.log(e.detail.value);
            this.zhanghao = this.$u.trim(e.detail.value,"all");
        },
        getBlurVal1(e) {
            this.zhanghaoA = this.$u.trim(e.detail.value,"all");
            // console.log(this.$u.trim(this.zhanghaoA));
        },
		logins() {
			// 检查网络
			uni.getNetworkType({
			    success:  (res) => {
			        console.log(res.networkType);
					if (res.networkType == 'none') {
						this.$refs.uToast.show({
							title: '网络似乎断开了，请检查网络后重试',
							type: 'error'
						});
					} 
			    },
			});
			
			// 不同意协议不让登录
			if(!this.consentPact)  return false;

			// 判断是否可以点击
            // console.log(this.zhanghao);
			// if(this.clickstate==false){
			// 	this.clickstate=true
			// }else{
				// 此验证存在逻辑问题,会出现卡死在这里的情况
				// this.$refs.uToast.show({
				// 	title: "不能重复提交",
				// 	type: 'error'
				// });
				// return
			// }
			// 清除本地存储才可以正常登陆（具体原因没有仔细检查）
			// uni.clearStorage();
			// 账号登录1达人0商家
			if(this.state==1){
				this.$u.api.login({ member_name: this.zhanghaoA, member_password: this.mimaA }).then(res => {
					console.log(JSON.stringify(res))
					if (res.errCode != 0) {
						// 恢复按钮可点击状态
						// this.clickstate=false
						this.$refs.uToast.show({
							title: res.message,
							type: 'error'
						});
					} else {
						uni.setStorageSync('token', res.data.token);
						uni.setStorageSync('userinfo', res.data);
						uni.setStorageSync('rongyun', res.data.rongcloud_token);
						this.$u.route({
							url: '/pages/user/myinfo',
							type:"redirect"
						});
					}
				});
			}else if (this.state == 2) {
                if (!this.$u.test.mobile(this.phone)) {
                    this.$refs.uToast.show({
                        title: '手机号码有误！',
                    })
                    return;
                }
                if (this.code.length < 4) {}
                this.$u.api.starLogin({
                    member_mobile: this.phone,
                    sms_code: this.code,
                }).then(res => {
                    if (res.errCode == 0) {
                        this.$refs.uToast.show({
                            title: res.message,
                            type: 'success',
                        })
                        uni.setStorageSync('token', res.data.token);
                        uni.setStorageSync('userinfo', res.data);
                        uni.setStorageSync('rongyun', res.data.rongcloud_token);
                        this.$u.route({
                        	url: '/pages/user/myinfo',
                        	type:"redirect"
                        });
                    } else {
                        this.$refs.uToast.show({
                            title: res.message,
                            type: "warning"
                        })
                    }
                })
            } else{
				this.$u.api.shoplogin({ member_name: this.zhanghao, member_password: this.mima }).then(res => {
					console.log(JSON.stringify(res))
					if (res.errCode != 0) {
						// 恢复按钮可点击状态
						// this.clickstate=false
						this.$refs.uToast.show({
							title: res.message,
							type: 'error'
						});
					} else {
						uni.setStorageSync('token', res.data.token);
						uni.setStorageSync('userinfo', res.data);
						uni.setStorageSync('rongyun', res.data.rongcloud_token);
						// this.$u.route({
						// 	url: '/pages/index/index',
						// 	type: 'switchTab'
						// });
                        uni.switchTab({
                            url: '/pages/index/index',
                        })
					}
				});
			}
		},
        // 60s
        getSendCode() {
            const TIME_COUNT = 60;
            if (!this.timer) {
                this.time_count = TIME_COUNT;
                this.is_sendcode = true;
                this.timer = setInterval(() => {
                    if (this.time_count > 0 && this.time_count <= TIME_COUNT) {
                        this.time_count--;
                    } else {
                        this.is_sendcode = false;
                        clearInterval(this.timer);
                        this.timer = null;
                    }
                }, 1000);
            }
        },
        moveHandle(){
            return;
		},
		viewAgreement(index) {
			console.log(index);
			if (index == 0) {
				this.$u.route({
					url: '/pages/login/agreements',
					params: {
						title: "用户协议"
					}
				});
			}
			
			if (index == 1) {
				this.$u.route({
					url: '/pages/login/privacy_policy',
					params: {
						title: "隐私政策"
					}
				});
			}
		}
	},
	onLoad() {
		let token = uni.getStorageSync('token');
		let user = uni.getStorageSync('userinfo');
		console.log( user.role == 2 ?  '/pages/index/index' : '/pages/user/myinfo')
		if (token != undefined && token) {
			this.$u.route({
				url: user.role == 2 ?  '/pages/index/index' : '/pages/user/myinfo',
				type: user.role == 2 ? 'tab' : 'to'
			});
		}
	},
};
</script>

<style lang="scss" scoped>
.login:before{
	background: url(../../static/bg.png) no-repeat;
	background-size: cover;
	width: 750rpx;
	height: 100vh;
	content: "";
	position: absolute;
	top: 0;
	left: 0;
	z-index: -1;/*-1 可以当背景*/
    transform: scale(1.1);
	-webkit-filter: blur(3px);
	filter: blur(3px);
}
.login {
	.images {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		z-index: -1;
	}

	.backes {
		background: rgba(0, 0, 0, 0.4);
		position: fixed;
		width: 100%;
		height: 100%;
		z-index: -1;
	}

	text {
		z-index: 9;
		color: #fff;
	}

	.more_Login {
		overflow: hidden;
		zoom: 1;
		width: 630rpx;
		margin: 0 auto;
	}

	.more_Login text {
		display: inline-block;
		width: 50%;
		float: left;
		font-size: 30rpx;
		color: rgba(255, 255, 255, 1);
		line-height: 36px;
		margin: 97rpx 0;
	}

	.more_Login .other {
		text-align: right;
	}
}

.title {
	font-size: 36rpx;
	font-weight: bold;
	margin-bottom: 150rpx;
	color: #fff;
	display: flex;
	justify-content: flex-start;
}
.title view{
	height: 100rpx;
	line-height: 100rpx;
	font-size: 24rpx;
	padding: 0 20rpx;
}
.title .show{
	font-size: 34rpx;
	font-weight: bold;
}

.content {
	width: 630rpx;
	z-index: 99999;
	margin: 0 auto;
	padding-top: 300rpx;

	.labales text {
		font-size: 30rpx;
	}
    
    .get-code {
        width: auto !important;
        height: 60rpx !important;
        padding: 0 30rpx !important;
        font-size: 24rpx !important;
        line-height: 60rpx !important;
        color: #fff !important;
        border: none !important;
        border-radius: 10rpx !important;
        &::after {
            border: none !important;
        }
    }

	.labales {
        display: flex;
        align-items: center;
		height: 90rpx;
		line-height: 90rpx;
		border-bottom: 1px #fff solid;
		&:first-child {
			margin-bottom: 160rpx;
		}
		&:nth-child(2) {
			margin-bottom: 80rpx;
		}
		input {
			width: 80%;
			margin-left: 30rpx;
			color: #fff;
			letter-spacing: 2rpx;
			background-color: none;
		}
	}
	.labales image {
        flex-shrink: 0;
		width: 38rpx;
		height: 40rpx;
	}
	.labales:nth-child(3) {
		position: relative;
		margin-bottom: 60rpx;
	}

	.identifying {
		position: absolute;
		right: 0;
		top: 0;
		bottom: 0;
		margin: auto;
		font-size: 30rpx;
		font-weight: 500;
		color: rgba(255, 120, 15, 1);
		line-height: 20px;
	}
}
.uni-input-input,
.uni-input-placeholder {
	color: #fff !important;
	letter-spacing: 2rpx;
}

.pact {
	width: 670rpx;
	margin: 0 auto 40rpx;
	font-size: 26rpx;
	overflow: hidden; //超出的文本隐藏
	text-overflow: ellipsis; //溢出用省略号显示
	white-space: nowrap; //溢出不换行
	.pact-container {
		display: flex;
		justify-content: flex-end;
		align-items: center;
		flex-wrap: wrap;
		/deep/ .u-checkbox {
			.u-checkbox__label {
				margin-right: 0;
			}
		}
		.pact-items {
			color: rgba(255, 120, 15, 1);
		}
	}
}

.u-checkbox-group {
	position: absolute;
	left: 0;
	top: 0;
	bottom: 0;
	margin: auto;
}

.u-checkbox__icon--square {
	border-radius: 50rpx !important;
	width: 22rpx;
	height: 22rpx;
}

.u-btn {
	width: 628rpx;
	height: 98rpx;
	background: rgba(255, 120, 15, 1) !important;
	border-radius: 49rpx;
	margin: 0 auto;
	font-size: 36rpx;
	color: rgba(255, 255, 255, 1) !important;
	line-height: 36px;
	border: 1px #ff780f solid;
	outline: none;
	border-color: rgba(255, 120, 15, 1) !important;
}

.warp {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100%;
}

.rect {
	width: 558rpx;
	height: 300rpx;
	background-color: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	zoom: 1;
	position: relative;
	display: flex;
	flex-wrap: wrap;
	display: -webkit-flex; /* Safari */
	.rect_view {
		width: 100rpx;
		height: 100rpx;
		.image {
			width: 45rpx;
			height: 53rpx;
			display: inline-block;
		}
		flex: 1;
		margin-top: 77rpx;
	}
	.rect_view:nth-child(2) image {
		width: 57rpx;
		height: 45rpx;
	}

	.rect_butoon {
		width: 100%;
		position: absolute;
		bottom: 0;
		height: 85rpx;
		text-align: center;
		line-height: 85rpx;
		border-top: 1px #999999 solid;
	}
	view {
		color: #666;
		font-size: 24rpx;
		margin-top: 30rpx;
	}
}
.rect_view:nth-child(1) image,
.rect_view:nth-child(1) view {
	float: right;
}
.rect_view:nth-child(1) image {
	margin-right: 20rpx;
}
.rect_view:nth-child(1) {
	position: relative;
	margin-right: 79rpx;
}
.rect_view:nth-child(1) view {
	position: absolute;
	bottom: 0;
	right: 0;
}
.rect_view:nth-child(2) image,
.rect_view:nth-child(2) view {
	float: left;
}
.rect_view:nth-child(2) image {
	margin-left: 20rpx;
}
.rect_view:nth-child(2) {
	position: relative;
}
.rect_view:nth-child(2) view {
	position: absolute;
	bottom: 0;
	left: 0;
}
.title_top {
	position: fixed;
	top: calc(var(--status-bar-height) + 18px);
	left: 0;
	right: 0;
	margin: auto;
	font-size: 36rpx;
	color: rgba(255, 255, 255, 1);
	text-align: center;
	width: 100%;
	/* 如果您想让slot内容占满整个导航栏的宽度 */
	/* flex: 1; */
	/* 如果您想让slot内容与导航栏左右有空隙 */
	/* padding: 0 30rpx; */
	background: none;
	letter-spacing: 4rpx;
}
button{
	width: 670rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	cursor: pointer;
	border-radius: 50rpx;
	height: 100rpx;
	padding: 0 40rpx;
	background-color: #ff780f;
	color: #FFFFFF;
}
</style>
