// src/stores/user.js
import { defineStore } from 'pinia';
import { ref } from 'vue';
import request from '@/utils/request';

export const useUserStore = defineStore('user', () => {
  const users = ref([]);
  const totalUsers = ref(0);
  const user = ref(null);
  const loading = ref(false);
  const error = ref(null);

  async function createUser(userData) {
    loading.value = true;
    error.value = null;
    try {
      const response = await request.post('/users', userData);
      return response
    } catch (err) {
      error.value =  err.response?.data?.error || '创建用户失败'
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function listUser(pageSize = 20, page = 1, active) {
    loading.value = true;
    error.value = null;
    try {
      const response = await request.get('/users', {
        params: {
          pageSize,
          page,
          active,
        },
      });
      users.value = response.data.data?.users;
      totalUsers.value = response.data.data?.total;
    } catch (err) {
      error.value = err.response?.data?.error || '获取用户列表失败';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function getUser(id) {
    loading.value = true;
    error.value = null;
    try {
      const response = await request.get(`/users/${id}`);
      console.log('getUser response',response);
      user.value = response.data.data;
      return response
    } catch (err) {
      error.value = err.response?.data?.error || '获取用户信息失败';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function refreshUser(id) {
    loading.value = true;
    error.value = null;
    try {
      const response = await request.get(`/users/${id}`);
      console.log('getUser response',response);
      user.value = response.data.data;
      return response
    } catch (err) {
      error.value = err.response?.data?.error || '获取用户信息失败';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function editUser(id, userData) {
    loading.value = true;
    error.value = null;
    try {
      const response= await request.put(`/users/${id}`, userData);
      console.log('editUser',response);
      return response
    } catch (err) {
      error.value = err.response?.data?.error || '编辑用户失败';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function deleteUser(id) {
    loading.value = true;
    error.value = null;
    try {
      const response = await request.delete(`/users/${id}`);
      return response
    } catch (err) {
      error.value = err.response?.data?.error || '删除用户失败';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function userOption(option, ids) {
    loading.value = true;
    error.value = null;
    try {
      const response = await request.post(`/users/batch/${option}`, { ids });
      
      return response
    } catch (err) {
      error.value = err.response?.data?.error || '操作失败';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  return {
    users,
    totalUsers,
    user,
    loading,
    error,
    createUser,
    listUser,
    getUser,
    refreshUser,
    editUser,
    deleteUser,
    userOption,
  };
});