import { shallowRef } from 'vue'
import { useTableColumnStore } from '@/stores'
import { TableColumnKey, pcTableColumnMap } from './columns'

export function useTableColumns(tableKey: string, operateColumn = true) {
    const tableColumnStore = useTableColumnStore()

    const columns = shallowRef(tableColumnStore.tableColumns.reduce((res, cur) => {
        if (cur.tablekey === tableKey) {
            cur.columns.forEach((e) => {
                res.push({
                    prop: e.columnfield,
                    label: e.columntitle,
                    width: Number(e.columnwidth),
                    show: e.isshow ? true : false,
                    fixed: e.fixed,
                })
            })
            if (operateColumn) {
                res.push({
                    prop: 'operate',
                    label: '操作',
                    show: true,
                })
            }
        }
        return res
    }, [] as Ermcp.TableColumn[]))

    const updateColumns = () => {
        console.log('更新表头列')
    }

    return {
        columns,
        updateColumns,
    }
}

/**
 * 获取表格列数据
 * @param tableKey 
 * @returns 
 */
export function getTableColumns(tableKey?: TableColumnKey) {
    if (tableKey) {
        const tableColumns = pcTableColumnMap.get(tableKey) ?? [] // *这里应该判断终端类型，有空再处理*
        return tableColumns.map((e) => {
            if (e.prop === 'operate') {
                e.fixed = 'right'
            }
            return e
        })
    }
    return []
}