<template>
	<view class="content">
		<view class="status_bar"></view>
		<view class="logo">
			<image src="~@/static/logo.png" mode="aspectFit"></image>
		</view>
		<view class="title">
			<text v-if="mine_code == 'ningdongyunying'">E信管理</text>
			<text v-if="mine_code == 'ningmeijituan'">智慧宁煤</text>
			<text v-if="mine_code == 'zaoquan'">枣泉煤矿</text>
			<text v-if="mine_code == 'qingshuiying'">清水营煤矿</text>
			<text v-if="mine_code == 'wuyegongsi'">智慧物业</text>
			<text v-if="mine_code == 'jinjiaqu' || mine_code == 'jinjiaqu_neiwang'">金家渠煤矿</text>
			<text v-if="mine_code == 'yangchangwan'">羊场湾煤矿</text>
			<text v-if="mine_code == 'shicaocun'">石槽村煤矿</text>
			<text v-if="mine_code == 'jinfeng' || mine_code == 'jinfeng_neiwang'">金凤煤矿</text>
		</view>
		<view class="login_box">
			<view class="item">
				<view class="name">工号</view>
				<view class="input">
					<input type="text" value="" v-model="user.username" />
				</view>
				<view class="icon icon_avatar"></view>
			</view>
			<view class="item">
				<view class="name">密码</view>
				<view class="input">
					<input type="password" value="" v-model="user.password" />
				</view>
				<view class="icon icon_password"></view>
			</view>
		</view>

		<view class="tips" @click="go_forget_password()">
			<text>忘记密码？点击这里重置！</text>
		</view>


		<view class="login_btn" @click="loginBtn()">
			<text>登录</text>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				mine_code:"",
				user: {
					username: '',
					password: ''
				}
			};
		},
		onLoad() {
			this.mine_code = uni.getStorageSync('mine_code')
		},
		methods: {
			loginBtn() {
				const user = this.user
				uni.setStorageSync('login_password', this.user.password)
				// 判断是否为空
				if (user.username == '' || user.password == '') {
					uni.showToast({
						icon: "none",
						title: "请填写用户信息"
					})
					return false
				}
				this.$api.login({
					username: user.username,
					password: user.password
				}).then((res) => {
					if (res.data.code === 0) {

						// console.log(res.data.data.token_type +''+res.data.data.access_token)
						console.log(res.data.data.user.mobile)

						uni.setStorageSync('Authorization', res.data.data.access_token)
						uni.setStorageSync('token_type', res.data.data.token_type)
						// 存储用户基本信息
						uni.setStorageSync('user', res.data.data.user);
						uni.setStorageSync('people_code', res.data.data.user.people_code);
						// 存储电话号码
						uni.setStorageSync('mobile', res.data.data.user.mobile);

						setTimeout(function() {
							uni.showToast({
								icon: "none",
								title: "登录成功"
							})
							// 清除信息平台的登录历史token
							uni.setStorageSync('ningmei_token',' ')
							// 登录成功跳转首页
							uni.switchTab({
								url:"../../tabbar/index/index",
								success: () => {
									//#ifdef H5
									window.location.reload();
									//#endif
								}
							})
						}, 500)

						// #ifdef APP-PLUS
						plus.runtime.restart();
						// #endif

					} else if (res.data.code === 1001) {
						uni.showToast({
							icon: "none",
							title: "用户不存在"
						})
					} else if (res.data.code === 1002) {
						uni.showToast({
							icon: "none",
							title: "密码错误"
						})
					} else {
						return false
					}
				}).catch((err) => {
					console.log('request fail', err);
					uni.showToast({
						icon: "none",
						title: err
					})
				})

			},
			
			// 重置密码
			go_forget_password(){
				uni.navigateTo({
					url:"../forget-password/forget-password"
				})
			}
		}

	}
</script>

<style lang="scss">
	page {}

	.content {
		width: 749rpx;
		height: 100vh;
		background-image: url(~@/static/login.png);
		background-size: cover;
		background-position: center center;
		background-repeat: no-repeat;

		.logo {
			width: 344rpx;
			height: 350rpx;
			margin: 0 auto;
			padding-top: 100rpx;

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

		.title {
			margin: 0 auto;
			margin-top: 32rpx;
			width: 288rpx;
			height: 47rpx;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 47rpx;
			text-align: center;
		}

		.login_box {
			padding-top: 40px;

			.item {
				margin: 0 auto;
				margin-bottom: 20px;
				width: 500rpx;
				height: 45px;
				background: #FFFFFF;
				border-radius: 25px;
				padding-left: 20px;
				padding-right: 20px;
				box-sizing: border-box;
				display: flex;
				align-items: center;

				.name {
					width: 80rpx;
					font-size: 16px;
					color: #009FE8;
					line-height: 45px;
				}

				.input {
					width: 280rpx;

					input {
						height: 45px;
						color: #666;
					}
				}

				.icon {}

				.icon_avatar {
					margin-left: 10rpx;
					width: 25px;
					height: 25px;
					background-image: url(./icon/Avatar.png);
					background-size: cover;
					background-repeat: no-repeat;
				}

				.icon_password {
					margin-left: 18rpx;
					width: 25px;
					height: 28px;
					background-image: url(./icon/Password.png);
					background-size: cover;
					background-repeat: no-repeat;
				}
			}
		}

		.tips {
			margin: 0 auto;
			margin-top: 25px;
			text-align: center;
			font-size: 14px;
			color: #ECF0F1;
			line-height: 40px;
			text-align: center;

		}

		.login_btn {
			margin: 0 auto;
			margin-top: 40px;
			width: 200px;
			height: 50px;
			background: rgba(100, 220, 255, 0.8);
			border-radius: 25px;
			text-align: center;
			line-height: 50px;

			text {
				font-size: 18px;
				color: #FFFFFF;
				letter-spacing: 4px;
			}
		}
	}
</style>
