import { ref } from 'vue'
import { ErmcpHedgePosition, ErmcpHedgePositionDetail } from '@/services/go/ermcp/exposure/interface';
import { ColumnType } from '@/common/methods/table';
import { FilterColumn } from '@/common/components/filter/interface';

/**
 * 表头对应字段
 */
function getColumns() {
    const columns: ColumnType[] = [
        {
            title: '交易用户',
            key: 'accountid',
            width: 200,
            customRender: ({ record }: { record: ErmcpHedgePosition }) => `${record.accountid}/${record.hedgeaccountcode}`,
        },
        {
            title: '期货合约/代码',
            key: 'goodsid',
            width: 160,
            customRender: ({ record }: { record: ErmcpHedgePosition }) => `${record.goodsname}/${record.goodscode}`,
        },
        {
            title: '当前数量',
            key: 'totalcurqty',
        },
        {
            title: '今日变更',
            key: 'increaseqty',
            customRender: ({ record }: { record: ErmcpHedgePosition }) => record.increaseqty + record.decreaseqty,
        },
        {
            title: '昨日数量',
            key: 'totalydqty'
        },
        {
            title: '开仓数量',
            key: 'increaseqty',
        },
        {
            title: '平仓数量',
            key: 'decreaseqty',
        },
    ];

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

/**
 * 明细表头对应字段
 */
function getColumnsDetail() {
    const columns = [
        {
            title: '期货合约/代码',
            key: 'hedgegoodsid',
            customRender: ({ record }: { record: ErmcpHedgePositionDetail }) => `${record.goodsname}/${record.goodscode}`,
        },
        {
            title: '方向',
            key: 'buyorsell',
            slots: {
                customRender: 'buyorsell'
            },
        },
        {
            title: '类型',
            key: 'channelbuildtype',
            slots: {
                customRender: 'channelbuildtype'
            },
        },
        {
            title: '期货数量(手)',
            key: 'tradeqty'
        },
        {
            title: '时间',
            key: 'tradetime',
        },
    ];

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

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

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