<template>
	<ch-flex justify="center" class="page login-page" items="center">
		<ch-flex class="login-win" items="center">
			<image class="login-img" src="@/assets/images/logo/ghlogo.png" mode="aspectFit"></image>
			<uni-forms ref="formRef" err-show-type="toast" :modelValue="form" :rules="rules">
				<!-- <uni-forms-item name="schoolId">
					<uni-data-select v-model="form.schoolId" :localdata="schoolList" prefixIcon="home-filled" placeholder="请选择学校">
					</uni-data-select>
				</uni-forms-item> -->
				<uni-forms-item name="studentno">
					<uni-easyinput v-model="form.studentno" placeholder="学生学号" prefixIcon="person-filled"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="studentpwd">
					<uni-easyinput v-model="form.studentpwd" type="password" placeholder="登录密码" prefixIcon="locked-filled"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item>
					<button type="primary" size="mini" :disabled="loading" @click="login">{{ loading ? '登 录 中...' : '登 录' }}</button>
				</uni-forms-item>
			</uni-forms>
		</ch-flex>
	</ch-flex>
</template>

<script setup>
import { ref, reactive, toRefs } from 'vue';
import useForm from '@/useModules/useForm.js';
import ChFlex from '@/components/ch-flex/ch-flex.vue';
import { useSystemStore } from '@/store/system.js';
import { useUserStore } from '@/store/user.js';
import { onLoad, onInit } from '@dcloudio/uni-app';
	
const defaultForm = {
	studentno: '',
	studentpwd: '',
	// schoolId: '',
}

const systemStore = useSystemStore();
const userStore = useUserStore();

const formRef = ref();

const { form, validateForm, resetForm } = useForm(formRef, defaultForm);

const data = reactive({
	loading: false,
	rules: {
		schoolId: {
			rules: [{ required: false, errorMessage: '请选择学校' }]
		},
		studentno: {
			rules: [{ required: true, errorMessage: '请输入学生编号' }]
		},
		studentpwd: {
			rules: [{ required: true, errorMessage: '请输入密码' }]
		}
	},
	schoolList: [
		{
			value: '001',
			text: '学校1'
		},
		{
			value: '002',
			text: '学校2'
		}
	]
})

const { rules, schoolList, loading } = toRefs(data);

onLoad(() => {
	const info = userStore.getUserInfo()
	console.log(info);
	if (info) {
		uni.redirectTo({
			url: '/pages/index/index'
		})
	}
})

function login () {
	validateForm().then(status => {
		if(!status) return false;
		loading.value = true;
		systemStore.Login(form).then(res => {
			uni.redirectTo({
				url: '/pages/index/index'
			})
		}).catch(error => {
			// loading.value = false;
			// uni.showToast({
			// 	icon: 'error',
			// 	title: '登录失败'
			// })
		}).finally(() => {
			loading.value = false;
		})
	})
}
</script>

<style lang="scss" scoped>
@import 'login.scss'
</style>
