<template>
	<view class="content flex items-center justify-center px-4">

		<div class="flex items-center justify-center mb-2 fixed top-4 left-4">

			<wd-transition :duration="300" :show="true" name="fade-left">
				<view class="flex-1 overflow-hidden ">
					<wd-img :width="34" :height="34" :src="webInfo.logo" mode="aspectFill" />
				</view>
			</wd-transition>

			<wd-transition :duration="400" :show="true" name="fade-left">
				<text class="flex px-2 text-center justify-center">
					{{ webInfo.name }}
				</text>
			</wd-transition>

		</div>


		<wd-form class="mb-24" ref="form" :model="model">

			<view class="rounded-md overflow-hidden">
				<wd-cell-group border>
					<wd-transition :duration="400" :show="true" name="fade-left">
						<wd-input label="用户名" label-width="100px" prop="username" clearable v-model="model.username"
							placeholder="请输入用户名" :rules="[{ required: true, message: '请填写用户名' }]" />
					</wd-transition>

					<wd-transition :duration="450" :show="true" name="fade-left">
						<wd-input label="密码" label-width="100px" prop="password" show-password clearable v-model="model.password"
							placeholder="请输入密码" :rules="[{ required: true, message: '请填写密码' }]" />
					</wd-transition>

				</wd-cell-group>
			</view>

			<view class="footer mt-4 px-6">
				<wd-transition :duration="500" :show="true" name="fade-left">
					<wd-button :loading="loading" type="primary" size="medium" @click="handleSubmit" block>登录</wd-button>
				</wd-transition>
			</view>
		</wd-form>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import user from '@/api/modules/user.js'
import systemApi from '@/api/modules/system.js'
import utils from '@/utils/utils.js'
import md5 from 'js-md5';
uni.clearStorage(); // 预防本地数据错误

const webInfo = ref({})

const getWebInfo = () => {
	systemApi.getSetting().then(res => {
		webInfo.value = res.data
	})
}

getWebInfo()


const model = ref({
	username: "",
	password: "",
})

const loading = ref(false)
const form = ref(null)

// if (token) {
// 	const user_info = uni.getStorageSync("user_info");
// 	if (user_info.type == 1) {
// 		utils.toUrl("/store/index/index")
// 	}
// } else {
// 	utils.toUrl("/pages/login/index")
// }

function handleSubmit() {
	loading.value = true
	form.value
		.validate()
		.then(({ valid, errors }) => {
			if (valid) {

				user.login({
					...model.value,
					password: md5(model.value.password)
				}).then(res => {

					if (res.code == 0) {
						uni.setStorageSync("token", res.data.access_token);
						uni.setStorageSync("user_info", res.data.user_info);

						uni.showToast({
							icon: "none",
							title: "登录成功",
						});

						if (res.data.user_info.type == 1) {
							utils.toUrl("/store/index/index", "redirectTo")
							uni.setStorageSync("store_info", res.data.user_info);
						} else if (res.data.user_info.type == 3) {
							uni.setStorageSync("mall_token", res.data.access_token);
							uni.setStorageSync("mall_info", res.data.user_info);
							utils.toUrl("/mall/index/index", "redirectTo")
						} else {
							uni.showToast({
								title: '登录平台开发中 ···',
								icon: 'none'
							})
						}
						loading.value = false
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						loading.value = false
					}

					loading.value = false
				})
			} else {
				loading.value = false
			}
		})
		.catch((error) => {
			console.log(error, 'error')
			loading.value = false
		})
}
</script>

<style>
.content {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	min-height: 100vh;
}

.logo {
	height: 200rpx;
	width: 200rpx;
	margin-top: 200rpx;
	margin-left: auto;
	margin-right: auto;
	margin-bottom: 50rpx;
}

.text-area {
	display: flex;
	justify-content: center;
}

.title {
	font-size: 36rpx;
	color: #8f8f94;
}
</style>
