<template>
	<view class="content">
		<!-- #ifdef APP-PLUS || H5 -->
		<view class="top">
			<view class="touxiang">
				<image src="../../static/xinlogo.png" mode=""></image>
			</view>
			<view class="twotap">
				<view :class="{'blck':activeid == index}" v-for="(item,index) in qh " :key="index"
					@click="getxz(index)">{{item.text}}</view>
			</view>
			<view class="inputs" v-if="activeid == 0 || activeid == 2 ">
				<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 || activeid == 2 ">
				<input type="password" v-model="wrod" placeholder="请输入密码">
			</view>
			<view class="wjworb" v-if="activeid == 0 || activeid == 2" @click="goforgot()">
				<view class="">忘记密码</view>
			</view>
		</view>
		<view class="dl" @click="getlogin()">
			<text>登 录</text>
		</view>
		<!-- #endif -->
		<view class="dbottom">
			<view class="quanzi" v-if="qindex == false " @click="qindex =! qindex"></view>
			<view class="lanquanzi" v-if="qindex == true" @click="qindex =! qindex"><u-icon name="checkbox-mark"
					color="#ffffff" size="14"></u-icon></view>
			<text>请仔细阅读</text>
			<text class="lan">《用户协议》</text>
			<text>和</text>
			<text class="lan">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	import request from '@/utils/request';
	import {
		loginApp
	} from '@/api/login';
	import {
		setToken,
		setTenantId,
    getTenantId,
    setUserInfo,
    setJSONData,
    setStrData
	} from '@/utils/auth'
	import {
		login,
		getInfo,
		getTenantIdByPhone
	} from '@/api/login'
	export default {
		data() {
			return {
			  //系统标识
			  sysCode:"weixiu",
				phoneNumber: "",
				baseUrl: this.$baseUrl,
				isButtonDisabled: false,
				countdownTime: 60,
				qh: [{
						text: '密码登录'
					},
					{
						text: '验证码登录'
					},
				],
				qindex: false,
				tel: '',
				yzm: '',
				wrod: '',
				activeid: 0
			}
		},
		computed: {
			buttonText() {
				if (this.isButtonDisabled) {
					return `${this.countdownTime}s 后重新发送`;
				} else {
					return "获取验证码";
				}
			},
		},
		mounted() {},
		methods: {
			wxlogin() {
				uni.login({
					provider: 'weixin',
					success(res) {

						if (res.code) {
							uni.setStorageSync('c1', res.code);
							console.log('wxlogin:', res.code);
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			//登录请求
			getlogin() {
				if (this.qindex != true) {
					uni.showToast({
						icon: "none",
						title: "请阅读并勾选用户协议",
						duration: 2000
					})
					return;
				}
				if (this.tel == '') {
					uni.showToast({
						title: '手机号不能为空！',
						icon: 'none'
					})
					return
				}
        const loginFun = () => {
          let data;
          if (this.activeid == 1) {
            //验证码
            data = {
              phone: this.tel,
              code: this.yzm,
              type: 1,
              sysCode:this.sysCode,
              tenantId:getTenantId()
            }
          } else {
            //密码登录
            data = {
              username: this.tel,
              password: this.wrod,
              type: 0,
              sysCode:this.sysCode,
              tenantId:getTenantId()
            }
          }
          request({
            url: '/admin-api/company/staff/loginApp',
            method: 'post',
            data: data
          }).then((res) => {
            console.log(res)
            if (res.code == 200) {
              uni.showToast({
                title: '登录成功',
                icon: 'none'
              })
              setToken(res.data.loginResult.accessToken)
              setUserInfo(res.data.userinfo)
              setStrData("userId",res.data.userinfo.id)
              if(res.data.userinfo.roleCodes.includes("repair_staff")){
                //维修工角色，需要再查一下是否是班组长
                this.getIdLeader()
              }
              this.getStaffInfo(res.data.userinfo.id)
              if (res.data.userinfo.roleCodes.includes("repair_warehouse")){
                //跳转首页
                uni.navigateTo({
                  url: '/pages-warehouse/home/home'
                })
              } else {
                //跳转首页
                uni.navigateTo({
                  url: '/pages-home/home/home'
                })
              }

            }else{
              uni.showToast({
                title: res.msg,
                icon: 'none'
              })
            }
          })
        }
        //查所有可选租户
				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
							}) => {
								setTenantId(res.data[tapIndex].id)
								loginFun()
							}
						})
					} else {
						//没有可选租户
            uni.showToast({
              title: '抱歉，您没有权限登录!',
              icon: 'none'
            })
					}
				})
			},
      /**
       * 查员工个人信息
       */
      getStaffInfo(userId){
        request({
          url: '/admin-api/company/staff/getById',
          method: 'get',
          params: {id:userId}
        }).then((res) => {
          console.log(res)
          if (res.code == 200) {
            setJSONData("staffInfo",res.data)
          }
        })
      },
      /**
       * 查是否班组长
       */
      getIdLeader(){
        request({
          url: '/admin-api/repair/worker/getIfLeader',
          method: 'get'
        }).then((res) => {
          console.log(res)
          if (res.code == 200) {
            setStrData("ifLeader",res.data)
          }
        })
      },
			getxz(index) {
				this.activeid = index
			},
			goregister() {
				uni.navigateTo({
					url: '/pages/login/register'
				})
			},
			goforgot() {
				uni.navigateTo({
					url: '/pages/login/forgot'
				})
			},
			// 验证码方法
			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,
          sysCode:this.sysCode,
				}
				request({
					url: '/app-api/company/staff/loginSmsCode',
					method: 'post',
					params: data
				}).then((res) => {
					console.log('验证码', res);
					if (res.code == 200) {
            this.disableButton(); // 禁用按钮
            this.startCountdown(); // 启动倒计时
						uni.showToast({
							title: '验证码已发送 请注意查收',
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '网络不佳请稍后再试',
							icon: 'none'
						})
					}
				})
			},
			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: 14px;
		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;
	}

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

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

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

	.twotap {
		width: 100%;
		display: flex;
		align-items: center;

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

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

	.inputs {
		box-sizing: border-box;
		padding: 16px 0px;
		display: flex;
		align-items: center;
		border-bottom: 2px solid #E8E8E8;
		margin-top: 20px;
	}
  .inputs input{
    width: 100%;
  }

  .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 {
		width: 100%;
		text-align: center;
		font-size: 12px;
		color: #333333;
		margin-top: 40px;
		position: fixed;
		bottom: 25px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.quanzi {
		width: 14px;
		height: 14px;
		border: 1px solid #666666;
		border-radius: 50%;
		margin-right: 10px;
	}

	.lanquanzi {
		width: 14px;
		height: 14px;
		border: 1px solid #0078FF;
		border-radius: 50%;
		margin-right: 10px;
		background: #0078FF;
		color: white;
	}

	.lan {
		color: #0078FF;
	}

	.jiuzhong {
		width: 90%;
		margin: 10px auto;
		display: flex;
		align-items: center;
	}

	.dist {
		display: flex;
		align-items: center;
		margin: 0px 10px;
	}

	.d-zi {
		font-size: 14px;
	}

	.d-qian {
		width: 14px;
		height: 14px;
		margin-right: 5px;
		overflow: hidden;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #666666;
		color: white;
	}

	.d-qian-lan {
		width: 14px;
		height: 14px;
		margin-right: 5px;
		overflow: hidden;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #0078FF;
		border: 1px solid #0078FF;
		color: white;
	}
</style>
