import { h, ref } from 'vue'
import { defineStore } from 'pinia'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import LoginSuccessNotification from '@/components/LoginSuccessNotification.vue'

export const useLoginStore = defineStore('login', () => {
  const router = useRouter()
  const username = ref('')
  const password = ref('')
  const remember = ref(false)
  const loading = ref(false)

  async function login() {
    loading.value = true
    try {
      // 模拟登录请求
      await new Promise((resolve) => setTimeout(resolve, 500))
      const loginInfo = {
        current: {
          ip: '188.253.12.45',
        },
        previous: {
          ip: '188.253.12.42',
          time: '2026-05-22 14:52:58',
        },
      }

      ElNotification({
        message: h(LoginSuccessNotification, loginInfo),
        customClass: 'login-success-notification',
        duration: 5000,
        offset: 16,
        position: 'bottom-right',
        showClose: true,
      })
      router.push('/')
    } finally {
      loading.value = false
    }
  }

  return { username, password, remember, loading, login }
})
