import { unref } from 'vue';
import { getBuyOrSellName } from '@/common/constants/enumsName';
import { TradePosition } from "@/hooks/account/interface";

export function getColumns() {
    const columns = [
        {
            title: '合约',
            key: 'goodscode',
            width: 150,
            customRender: ({ record }: { record: TradePosition }) => {
                return `${record.goodsname}/${record.goodscode}`
            }
        },
        {
            title: '方向',
            key: 'buyorsell',
            customRender: ({ text }: { text: number }) => getBuyOrSellName(text)
        },
        {
            title: '总持仓',
            key: 'curpositionqty'
        },
        {
            title: '可用',
            key: 'enableqty'
        },
        {
            title: '开仓均价',
            key: 'openAveragePrice',
            customRender: ({ record }: { record: TradePosition }) => {
                const value = unref(record.openAveragePrice);
                return value.toFixed(record.decimalplace);
            }
        },
        {
            title: '持仓均价',
            key: 'positionAveragePrice',
            customRender: ({ record }: { record: TradePosition }) => {
                const value = unref(record.positionAveragePrice);
                return value.toFixed(record.decimalplace);
            }
        },
        {
            title: '浮动盈亏',
            key: 'positionProfitAndLoss',
            customRender: ({ text }: { text: number }) => {
                if (text === 0) return 0;
                return handlePriceColor(text, text.toFixed(2));
            }
        },
        {
            title: '盈亏比例',
            key: 'positionProfitAndLossRate',
            customRender: ({ text }: { text: number }) => {
                if (text === 0) return 0;
                return handlePriceColor(text, text.toFixed(2) + '%');
            }
        },
    ];

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

/**
 * 处理显示颜色
 */
function handlePriceColor(value: number, text: string) {
    let className = ''
    if (value > 0) {
        className = 'up-quote-color'
    } else {
        className = 'down-quote-color'
    }
    return <span class={className}>{text}</span>;
}