<template>
	<view class="loginPage">

		<view class="loginHeader">
			<view class="loginHeaderTitle">欢迎使用巡查上报系统</view>
		</view>
		<view class="loginBox">
			<u-field v-model="username" label-width=0 icon="account" placeholder="请填写登陆名">
			</u-field>
			<u-field v-model="password" label-width=0 icon="lock" password=true placeholder="请填写密码">
			</u-field>
		</view>

		<view class="loginFoot">
			<u-button :ripple="true" @click="loginBind" ripple-bg-color="#2979ff" type="primary">登陆</u-button>
		</view>
	</view>
</template>

<script>
	var util = require("../../utils/util.js");
	export default {
		data() {
			return {
				username: '',
				password: ''
			}
		},
		onLoad: function() {
			var user = uni.getStorageSync('userInfo');
			if (!util.isNull(user.access_token)) {
				util.request({
					api: 'worker/check-token',
					utilSuccess: function(res) {
						if (res.token_available) {

							uni.redirectTo({
								url: '/pages/index/index'
							});
						}
					},
					utilFail: function(res) {
						console.log(res)
					}
				});

			}

		},
		methods: {
			loginBind: function() {
				let that = this;

				if (that.username == "") {
					util.alert("请输入登陆账号");
					return false;
				}

				if (that.password == "") {
					util.alert("请输入密码");
					return false;
				}

				util.request({
					api: 'worker/login-by-username',
					method: 'POST',
					data: {
						'mobile': that.username,
						'password': that.password
					},
					utilSuccess: function(res) {
						console.log(res)
						uni.setStorageSync('userInfo', res);
						uni.redirectTo({
							url: '/pages/index/index'
						});
					},
					utilFail: function(res) {
						console.log(res)
						util.alert(res);
					}
				});
			}

		}
	}
</script>

<style>
	.loginPage {}

	.loginFoot {
		padding: 40rpx;
		margin-top: 100rpx;
	}

	.loginBox {
		padding: 120rpx 40rpx;
		background: #FFFFFF;
		margin-top: -50rpx;
		background: #FFFFFF;
		border-radius: 50rpx;
	}

	.loginHeaderTitle {
		display: flex;
		font-size: 40rpx;
		padding: 80rpx 0;
		color: #fff;
	}

	.loginHeader {
		padding: 80rpx 40rpx;
		background: #2979ff;
	}
</style>
