<template>
    <!-- 回购仓单 -->
    <Drawer
        :title="'回购仓单'"
        :placement="'right'"
        :visible="visible"
        class="delistingBottom"
        @cancel="cancel"
    >
        <div class="listed">
            <a-form class="inlineForm dialogForm" ref="formRef" :model="formState" :rules="rules">
                <div class="formBar">
                    <a-row :gutter="24">
                        <a-col :span="12">
                            <a-form-item label="品种">
                                <span class="white ml10">{{selectedRow.deliverygoodsname}}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="仓库">
                                <span class="white ml10">{{selectedRow.warehousename}}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item label="履约商品">
                                <span class="white ml10">{{selectedRow.wrtypename}}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="购买数量">
                                <span
                                    class="white ml10"
                                >{{ selectedRow.wrqty + " " + selectedRow.enumdicname }}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="已回购数量">
                                <span
                                    class="white ml10"
                                >{{ selectedRow.buybackwrpositionqty + " " + selectedRow.enumdicname }}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="待回购数量">
                                <span
                                    class="white ml10"
                                >{{ selectedRow.remainwrpositionqty + " " + selectedRow.enumdicname }}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="本次回购数量" name="num" class="relative">
                                <a-input-number
                                    class="commonInput ml10"
                                    :min="1"
                                    v-model:value="formState.num"
                                    style="width: 200px"
                                />

                                <span class="input-enumdicname-absolute">吨</span>
                                <!-- 本次可回购数量=min{资金方对应仓单头寸可用数量，融资合同剩余待回购数量} -->
                                <div
                                    class="tip"
                                >{{ "本次可回购数量" + selectedRow.remainwrpositionqty + " " + selectedRow.enumdicname }}</div>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <!--（回购数量/合同剩余数量） * 合同剩余金额 -->
                            <a-form-item label="本次回购金额">
                                <span
                                    class="white ml10"
                                >{{ (((formState.num === 0 ? 1 : formState.num) * selectedRow.remainamount) / selectedRow.remainwrpositionqty).toFixed(2) + " 元" }}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="剩余保证金">
                                <span class="white ml10">{{ selectedRow.initmargin + " 元" }}</span>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </div>
                <a-row :gutter="24">
                    <a-col :span="24" class="fixedBtns">
                        <a-form-item class="btnCenter">
                            <a-button
                                class="listedBtn"
                                :loading="loading"
                                :disabled="loading"
                                @click="submit"
                            >提交</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
    </Drawer>
</template>

<script lang="ts">
import { defineComponent, PropType, ref } from 'vue';
import Drawer from '@/common/components/drawer/index.vue';
import { WrScfContract } from '@/services/go/wrtrade/interface';
import { ModalEnum } from '@/common/constants/modalNameEnum';
import { _closeModal } from '@/common/setup/modal/modal';
import { handleForm } from './setup';
import { validateAction } from '@/common/setup/form';
import { FormState } from './interface';
import { WarehouseRepurchase } from '@/services/proto/warehousetrade';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { WarehouseRepurchaseReq } from '@/services/proto/warehousetrade/interface';
import { getTimeValue } from '@/utils/time';
import Long from 'long';

export default defineComponent({
    name: ModalEnum.financing_manager_contract_repurchase_warrant,
    components: { Drawer },
    emits: ['cancel', 'update'],
    props: {
        selectedRow: {
            type: Object as PropType<WrScfContract>,
            default: {},
        },
    },
    setup(props, context) {
        const { visible, cancel } = _closeModal(context);
        const loading = ref<boolean>(false);
        const { rules, formState, formRef } = handleForm();
        function submit() {
            validateAction<FormState>(formRef, formState).then((param) => {
                const paramReq: WarehouseRepurchaseReq = {
                    ExtBusinessID: getTimeValue(), // uint64 外部业务流水号
                    SCFContractID: Long.fromString(props.selectedRow.scfcontractid), // uint64 SCF合同ID
                    SCFContractType: props.selectedRow.scfcontracttype, // int32 合同类型
                    WRPositionQty: param.num, // uint64 仓单头寸数量
                };
                requestResultLoadingAndInfo(WarehouseRepurchase, paramReq, loading, ['回购仓单成功', '回购仓单失败:']).then(() => {
                    cancel(true);
                });
            });
        }

        return {
            visible,
            cancel,
            submit,
            loading,
            rules,
            formState,
            formRef,
        };
    },
});
</script>

<style lang="less" scoped>
</style>;
