
import { refDebounced } from '@vueuse/core'
import XEUtils from 'xe-utils'

/**
 * 表内检索，用于假分页时进行表内搜索
 * @param tableData 全部数据
 * @param keys 需要检索的key
 * @returns 检索后的数据,检索关键词，总数，分页对象
 */
export function useInTableSearchAndPaging(tableData: any, keys: Array<string> = []){

    const tableKeyword = ref('')
    const debounceTableKeyword = refDebounced(tableKeyword, 500)
    const total = ref(0);
    const paging = ref({
        page: 1,
        pageSize: 10
    })

    const showData = computed(()=>{
        const filterData = tableData.value.filter((item: any) => {
            if (keys.length > 0) {
                const values = []
                for (const key in item) {
                    if (keys.includes(key)) {
                        values.push(item[key])
                    }
                }
                return values.some(val => String(val).includes(debounceTableKeyword.value))
            }
            return Object.values(item).some(val => String(val).includes(debounceTableKeyword.value))
        })

        const  data = filterData.slice((paging.value.page - 1) * paging.value.pageSize, (paging.value.page - 1) * paging.value.pageSize + paging.value.pageSize)
        total.value = filterData.length
        return data
    })

    return {tableKeyword, showData, total, paging}
}

/**
 * VxeGrid表内检索，用于真分页时进行表内搜索
 * @param filterName 关键字对象
 * @param grid 表格对象
 * @param tableData 全部数据对象
 * @param searchProps 需要检索的key
 * @param options 选项，{children: 'children'}是否作为子节点展示
 * @param allTreeExpand 是否展开树形结构子节点
 */
export function useInVxeTableSearch(filterName:any, grid:any, tableData: any, searchProps: Array<string> = [],options:any,allTreeExpand:boolean=false){
    const filterVal = XEUtils.toValueString(filterName.value).trim().toLowerCase()
    const $table = grid.value
    if(!$table)
        return false
    if (filterVal) {
        if(searchProps.length==0){
            searchProps=Object.keys(tableData.value[0])
        }
        const rest = XEUtils.searchTree(tableData.value, item => searchProps.some(key => String(item[key]).toLowerCase().indexOf(filterVal) > -1), options)
        grid.value.loadData(rest)
        if(allTreeExpand){
            // 搜索之后默认展开所有子节点
            nextTick(() => {
                $table.setAllTreeExpand(true)
            })
        }
    }else {
        $table.loadData(tableData.value)
    }
}

/**
 * VxeGrid表内检索，用于真分页时进行表内搜索
 * @param filterName 关键字对象
 * @param grid 表格对象
 * @param tableData 全部数据对象
 * @param searchProps 需要检索的key
 * @param options 选项，{children: 'children'}是否作为子节点展示
 * @param allTreeExpand 是否展开树形结构子节点
 */
export function useInVxeTableSearchAndPageing(filterName:any, grid:any, tableData: any, searchProps: Array<string> = [],options:any,allTreeExpand:boolean=false,
    page:number,size:number){
    const filterVal = XEUtils.toValueString(filterName.value).trim().toLowerCase()
    const $table = grid.value
    if(!$table)
        return false
    if (filterVal) {
        if(searchProps.length==0){
            searchProps=Object.keys(tableData.value[0])
        }
        const rest = XEUtils.searchTree(tableData.value, item => searchProps.some(key => String(item[key]).toLowerCase().indexOf(filterVal) > -1), options)
        grid.value.loadData(rest.slice((page - 1) * size, page * size))//重新加载表格数据
        if(allTreeExpand){
            // 搜索之后默认展开所有子节点
            nextTick(() => {
                $table.setAllTreeExpand(true)
            })
        }
    }else {
        grid.value.loadData(tableData.value.slice((page - 1) * size, page * size))
    }
}

export function useTableDataPageing(tableData: any,page:number,size:number){
    return tableData.slice((page - 1) * size, page * size)
}