import { ref, computed, nextTick } from 'vue'
import useList from './list'

function useSearch(url, beforeData = {}, httpType) {
    const isSearch = ref(false)
    const search = ref('')

    const { empty, loading, finished, page, totalElements, size, list, getData } = useList(
        url,
        () => {
            return {
                ...beforeData,
                search: search.value
            }
        },
        httpType
    )

    const getSearch = val => {
        isSearch.value = true
        search.value = val
        nextTick(() => {
            getData(true)
        })
    }
    const onCancel = () => {
        if (isSearch.value) {
            isSearch.value = false
            search.value = ''
            list.value = []
        } else {
            getSearch(search.value)
        }
    }

    return {
        empty,
        loading,
        finished,
        page,
        totalElements,
        size,
        list,
        getData,
        isSearch,
        search,
        getSearch,
        onCancel
    }
}

export default useSearch
