<template>
	<view class="content">
		<view class="top">
			<!-- 	<view class="touxiang">
				<image src="../../static/logo.png" mode=""></image>
			</view> -->
			<view class="dalan">你好，</view>
			<view class="dalan">欢迎登陆商户端</view>

			<view class="box-box">
				<!-- <view class="bb-top"  v-if="shopinfo == 0" @click="shopinfo = 1">
					<view class="bb-bai">
						<text>商户登录</text>
					</view>
					<view class="bb-hui">
							<text>员工登录</text>
					</view>
				</view>
				<view class="bb-top"  v-if="shopinfo == 1" @click="shopinfo = 0">
					<view class="bb-hui">
						<text>商户登录</text>
					</view>
					<view class="bb-bai">
							<text>员工登录</text>
					</view>
				</view> -->
				<view class="bb-top">
					<view class="bb-bai" :class="{'shendu' : shopinfo == 0}" @click="shopinfo = 0">
						<text>商户登录</text>
					</view>
					<!--					<view class="bb-bai" :class="{'shendu' : shopinfo == 1}" @click="shopinfo = 1">-->
					<!--						<text>官方登录</text>-->
					<!--					</view>-->
					<view class="bb-bai" :class="{'shendu' : shopinfo == 2}" @click="shopinfo = 2">
						<text>员工登录</text>
					</view>
					<view class="bb-bai" :class="{'shendu' : shopinfo == 3}" @click="shopinfo = 3">
						<text>业务登录</text>
					</view>
					<view class="bb-bai" :class="{'shendu' : shopinfo == 4}" @click="shopinfo = 4">
						<text>合作商登录</text>
					</view>

				</view>
				<view class="bb-baibox">
					<view class="inputs" v-if="activeid == 0">
						<input type="text" v-model="tel" placeholder="请输入手机号">
					</view>
					<view class="inputs" v-if="activeid == 1">
						<input type="text" v-model="tel" placeholder="请输入手机号">
					</view>
					<view class="inputs" style="justify-content: space-between; " v-if="activeid == 1">
						<input type="text" v-model="yzm" placeholder="请输入验证码">
						<button class="jsq" :disabled="isButtonDisabled"
							@click="sendVerificationCode">{{ buttonText }}</button>
					</view>
					<view class="inputs" v-if="activeid == 0">
						<input type="password" v-model="wrod" placeholder="请输入密码">
					</view>
					<view class="twotap">
						<view :class="{'blck':activeid == index}" v-for="(item,index) in qh " :key="index"
							@click="getxz(index)">{{item.text}}</view>
						<view @click="getxz1()">忘记密码</view>
					</view>
					<view class="anniulanan" @click="getlogin()">
						<text>登录</text>
					</view>
				</view>
			</view>

		</view>


		<view class="dbottom">
			<view class="axquan" v-if="yd == false" @click="yd = true"></view>
			<view class="axquanl" v-if="yd == true" @click="yd = false">
				<uni-icons type="checkmarkempty" color="#ffffff" size="14"></uni-icons>
			</view>
			<text>请您认真阅读</text>
			<text class="lan">《用户协议》</text>
			<text>和</text>
			<text class="lan">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	import request from '../../utils/request';
	import {
		setToken,
		setTenantId,
		getToken,
		getTenantId,
		setUserInfo
	} from '@/utils/auth.js'
	import {
		getTenantIdByPhone,
		getTenantIdByWebsite
	} from '@/api/login.js'
	export default {
		data() {
			return {
				phoneNumber: "",
				shopinfo: 0,
				yd: false,
				isButtonDisabled: false,
				countdownTime: 60,
				qh: [{
						text: '密码登录'
					},
					{
						text: '验证码登录'
					},
					// {text:'忘记密码'}
				],
				tel: '',
				yzm: '',
				wrod: '',
				sysCode: 'jiance',
				activeid: 0,
				tenantCode: 'lanan'
			}
		},
		mounted() {
			getTenantIdByWebsite(this.tenantCode).then(res => {
				const tenantId = res.data;
				if (tenantId && tenantId >= 0) {
					setTenantId(tenantId)
				}
			})
		},
		onLoad() {

		},
		computed: {
			buttonText() {
				if (this.isButtonDisabled) {
					return `${this.countdownTime}s 后重新发送`;
				} else {
					return "获取验证码";
				}
			},
		},
		methods: {
			//登录请求
			getlogin() {
				if (this.yd == false) {
					uni.showToast({
						title: '请仔细阅读用户协议与隐私政策！',
						icon: 'none'
					})
					return
				}
				if (this.tel == '') {
					uni.showToast({
						title: '输入框不能为空！',
						icon: 'none'
					})
					return
				}
				getTenantIdByPhone(this.tel).then(res => {
					if (res.code === 200 && res.data && res.data.length > 0) {
						uni.showActionSheet({
							itemList: res.data.map(m => m.name),
							success: ({
								tapIndex
							}) => {
								console.log('选择的租户id', res.data[tapIndex].id);
								setTenantId(res.data[tapIndex].id)
								loginFun()
							}
						})
					} else {
						//没有可选租户
						uni.showToast({
							title: '抱歉，您没有权限登录!',
							icon: 'none'
						})
					}
				})
				const loginFun = () => {
					if (this.activeid == 0) {
						const data = {
							username: this.tel,
							password: this.wrod,
							type: 0,
							sysCode: this.sysCode,
							tenantId: getTenantId()
						}
						request({
							url: '/company/staff/loginApp',
							method: 'post',
							data: data
						}).then((res) => {
							if (res.code == 200) {
								setToken(res.data.loginResult.accessToken)
								setUserInfo(res.data.userinfo)
								this.$startSocketConnect(res.data.userinfo.id)
								uni.setStorageSync('userId', res.data.userinfo.id)
								this.userinfo()
							}
						})

					}
					if (this.activeid == 1) {
						const data = {
							phone: this.tel,
							code: this.yzm,
							type: 1
						}
						request({
							url: '/rescue/loginApp',
							method: 'post',
							data: data
						}).then((res) => {
							if (res.code == 200) {
								setToken(res.data.accessToken)
								this.userinfo()
							}
						})
					}
				}
			},
			userinfo() {
				uni.setStorageSync("chooseIdx", this.shopinfo)
				if (this.shopinfo == 0) {
					uni.showLoading({
						title: '加载中'
					});
					request({
						url: '/getJcPartnerInfo',
						method: 'get',

					}).then((res) => {
						if (res.code == 500) {
							uni.showToast({
								title: "信息有误"
							})
							return
						}
						if (res.code == 200) {
							if (res.data.shop == true) {
								uni.setStorageSync('userinfo', res.data.user)
								uni.setStorageSync('roleSelect', 'shop')
								uni.hideLoading();
								this.$startSocketConnect(res.data.user.id)
								uni.setStorageSync('userId', res.data.user.id)
								this.goselect()
							} else {
								uni.showToast({
									title: '您不是商户，请选择员工登录',
									icon: 'none'
								})
								return
							}
						}
					})

				}
				if (this.shopinfo == 2) {
					uni.setStorageSync('roleSelect', 'worker')
					uni.redirectTo({
						url: '/pages/staff/staff'
					})
					return
				}
				if (this.shopinfo == 3 || this.shopinfo == 4) {
					uni.setStorageSync('roleSelect', 'businessManager')
					uni.redirectTo({
						url: '/pages/businessManager/businessManager'
					})
					return
				}
				if (this.shopinfo == 1) {
					request({
						url: '/rescue/getJcgfInfo',
						method: 'get',

					}).then((res) => {
						if (res.code == 200) {
							// uni.setStorageSync('staffinfo',res.user)
							// uni.setStorageSync('partnerId',res.partnerInfo.partnerId)
							uni.setStorageSync('roleSelect', 'jcgf')
							uni.setStorageSync('userinfo', res.user)
							uni.hideLoading();
							uni.redirectTo({
								url: '/pages/authority/authority'
							})
						}
					})

				}


			},
			getxz(index) {
				this.activeid = index

			},
			getxz1() {

				uni.navigateTo({
					url: '/pages/Login/modify'
				})

			},
			goregister() {
				uni.navigateTo({
					url: '/pages/login/register'
				})
			},
			goforgot() {
				uni.navigateTo({
					url: '/pages/login/forgot'
				})
			},
			goselect() {
				uni.reLaunch({
					url: '/pages/index/newindex'
				})
			},
			// 验证码方法
			sendVerificationCode() {
				let reg = /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/;
				if (!reg.test(this.tel)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的11位手机号'
					})
					this.tel = '';
					return false;
				}
				if (this.isButtonDisabled) {
					return; // 如果按钮已禁用，则直接返回
				}
				const data = {
					phone: this.tel,
				}
				request({
					url: '/loginSmsCode',
					method: 'post',
					params: data
				}).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: '验证码已发送 请注意查收',
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '网络不佳请稍后再试',
							icon: 'none'
						})
					}
				})


				this.disableButton(); // 禁用按钮
				this.startCountdown(); // 启动倒计时

			},
			disableButton() {
				this.isButtonDisabled = true;
			},
			enableButton() {
				this.isButtonDisabled = false;
			},
			startCountdown() {
				let countdown = setInterval(() => {
					this.countdownTime--;
					if (this.countdownTime === 0) {
						clearInterval(countdown);
						this.enableButton(); // 启用按钮
					}
				}, 1000);
			},
		}
	}
</script>

<style scoped lang="scss">
	.jsq {
		border: none !important;
		font-size: 12px;
		height: 30px;
		font-weight: 400;
		color: #0078FF;
	}

	button {
		border: none !important;
		background: none !important;
		margin: 0px;
		display: block;
	}

	button:focus {
		color: #0078FF;
		outline: none;
		box-shadow: none;
	}

	.content {
		box-sizing: border-box;
		background: linear-gradient(180deg, rgba(13, 46, 141, 0.2) 0%, rgba(255, 255, 255, 0) 100%);
	}

	.top {
		box-sizing: border-box;
		padding: 0px 27px;
		padding-top: 100px;
		// background-color:lightseagreen;
		background-image: url('../../static/detection/loginbj.png');
		background-size: cover;
		// background-position: center;
		width: 100%;
		height: calc(100vh);
	}

	.touxiang {
		width: 88px;
		height: 88px;
		border-radius: 10px;
		background: #D7D7D7;
		margin: 0 auto;
		margin-bottom: 40px;
		overflow: hidden;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.twotap {
		margin-top: 10px;
		width: 100%;
		display: flex;
		align-items: center;

		view {
			font-size: 12px;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #666666;
			margin-right: 38.5px;
		}
	}

	.blck {
		color: #333333 !important;
		font-weight: bold !important;
	}

	.inputs {
		height: 54px;
		box-sizing: border-box;
		padding: 16px 0px;
		display: flex;
		align-items: center;
		border-bottom: 2px solid #E8E8E8;
		// margin-top: 20px;
	}

	.wjworb {
		margin-top: 5px;
		width: 100%;
		display: flex;
		font-size: 12px;
		color: #0078FF;
		justify-content: flex-end;
		align-items: center;

	}

	.dl {
		width: 90%;
		height: 44px;
		background: linear-gradient(-46deg, #0853C4 0%, #4282D8 80%);
		border-radius: 5px;
		margin: 0 auto;
		font-size: 16px;
		color: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 22px;
	}

	.hging {
		width: 100%;
		text-align: center;
		font-size: 14px;
		font-weight: 400;
		color: #636363;
		margin-top: 20px;
	}

	.wx {
		width: 38px;
		height: 38px;
		overflow: hidden;
		box-sizing: border-box;
		margin: 0px auto;
		margin-top: 20px;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.hui {
		font-size: 10px;
		width: 100%;
		text-align: center;
		font-weight: 400;
		color: #666666;
	}

	.dbottom {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		text-align: center;
		font-size: 12px;
		color: #333333;
		margin-top: 40px;
		position: fixed;
		bottom: 25px;
	}

	.lan {
		color: #0D2E8D;
	}

	.box-box {
		//width: 95%;
		margin: 20px auto;
		border-radius: 8px;
		overflow: hidden;
	}

	.bb-top {
		height: 55px;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #FFFFFF;
	}

	.bb-bai {
		//width: 33%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 17px;
		font-weight: bold;
		background: #FFFFFF;
		// color: #0D2E8D;
		color: #B5BACA;
	}

	.shendu {
		color: #0D2E8D !important;
	}

	.bb-hui {
		height: 100%;
		width: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #B5BACA;
		font-size: 17px;
		font-weight: 400;
		color: #FFFFFF;
	}

	.bb-baibox {
		box-sizing: border-box;
		width: 100%;
		padding: 15px;
		background-color: white;

	}

	.anniulanan {
		width: 90%;
		height: 42px;
		margin: 20px auto;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 50px;
		background-color: #0D2E8D;
		color: white;
	}

	.dalan {
		font-size: 25px;
		font-weight: bold;
		color: #0D2E8D;
		margin-left: 10px;
	}

	.axquan {
		width: 14px;
		height: 14px;
		border-radius: 50%;
		border: 1px solid #AAAAAA;
		margin-right: 5px;
	}

	.axquanl {
		width: 14px;
		height: 14px;
		border-radius: 50%;
		border: 1px solid #AAAAAA;
		margin-right: 5px;
		background-color: #0D2E8D;
	}
</style>