import { ref } from 'vue'
import { Ermcp3SpotGoodsPrice } from '@/services/go/ermcp/qhj/interface';
import { ColumnType } from '@/common/methods/table';
import { FilterColumn } from '@/common/components/filter/interface';

function getColumns() {
    const columns: ColumnType[] = [
        {
            title: '现货品种',
            key: 'deliverygoodsname',
        },
        {
            title: '商品',
            key: 'wrstandardname'
        },
        {
            title: '品牌',
            key: 'brandname',
        },
        {
            title: '价格',
            key: 'spotgoodsprice',
            customRender: ({ text, record }: { text: Number, record: Ermcp3SpotGoodsPrice }) => {
                return `${text} ${record.currencyname}/${record.gbenumdicname}`;
            }
        },
    ];

    return columns.map(el => {
        return {
            dataIndex: el.key,
            width: 100,
            align: 'center',
            ...el
        }
    })
}

export function useColumn() {
    const columns = ref(getColumns());

    const updateColumn = (filters: FilterColumn[]) => {
        columns.value.forEach((item) => {
            const f = filters.find((e) => e.key === item.key);
            if (f) {
                item.filteredValue = f.filteredValue;
                item.onFilter = f.onFilter;
            } else {
                item.filteredValue = [];
            }
        })
    }

    return {
        columns,
        updateColumn,
    }
}