import { ref } from 'vue'
import { Ermcp3ContractRsp } from '@/services/go/ermcp/spot-contract/interface';
import { getBizTypeName, getContractStatusName, getPriceTypeName } from '@/common/constants/enumsName';
import { formatTime, formatValue } from '@/common/methods';
import { ColumnType } from '@/common/methods/table';
import { FilterColumn } from '@/common/components/filter/interface';

function getColumns() {
    const columns: ColumnType[] = [
        {
            title: '合同编号',
            key: 'contractno',
            width: 160,
        },
        {
            title: '对手方',
            key: 'negative',
            width: 280,
            customRender: ({ record }: { record: Ermcp3ContractRsp }) => {
                // 根据合同类型显示对手方
                if (record.contracttype === 1) {
                    return record.sellusername;
                }
                return record.buyusername;
            },
        },
        {
            title: '商品',
            key: 'goodsname'
        },
        {
            title: '业务类型',
            key: 'biztype',
            customRender: ({ text }: { text: number }) => getBizTypeName(text),
        },
        {
            title: '定价类型',
            key: 'pricetype',
            customRender: ({ text }: { text: number }) => getPriceTypeName(text),
        },
        {
            title: '结算币种',
            key: 'currencyname',
        },
        {
            title: '合同量',
            key: 'qty',
        },
        {
            title: '价格',
            key: 'price',
        },
        {
            title: '升贴水',
            key: 'pricemove',
        },
        {
            title: '金额',
            key: 'amount',
        },
        {
            title: '保证金',
            key: 'margin',
        },
        {
            title: '点价合约',
            key: 'goodscode',
        },
        {
            title: '点价期',
            key: 'startdate',
            width: 200,
            customRender: ({ record }: { record: Ermcp3ContractRsp }) => {
                return formatValue(formatTime(record.startdate, 'd') + '--' + formatTime(record.enddate, 'd'));
            }
        },
        {
            title: '交收期',
            key: 'deliverystartdate',
            width: 200,
            customRender: ({ record }: { record: Ermcp3ContractRsp }) => {
                return formatValue(formatTime(record.deliverystartdate, 'd') + '--' + formatTime(record.deliveryenddate, 'd'));
            }
        },
        {
            title: '业务员',
            key: 'saleusername',
        },
        {
            title: '跟单员',
            key: 'merusername',
        },
        {
            title: '交易用户',
            key: 'tradeusername',
            width: 120,
        },
        {
            title: '状态',
            key: 'contracctstatus',
            customRender: ({ text }: { text: number }) => getContractStatusName(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,
    }
}