import { ref } from 'vue'
import { getPlanContractType, getPlanStatusName } from '@/views/business/plan/setup';
import { ColumnType } from '@/common/methods/table';
import { FilterColumn } from '@/common/components/filter/interface';

function getColumns() {
    const columns: ColumnType[] = [
        {
            title: '计划名称',
            key: 'hedgeplanno',
        },
        {
            title: '商品',
            key: 'wrstandardname'
        },
        {
            title: '类型',
            key: 'contracttype',
            customRender: ({ text }: { text: number }) => getPlanContractType(text),
        },
        {
            title: '结算币种',
            key: 'currencyname',
        },
        {
            title: '计划量',
            key: 'planqty',
        },
        {
            title: '交易用户',
            key: 'tradeusername',
        },
        {
            title: '状态',
            key: 'hedgeplanstatus',
            customRender: ({ text }: { text: number }) => getPlanStatusName(text),
        },
    ];

    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,
    }
}