import axios from '../plugins/axios'
import { defineStore } from 'pinia'

export const useUsersStore = defineStore('users', {
    state: () => ({
        items: [],
        fetching: true,
        total: 0
    }),
    actions: {
        async index(page, pageSize) {
            this.items = [];
            this.fetching = true

            const params = { page, pageSize }
            const { status, data } = await axios.get('/admin/user/list', { params })

            this.fetching = false
            this.total = 30
            this.items = data;

        },
        async changeStatus(form) {
            const { status, data } = await axios.patch('/admin/user/status', form)
            return data
        },
        async confirm(item, value) {
            item.confirming = true
            const result = await axios.put(`/admin/users/seller/${item.id}`, { confirmToSales: value })
            item.confirming = false

            if (result.status === 200) {
                const index = this.items.findIndex(({ _id }) => _id == item.id)
                if (index >= 0)
                    this.items.splice(index, 1)
                else
                    this.items.unshift(item)
            }

            return result;
        },
    }
})