import moment from "moment";
import { reactive } from "vue";
import { GoodsQuote } from '@/services/go/ermcp/goodsInfo/interface';
import { ChannelOrderReq } from '@/services/proto/futures/interface';
import { v4 as uuidv4 } from 'uuid';
import { PriceType } from '@/common/constants/enumCommon';
import { getBuyOrSellName } from '@/common/constants/enumsName';
import { geLoginID_number } from '@/services/bus/login';
import { getUserId } from '@/services/bus/user';
import { RuleObject } from 'ant-design-vue/es/form/interface';
import { QueryErmcpTradePositionRsp } from '@/services/go/ermcp/futures/interface';
import { getSelectedAccountId } from '@/services/bus/account';

export function getColumns() {
    const columns = [
        {
            title: '方向',
            key: 'buyorsell',
            customRender: ({ record }: { record: QueryErmcpTradePositionRsp }) => {
                return getBuyOrSellName(record.buyorsell)
            }
        },
        {
            title: '可用',
            key: 'enableqty'
        },
        {
            title: '开仓均价',
            key: 'openaverageprice',
            customRender: ({ record }: { record: QueryErmcpTradePositionRsp }) => {
                const { opencost, curpositionqty, agreeunit } = record;
                // 开仓成本 ÷ 期末头寸 ÷ 合约单位
                const result = opencost / curpositionqty / agreeunit;
                return result.toFixed(2)
            }
        },
        {
            title: '持仓均价',
            key: 'positionaverageprice',
            customRender: ({ record }: { record: QueryErmcpTradePositionRsp }) => {
                const { positioncost, curpositionqty, agreeunit } = record;
                // 持仓成本 ÷ 期末头寸 ÷ 合约单位
                const result = positioncost / curpositionqty / agreeunit;
                return result.toFixed(2);
            }
        },
    ];

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

export function handleForm() {
    // 数据初始化
    const formData = reactive<ChannelOrderReq>({
        ClientSerialNo: uuidv4(), // string 客户端流水号
        ClientOrderTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'), // string 客户端委托时间
        ClientType: 4, // uint32 终端类型
        LoginID: geLoginID_number()!, // uint64 登陆账号
        AccountID: getSelectedAccountId(), // uint64 交易账号
        GoodsID: undefined, // uint32 商品ID
        MarketID: 0, // uint32 市场ID
        ValidType: 1, // int32 有效类型 - 1当日有效
        ChannelOperateType: 1, // uint32 操作类型:
        ChannelOrderSrc: 1, // uint32 单据来源委托来源 -  1:客户端 2:管理端 3:风控服务
        HedgeFlag: 0, // uint32 投机套保标志 - 0:无 1:投机 2:套保 3:套利 4:套期保值 5:单边 6:移仓 7:错单处理 8:跨期套利
        OperatorID: getUserId(), // uint64 操作员账号ID
        OrderPrice: 0, // double 委托价格
        OrderQty: 1, // uint64 委托数量
        BuyOrSell: 0, // uint32 买卖方向(买卖 - 0:买 1:卖 )
        ChannelBuildType: 0, // uint32 下单类型(开平标志 - 0:无 1:建仓 2:平仓)
        CloseType: 0, // uint32 平仓方式(平仓方式 - 0:无 1:平今 2:平昨)
        PriceMode: PriceType.market, // uint32 取价方式 - 1:市价 2: 限价
        TimeValidType: 1, // uint32 时间有效类型
    })

    // 验证交易价格
    const verifyOrderPrice = async (rule: RuleObject, value: number) => {
        if (value) {
            return Promise.resolve();
        } else {
            return Promise.reject('交易价格不能小于0')
        }
    }

    const rules = {
        AccountID: [{ required: true, type: 'number', message: '请选择账号' }],
        GoodsID: [{ required: true, type: 'number', message: '请选择合约' }],
        OrderPrice: [{ required: true, trigger: 'blur', type: 'number', validator: verifyOrderPrice, }],
        OrderQty: [{ required: true, trigger: 'blur', type: 'number', message: '请输入交易数量' }],
    }

    return {
        rules,
        formData,
    }
}


