import { message } from "ant-design-vue";
import { ref } from "vue";

type PromoiseFn = (param?: any) => Promise<any>

// 通用 查询数据 hooks
export function useQueryData<T>(fn: PromoiseFn, param?: any) {
    const loading = ref<boolean>(true)
    const list = ref<T[]>([])
    function update() {
        if (param) {
            console.log('查询数据 参数', param)
            fn(param).then(res => {
                console.log(' 查询数据 hooks', res)
                list.value = res
            })
                .catch(err => message.error(err))
                .finally(() => loading.value = false)
        } else {
            fn().then(res => {
                console.log(' 查询数据 hooks', res)
                list.value = res
            })
                .catch(err => message.error(err))
                .finally(() => loading.value = false)
        }
    }
    update()
    return { loading, list, update }
}