<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="mobile">
			<el-input v-model="form.mobile" prefix-icon="el-icon-iphone" clearable :placeholder="$t('login.mobilePlaceholder')"></el-input>
		</el-form-item>
		<el-form-item prop="verify_code"  style="margin-bottom: 35px;">
			<div class="login-msg-yzm">
				<el-input v-model="form.verify_code" prefix-icon="el-icon-unlock" clearable :placeholder="$t('login.smsPlaceholder')"></el-input>
				<el-button @click="getYzm" :disabled="disabled">{{this.$t('login.smsGet')}}<span v-if="disabled"> ({{time}})</span></el-button>
			</div>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="isLogin" @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
		<div class="login-reg">
			{{$t('login.noAccount')}} <router-link to="/user_register">{{$t('login.createAccount')}}</router-link>
		</div>
	</el-form>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					mobile: "",
					verify_code: "",
				},
				rules: {
					mobile: [
						{required: true, message: this.$t('login.mobileError')}
					],
					verify_code: [
						{required: true, message: this.$t('login.smsError')}
					]
				},
				disabled: false,
				time: 0,
				isLogin: false,
			}
		},
		mounted() {

		},
		methods: {
			async getYzm(){
				const validate = await this.$refs.loginForm.validateField("mobile").catch(() => {});
				if(!validate){ return false }

				const res = await this.$API.user.verifyCode.post({mobile: this.form.mobile});
				if(res.code == 200){
					this.$message.success(this.$t('login.smsSent'))
					this.disabled = true
					this.time = 60
					const t = setInterval(() => {
						this.time -= 1
						if (this.time < 1) {
							clearInterval(t)
							this.disabled = false
							this.time = 0
						}
					}, 1000);
				}
			},
			async login(){
				const validate = await this.$refs.loginForm.validate().catch(() => {});
				if(!validate){ return false }

				//获取token
				const user = await this.$API.auth.phoneLogin.post(this.form);
				if (user.code == 200) {
					this.$TOOL.cookie.set("TOKEN", user.data.token, {
						expires: this.form.autologin ? 24 * 60 * 60 : 0
					})
					this.$TOOL.data.set("USER_INFO", user.data.user)
					if(user.data.user.avatar!=''){
						this.$store.commit("SET_LOGIN_AVATAR", user.data.user.avatar);
					}
					this.$store.commit("SET_LOGIN_NAME", user.data.user.name);
				} else {
					this.islogin = false
					return false
				}

				if(user.data.user.company_id === 0){
					this.$router.replace({
						path: '/maintenance'
					})
					this.islogin = false
					return
				}

				//获取菜单
				const menu = await this.$API.system.menu.myMenus.get();
				if (menu.code == 200) {
					if (menu.data.menu.length == 0) {
						this.islogin = false
						await this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
							type: 'error',
							center: true
						})
						return false
					}
					this.$TOOL.data.set("MENU", menu.data.menu)
					const data_auth = this.$TOOL.objCopy(menu.data.permissions);
					data_auth.auth = this.$TOOL.authPermissions(data_auth.data_permissions);
					this.$TOOL.data.set("PERMISSIONS", data_auth)
					this.$TOOL.data.set("DASHBOARDGRID", menu.data.dashboardGrid)
				} else {
					this.islogin = false
					this.$message.warning(menu.message)
					return false
				}

				this.$router.replace({
					path: '/'
				})
				this.islogin = false
			}
		}
	}
</script>

<style>

</style>
