import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { User, UserQueryParams, UserStatus } from '../../types/user'



export const useUserStore = defineStore('user', () => {
  // 状态
  const users = ref<User[]>([])
  const currentUser = ref<User | null>(null)
  const loading = ref(false)
  const totalCount = ref(0)
  const queryParams = ref<UserQueryParams>({
    page: 1,
    pageSize: 10,
    keyword: '',
    status: undefined,
    startDate: '',
    endDate: ''
  })

  // 获取用户列表
  const fetchUsers = async (params?: UserQueryParams) => {
    loading.value = true
    try {
      // 这里应该是API调用，暂时用模拟数据
      const mockUsers: User[] = [
        {
          id: 1,
          username: 'user1',
          nickname: '用户1',
          avatar: '',
          phone: '13800138000',
          email: 'user1@jiebanke.com',
          status: 'active',
          registerTime: '2024-01-01 10:00:00',
          lastLoginTime: '2024-01-15 15:30:00',
          userType: 'normal'
        },
        {
          id: 2,
          username: 'merchant1',
          nickname: '商家1',
          avatar: '',
          phone: '13900139000',
          email: 'merchant1@jiebanke.com',
          status: 'active',
          registerTime: '2024-01-02 09:00:00',
          lastLoginTime: '2024-01-14 14:20:00',
          userType: 'merchant'
        }
      ]
      
      users.value = mockUsers
      totalCount.value = mockUsers.length
      
      if (params) {
        queryParams.value = { ...queryParams.value, ...params }
      }
    } catch (error) {
      console.error('获取用户列表失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取用户详情
  const fetchUserDetail = async (userId: number) => {
    loading.value = true
    try {
      // 模拟API调用
      const mockUser: User = {
        id: userId,
        username: `user${userId}`,
        nickname: `用户${userId}`,
        avatar: '',
        phone: '13800138000',
        email: `user${userId}@jiebanke.com`,
        status: 'active',
        registerTime: '2024-01-01 10:00:00',
        lastLoginTime: '2024-01-15 15:30:00',
        userType: 'normal'
      }
      
      currentUser.value = mockUser
      return mockUser
    } catch (error) {
      console.error('获取用户详情失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新用户状态
  const updateUserStatus = async (userId: number, status: UserStatus) => {
    try {
      // 模拟API调用
      const user = users.value.find((u: User) => u.id === userId)
      if (user) {
        user.status = status
      }
      
      if (currentUser.value && currentUser.value.id === userId) {
        currentUser.value.status = status
      }
      
      return true
    } catch (error) {
      console.error('更新用户状态失败:', error)
      throw error
    }
  }

  // 搜索用户
  const searchUsers = async (keyword: string) => {
    queryParams.value.keyword = keyword
    return fetchUsers(queryParams.value)
  }

  // 重置查询参数
  const resetQueryParams = () => {
    queryParams.value = {
      page: 1,
      pageSize: 10,
      keyword: '',
      status: undefined,
      startDate: '',
      endDate: ''
    }
  }

  // 导出用户数据
  const exportUsers = async () => {
    try {
      // 模拟导出功能
      const csvContent = users.value.map(user => 
        `${user.id},${user.username},${user.nickname},${user.phone},${user.email},${user.status}`
      ).join('\n')
      
      return csvContent
    } catch (error) {
      console.error('导出用户数据失败:', error)
      throw error
    }
  }

  return {
    users,
    currentUser,
    loading,
    totalCount,
    queryParams,
    fetchUsers,
    fetchUserDetail,
    updateUserStatus,
    searchUsers,
    resetQueryParams,
    exportUsers
  }
})