<template>
	<dialog-base ref="baseDialog" title="账号登录">
		<template v-if="currentUser.nickname">
			<view class="user-info-container uni-row">
				<view class="user-avatar uni-row"><text class="label">{{ currentUser.nickname }}</text></view>
				<view class="user-info">
					<view class="nickname"><text class="label">{{ currentUser.nickname }}</text></view>
					<view class="current-process"><text class="label">热处理</text></view>
				</view>
			</view>
		</template>
		<template v-else>
			<input class="input-control" v-model="currentUser.userName" placeholder="员工编码" />
		</template>
		<input class="input-control" :password="true" v-model="currentUser.password" placeholder='密码' />
		<view class="login-btn uni-row" @click="handleLogin"><text class="label">登录</text></view>
	</dialog-base>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		getUserInfo
	} from '@/api/login/index.js'
	import { getUserProcess } from '@/api/process/process.js'
	import baseURL from '@/api/base/path.js'
	import { store } from '@/store/index.js'
	// 对话框
	const baseDialog = ref(null)
	const currentUser = ref({})
	const errorMsg = ref('用户名或密码错误')
	const successMsg = ref('登录成功')
	const userInfo = ref({})
	const user = ref([])
	// const { proxy } = getCurrentInstance()

	const open = (data) => {
		currentUser.value = data
		// console.log(dialog.value)
		baseDialog.value.open()
	}
	 const handleLogin = () => {
		 uni.showLoading({
		 	title: '加载中'
		 });
		 setTimeout(function(){
			 uni.hideLoading();
		 },3000)
		uni.request({
			method: 'POST',
			url: baseURL + '/login',
			data: {
				username: currentUser.value.userName,
				password: currentUser.value.password
			},
			success: (res) => {
				if (res.data.code === 200) {
					uni.showToast({
						title: successMsg.value,
						icon: 'success',
						duration: 2000
					});
					// 保存token
					uni.setStorageSync('token', res.data.token);
					uni.setStorage({
						key: currentUser.value.userName,
						data: currentUser.value.password,
					});
					// 获取员工工序信息
					getUserProcess().then(res => {
						store.userProcessInfo = res.data;
					})
					// 获取员工信息存入store,并跳转到控制面板
					getUserInfo(currentUser.value).then((res) => {
						if (res.code == 200) {
							userInfo.value = res.data;
							user.value.push(userInfo.value);
							store.userInfo = res.data
							console.log(store.userInfo)
							uni.redirectTo({
								url: '/pages/dashboard/index'
							});
						}
					})
				} else {
					uni.showToast({
						title: errorMsg.value,
						icon: 'error',
						//显示持续时间为 2秒
						duration: 2000
					})
				}
			}
		})
	}

	/** 暴露给父组件的方法 */
	defineExpose({
		open
	})
</script>

<style lang="scss">
	.dialog-body {
		.user-info-container {
			margin-top: 32rpx;
			align-items: center;

			.user-avatar {
				width: 120rpx;
				height: 120rpx;
				justify-content: center;
				align-items: center;
				border-radius: 120rpx;
				background-color: #F5F5F5;

				.label {
					font-size: 72rpx;
				}
			}

			.user-info {
				flex: 1;
				padding-left: 16rpx;

				.nickname {
					margin-bottom: 8rpx;

					.label {
						font-size: 40rpx;
					}
				}

				.current-process {
					.label {
						font-size: 32rpx;
						color: #666666;
					}
				}
			}
		}

		.input-control {
			border: 1px solid #666666;
			padding: 16rpx 24rpx;
			font-size: 32rpx;
			margin-top: 40rpx;
		}

		.login-btn {
			// background-color: #999999;
			background-color: #1684fc;
			margin-top: 32rpx;
			justify-content: center;
			padding: 16rpx 0;

			.label {
				font-size: 40rpx;
				color: #FFFFFF;
			}
		}
	}
</style>