import { BuyOrSell } from "@/common/constants/enumCommon";
import { EnumRouterName } from "@/common/constants/enumRouterName";
import { getAccountTypeList, getCanUseMoney } from "@/services/bus/account";
import { getRules } from '@/services/bus/rules';
import { AccountListItem } from "@/services/dataCenter/interafce/account";
import { WrOrderQuote } from "@/services/go/wrtrade/interface";
import moment from "moment";
import { reactive, ref, UnwrapRef } from "vue";
import { getGoodsPrice, handleIs } from "../buy-sell-market/setup";
import { FormParam } from "./interface";

const formState: UnwrapRef<FormParam> = reactive({
    accountid: undefined,
    FixedPrice: 0,
    OrderQty: 0,
    PriceMove: 0,
    DeliveryMonth: moment()
})
export function handleForm() {
    const formRef = ref();
    const rules = {
        FixedPrice: [{ required: true, message: '请输入挂牌价格', trigger: 'blur', type: 'number', }],
        OrderQty: [{ required: true, message: '请输入挂牌数量', trigger: 'blur', type: 'number', min: 1 }],
        PriceMove: [{ required: true, message: '请输入基差', trigger: 'blur', type: 'number' }],
        accountid: [{ required: true, message: '请输入交易账号' }],
    }
    return { rules, formState, formRef }
}


export function handleNumAndPrice(enumName: EnumRouterName, selectedRow: WrOrderQuote) {
    const { isFloat } = handleIs(enumName, BuyOrSell.buy)
    // 资金账号
    const accountList = getAccountTypeList([2]);
    // 获取选中的资金账号
    const getSelectedAccount = () => accountList.find((e) => e.accountid === formState.accountid);
    // 交易规则
    const rules = getRules()
    const { buymarginvalue, buymarginalgorithm } = rules[0]
    //保证金模式 1： 比率 2： 固定
    const marginMethod = () => buymarginalgorithm === 1
    // 挂牌最大数量
    function getMaxNum() {
        // 可用资金
        const canUseMoney = Number(getCanUseMoney(getSelectedAccount() as AccountListItem))
        const { buymarginvalue } = rules[0]
        // 挂牌最大数量=可用资金/(买方履约保证金比例*挂牌价格)
        let result = 0
        if (isFloat()) {
            const price = getPrice()
            if (price !== '--') {
                result = canUseMoney / (Number(price) * buymarginvalue)
            }
        } else {
            const margin = marginMethod() ? (buymarginvalue * formState.FixedPrice) : (buymarginvalue + formState.FixedPrice)
            if (margin) {
                result = Math.floor(canUseMoney / margin)
            }
        }
        return +result.toFixed(0)
    }
    //  摘牌金额
    function getMoney() {
        let result = 0
        if (isFloat()) {
            const goodsPrice = getGoodsPrice()
            if (goodsPrice.value && goodsPrice.value !== '--') {   // 有实时行情价格
                // 估算总价=挂牌基差+期货合约价；
                const predictTotal = formState.PriceMove + (goodsPrice.value as number);
                // 估算总额=估算总价*摘牌数量；
                result = predictTotal * formState.OrderQty
            }
        } else {
            // 摘牌金额=挂牌价格*摘牌数量
            result = formState.OrderQty * formState.FixedPrice
        }
        return Number(result.toFixed(2))
    }
    // 履约保证金
    function getMargin() {
        const { buymarginvalue } = rules[0]
        // 浮动价 履约保证金=估算总额*买方履约保证金比例
        // 一口价 履约保证金=挂牌金额*买方履约保证金比例
        const margin = marginMethod() ? (buymarginvalue * getMoney()) : (buymarginvalue + getMoney())
        return Number((margin).toFixed(2))
    }
    function getPrice() {
        let result = '--'
        const goodsPrice = getGoodsPrice()
        if (goodsPrice.value && goodsPrice.value !== '--') {   // 有实时行情价格
            result = ((goodsPrice.value as number) + formState.PriceMove).toFixed(2)
        }
        return result
    }
    return { getMaxNum, getMoney, getMargin, getPrice }
}