import { Taaccount } from '@/services/go/TaAccount/interface';
import { getTaacountStatus, getPayCurrencyTypeEnumItemName } from '@/common/constants/enumsName';
import { formatAmount } from '@/utils/number';

export function getColumns() {
    const columns = [
        {
            title: '资金账号',
            key: 'accountid',
        },
        {
            title: '币种',
            key: 'currencyid',
            customRender: ({ record }: { record: Taaccount }) => {
                return getPayCurrencyTypeEnumItemName(record.currencyid)
            }
        },
        {
            title: '当前权益',
            key: 'equity',
            customRender: ({ text }: { text: number }) => formatAmount(text)
        },
        {
            title: '可用资金',
            key: 'availableBalance',
            customRender: ({ text }: { text: number }) => formatAmount(text)
        },
        {
            title: '占用资金',
            key: 'usedmargin',
            customRender: ({ text }: { text: number }) => formatAmount(text)
        },
        {
            title: '冻结金额',
            key: 'freezeAmount',
            customRender: ({ text }: { text: number }) => formatAmount(text)
        },
        {
            title: '持仓盈亏',
            key: 'positionProfitAndLoss',
            customRender: ({ text }: { text: number }) => {
                if (text === 0) return 0;
                return handlePriceColor(text, formatAmount(text));
            }
        },
        {
            title: '平仓盈亏',
            key: 'closepl',
            customRender: ({ text }: { text: number }) => formatAmount(text)
        },
        {
            title: '风险率',
            key: 'hazardRatio',
            customRender: ({ text }: { text: number }) => {
                if (text > 0) {
                    return (text * 100).toFixed(2) + '%';
                }
                return 0;
            }
        },
        {
            title: '状态',
            key: 'tradestatus',
            customRender: ({ record }: { record: Taaccount }) => {
                return getTaacountStatus(record.tradestatus)
            }
        },
    ];

    return columns.map(el => {
        return {
            dataIndex: el.key,
            width: 150,
            align: 'center',
            slots: { customRender: el.key, },
            ...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>;
}