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

// 表格数据模型
export function getCustomModule<T>() {
    // 表头数据
    const columns = ref<ColumnType[]>([]);
    // 表格数据
    const customList = ref<T[]>([]);
    // 过滤信息
    const filteredInfo = ref();
    // 加载状态
    const loading = ref<boolean>(false);
    // 表头数据更新
    let columnsListToUpdate: Function = (): void => { };
    // 注册表头
    function getColumns(tableName: keyof TableKey, cb: Function) {
        const list = getTableHead(tableName);
        const filtered = filteredInfo.value || {};
        columns.value.length = 0;
        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 },
            };
            cb(e, item, filtered);
            columns.value.push(item);
        });
    }
    // 获取表格列表数据
    function getColumnsList(cb: Function) {
        columnsListToUpdate = () => cb();
        columnsListToUpdate();
    }

    return { columns, customList, loading, getColumns, getColumnsList, columnsListToUpdate };
}
