
import { ColumnType, getTableHead } from '@/common/methods/table';
import { TableKey } from '@/common/methods/table/interface';
import { ref } from 'vue';

/**
 * 
 * 获取表头和查询
 * @param tableKey 动态表头名
 * @param hasFilter 是否有过滤功能
 * @returns 表头：columns；search 搜索方法
 */
export function getColumsAndSearch(tableKey: keyof TableKey, hasFilter: boolean) {
    const columns = ref<ColumnType[]>([]);
    const filteredInfo = ref();
    function getColumns() {
        const list = getTableHead(tableKey);
        // 过滤项
        const filtered = filteredInfo.value || {};
        list.forEach((e, i) => {
            const { columnfield, columntitle, aligntype } = e;
            const item: ColumnType = {
                key: String(i),
                dataIndex: columnfield, // 表格数据对应的key
                title: columntitle,
                align: aligntype === 1 ? 'center' : aligntype === 2 ? 'left' : 'right',
                slots: { customRender: columnfield },
            };
            // // 以下添加过滤数据对应的方法
            if (hasFilter) {
                if (e.columnfield === 'MiddleGoodsName') {
                    item.onFilter = (value: string, record: any) => record.MiddleGoodsName.includes(value);
                    item.filteredValue = filtered.MiddleGoodsName || null;
                }
            }
            columns.value.push(item);
        });

        return { columns }
    }
    // 查询
    function search(value: any) {
        filteredInfo.value = value;
        getColumns();
    }
    return { columns, search, getColumns }
}


