<template>
  <div>

  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'

import { login, getUserInfo } from '@/api/login'
import { ElMessage } from 'element-plus' // 
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
// 1. 引入封装好的加密方法
import { encryptPassword } from '@/utils/jsencrypt'

const userStore = useUserStore()
const router = useRouter() // 获取路由实例
// 响应式数据
const userName = ref('huijiaoyan')
const passWord = ref('123456')
const remeberPassWord = ref(false)
const loadingLogin = ref(false)
const showPass = ref(false)


onMounted(() => {

  //获取地址栏参数 token
  const token = router.currentRoute.value.query.token;
  if(token==undefined)
  {
    //没有token
  }
  else
  {
    console.log("打印token",token)
    // 确保 token 是字符串类型
    const tokenStr = Array.isArray(token) ? token[0] : token;
    if(tokenStr)
    {
      localStorage.setItem('token', tokenStr)
    }
    
    getUserInfo().then((userInfoRes) => { 
      console.log("打印用户信息返回res",userInfoRes)
      if (userInfoRes && userInfoRes.code === 200)
      {
        //获取用户信息成功 token未失效
        // localStorage.setItem('userInfo', JSON.stringify(userInfoRes.data))
        userStore.setUserInfo({
          userId: userInfoRes.data.id,
          userName: userInfoRes.data.userName,
          schoolId: userInfoRes.data.schoolId,
          schoolName: userInfoRes.data.schoolName,
          accountName: userInfoRes.data.loginName,
          phone: userInfoRes.data.phone,
          identify: userInfoRes.data.identify,
          roleNames: userInfoRes.data.roleNames,
          roleCodes: userInfoRes.data.roleCodes,
        })
        // ElMessage.success('登录成功');
        // 跳转到主页
        router.push('/main/choice')
      }
    })
  }
  
})

// 清空输入框
const ClearInput = () => {
  userName.value = ''
}
 const menuList = ref([])
// 登录提交
const SubmitLogin = () => {
  if(!userName.value || !passWord.value) {
    ElMessage.error('请输入账号和密码');
    return
  }

  const params = {
    username: userName.value.trim(),
    password: encryptPassword(passWord.value.trim()),
    deviceType:'PC'
  }
  loadingLogin.value = true
  login(params).then((res) => {
    console.log("打印登录接口返回res",res)
       loadingLogin.value = false
      if (res.code === 200) {
        console.log("登录成功返回",res)
        localStorage.setItem('token', res.data.tokenValue)
        menuList.value = res.data.pcMenuVOS
        localStorage.setItem('menuList', JSON.stringify(res.data.pcMenuVOS))
        localStorage.setItem('permissions', JSON.stringify(res.data.permissions))
        // 登录成功后调用获取用户信息接口
        return getUserInfo()
      }
  }).then((userInfoRes)=>{
    console.log("获取用户信息返回",userInfoRes)
    if (userInfoRes && userInfoRes.code === 200) {
      // 存储用户信息到 localStorage 或 Pinia
      // localStorage.setItem('userInfo', JSON.stringify(userInfoRes.data))
      userStore.setUserInfo({
        userId: userInfoRes.data.id,
        userName: userInfoRes.data.userName,
        schoolId: userInfoRes.data.schoolId,
        schoolName: userInfoRes.data.schoolName,
        accountName: userInfoRes.data.loginName,
        phone: userInfoRes.data.phone,
        identify: userInfoRes.data.identify,
        roleNames: userInfoRes.data.roleNames,
        roleCodes: userInfoRes.data.roleCodes,
      })
      ElMessage.success('登录成功');
      // 跳转到主页
      router.push('/main/choice')
    }
  }).catch((err) => {
      loadingLogin.value = false

      console.error(err)
  })
}

// 去往官网
const GoWebsite = () => {
  window.open('https://www.k12100.com/website')
}
</script>
<style lang="scss">
@use "../../styles/login.scss" as *;
</style>
