<template>
	<div id="login">
		<div class="header">
			<div class="header-img">
				<img @click="goHome" src="@/assets/image/sailunLogo.png" alt="" />
				<img src="../assets/image/loginsailun.png" class="bamnar" alt="" />
			</div>
			<!-- <div>产品与服务</div>
      <div>项目案例</div> -->
			<div>帮助文档</div>
		</div>
		<div class="login-content">
			<div class="login-module">
				<div class="title">橡链云统一登录</div>
				<el-form :model="numberValidateForm" ref="numberValidateForm" label-width="80px" class="demo-ruleForm">
					<el-form-item label="账号" prop="number" :rules="[{ required: true, message: '账号不能为空' }]">
						<el-input v-model.trim="numberValidateForm.number" :spellcheck="false" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="密码" prop="pass" :rules="[{ required: true, message: '密码不能为空' }]">
						<el-input
							type="password"
							v-model.trim="numberValidateForm.pass"
							v-on:keyup.enter.native="submitForm('numberValidateForm')"
						></el-input>
					</el-form-item>
					<el-form-item prop="code" :rules="[{ required: true, message: '请输入图片验证码' }]">
						<div :style="{ display: 'flex', width: '200px', height: '42px', fontSize: '14px' }">
							<el-input
								placeholder="请输入验证码"
								v-model.trim="numberValidateForm.code"
								v-on:keyup.enter.native="submitForm('numberValidateForm')"
							></el-input>
							<img
								style="cursor: pointer; width: 80px; height: 32px; margin-left: 10px"
								@click="getCaptchaImage"
								:src="`data:image/jpg;base64,${img}`"
								alt=""
							/>
						</div>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="submitForm('numberValidateForm')" :loading="loading">登录</el-button>
					</el-form-item>
				</el-form>
			</div>
		</div>
		<div class="custom-shape-divider-bottom">
			<svg data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 120" preserveAspectRatio="none">
				<path
					d="M0,0V46.29c47.79,22.2,103.59,32.17,158,28,70.36-5.37,136.33-33.31,206.8-37.5C438.64,32.43,512.34,53.67,583,72.05c69.27,18,138.3,24.88,209.4,13.08,36.15-6,69.85-17.84,104.45-29.34C989.49,25,1113-14.29,1200,52.47V0Z"
					opacity=".25"
					class="shape-fill"
				></path>
				<path
					d="M0,0V15.81C13,36.92,27.64,56.86,47.69,72.05,99.41,111.27,165,111,224.58,91.58c31.15-10.15,60.09-26.07,89.67-39.8,40.92-19,84.73-46,130.83-49.67,36.26-2.85,70.9,9.42,98.6,31.56,31.77,25.39,62.32,62,103.63,73,40.44,10.79,81.35-6.69,119.13-24.28s75.16-39,116.92-43.05c59.73-5.85,113.28,22.88,168.9,38.84,30.2,8.66,59,6.17,87.09-7.5,22.43-10.89,48-26.93,60.65-49.24V0Z"
					opacity=".5"
					class="shape-fill"
				></path>
				<path
					d="M0,0V5.63C149.93,59,314.09,71.32,475.83,42.57c43-7.64,84.23-20.12,127.61-26.46,59-8.63,112.48,12.24,165.56,35.4C827.93,77.22,886,95.24,951.2,90c86.53-7,172.46-45.71,248.8-84.81V0Z"
					class="shape-fill"
				></path>
			</svg>
		</div>
	</div>
</template>
<script>
import cookie from "js-cookie"
import { postLogin, captchaImageReq, VcaptchaImageReq } from "@/utils/api/api"
import { setUid, setToken, CreateTenant, setName, setRole, setEmail } from "@/utils/auth"
import baseURL from "@/utils/app"

export default {
	name: "login",
	data() {
		return {
			numberValidateForm: {
				number: "",
				pass: "",
				code: ""
			},
			uuid: "",
			img: null,
			loading: false
		}
	},
	created() {
		sessionStorage.clear("replacement_token")
		// this.getVerifyCode();
		let cookies = cookie.get()
		for (const key in cookies) {
			cookie.remove(key, { path: "/", domain: baseURL.DomainName })
		}
		this.getCaptchaImage()
	},
	methods: {
		getCaptchaImage() {
			captchaImageReq()
				.then(result => {
					if (result.code === 200) {
						this.img = result.data.image
						this.uuid = result.data.uuid
					}
				})
				.catch(err => {
					console.log(err)
				})
		},
		submitForm(formName) {
			this.$refs[formName].validate(valid => {
				if (valid) {
					let data = {
						uuid: this.uuid,
						code: this.numberValidateForm.code
					}
					VcaptchaImageReq(data).then(result => {
						if (result.code === 200 && result.data) {
							this.login()
						} else {
							this.$message.warning(result.message)
							this.getCaptchaImage()
							this.numberValidateForm.code = ""
						}
					})
				} else {
					return false
				}
			})
		},
		resetForm(formName) {
			this.$refs[formName].resetFields()
		},
		// 登录
		login() {
			let data = {
				password: this.numberValidateForm.pass,
				ssoCode: "",
				userName: this.numberValidateForm.number
			}
			this.loading = true
			postLogin(data)
				.then(res => {
					this.loading = false
					if (res.code === 2004) {
						sessionStorage.setItem("replacement_token", res.message)
						this.$router.push({ name: "replacement" })
						return
					}
					if (res.code !== 200) {
						return
					}
					CreateTenant(res.data.isCreateTenant)
					setUid(res.data.userInfo.uid)
					setToken(res.data.jwtToken)
					setName(res.data.userInfo.name)
					setRole(res.data.userInfo.role)
					setEmail(res.data.userInfo.email)
					window.location.href = baseURL.VUE_APP_PDMS_PATH
				})
				.catch(() => {
					this.loading = false
				})
		},
		goHome() {
			this.$router.push({ path: "/" })
		}
		// 获取验证码
		// getVerifyCode() {
		//   getVerify().then((res) => {
		//     this.img=res;
		//   });
		// },
	}
}
</script>
<style scoped lang="scss">
@import "src/assets/styles/encapsulation";

#login {
	width: 100%;
	min-height: 800px;
	height: 100%;
	background: $primary-color;
	background-size: cover;
	position: relative;
	padding-top: 0.02px;

	.bamnar {
		position: absolute;
		top: 50%;
		margin-top: 320px;
		left: 100px;
		width: 660px;
		height: 320px !important;
	}

	.header {
		display: flex;
		height: 70px;
		align-items: center;
		position: absolute;
		font-size: 14px;
		background-color: rgba(0, 0, 0, 0);
		width: 100%;
		color: #fff;

		.header-img img {
			height: 40px;
		}

		div {
			margin-left: 50px;
			cursor: pointer;
		}
	}

	.login-content {
		align-items: center;
		height: calc(100% - 70px);
		padding-top: 220px;

		.loginImage {
			width: 500px;
			margin-left: 30px;
		}

		.login-module {
			padding: 30px;
			padding-right: 50px;
			width: 350px;
			background-color: rgba(255, 255, 255, 0.55);
			float: right;
			border-radius: 5px;
			display: flex;
			box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.18);
			padding-top: 120px;
			position: relative;
			margin-right: 100px;

			.el-button--small {
				width: 100%;
			}

			.el-form-item--small.el-form-item {
				margin-bottom: 25px;
			}

			.title {
				position: absolute;
				top: 50px;
				left: calc(50% - 60px);
				font-size: 22px;
				color: #fff;
				font-weight: bold;
			}
		}
	}

	.custom-shape-divider-bottom {
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		overflow: hidden;
		line-height: 0;
		transform: rotate(180deg);
	}

	.custom-shape-divider-bottom svg {
		position: relative;
		display: block;
		width: calc(100% + 1.3px);
		height: 73px;
	}

	.custom-shape-divider-bottom .shape-fill {
		fill: #ffffff21;
	}
}
</style>
