import { BuyOrSell } from "@/common/constants/enumCommon";
import { queryResultLoadingAndInfo } from "@/common/methods/request/resultInfo";
import { validateCommon } from "@/common/setup/validate";
import { queryHoldLB } from "@/services/go/wrtrade";
import { QueryOrderQuoteMyqQsp, WrHoldLB } from "@/services/go/wrtrade/interface";
import { Wrhold } from "@/views/market/market-spot/spot_trade_order_transaction/components/delisting/interface";
import { RuleObject } from "ant-design-vue/lib/form/interface";
import { reactive, Ref, ref, UnwrapRef } from "vue";
import { ListingForm } from "./interface";



export function handleForm() {
    const formRef = ref();
    const formState: UnwrapRef<ListingForm> = reactive({
        accountid: undefined,
        price: 0,
        num: 0,
        LadingBillId: '',
        case: '',
        caseId: 0
    })
    const v_ContractType = async (rule: RuleObject, value: number) => {
        return validateCommon(value, '请输入摘牌数量');
    };
    const rules = {
        accountid: [{ required: true, message: '请选择交易账户' }],
        LadingBillId: [{ required: true, message: '请选择现货仓单' }],
        caseId: [{ required: true, message: '请选择融资方案' }],
        num: [{ required: true, message: '请输入摘牌数量', trigger: 'blur', type: 'number', },
        { required: true, validator: v_ContractType }],

    }
    return { rules, formState, formRef }
}


export function handleSpotWarrant(buyOrSell: BuyOrSell, selectedRow: QueryOrderQuoteMyqQsp, loading: Ref<boolean>) {
    const wrHoldList = ref<Wrhold[]>([])
    const allWrHoldList = ref<WrHoldLB[]>([])
    if (buyOrSell === BuyOrSell.buy) {
        queryResultLoadingAndInfo(queryHoldLB, loading).then(res => {
            allWrHoldList.value = res
            wrHoldList.value.length = 0
            res.forEach(el => {
                if (el.wrfactortypeid === String(selectedRow.wrfactortypeid)) {
                    const { wrholdeno, enableqty, ladingbillid, enumdicname, subnum } = el
                    const result = { id: ladingbillid, name: `${wrholdeno}（${enableqty}${enumdicname}）`, subnum }
                    wrHoldList.value.push(result)
                }
            })
        })
    }

    return { wrHoldList, allWrHoldList }
}
