<template>
    <!-- 修改套保品种 -->
    <a-modal
        class="commonModal fieldsetForm modify-custom"
        title="修改套保品种"
        v-if="visible"
        v-model:visible="visible"
        :loading="loading"
        centered
        :maskClosable="false"
        @cancel="cancel"
        width="890px"
    >
        <template #footer>
            <a-button key="submit" type="primary" :loading="loading" @click="submit">完成</a-button>
        </template>
        <a-spin :spinning="loading">
            <a-form class="inlineForm" ref="formRef" :model="formState" :rules="rules">
                <fieldset class="formFieldSet">
                    <legend>基本信息</legend>
                    <a-row :gutter="24">
                        <a-col :span="12">
                            <a-form-item label="套保品种名称">
                                <span class="white">{{selctedMG.mg.middlegoodsname}}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="单位">
                                <span
                                    class="white"
                                >{{getGoodsUnitEnumItemName(selctedMG.mg.goodsunitid)}}</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="当前套保比例">
                                <span
                                    class="white"
                                >{{(selctedMG.mg.needhedgeratio * 100).toFixed(2)}}%</span>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item label="修改后套保比例" name="needhedgeratio" class="relative">
                                <a-input-number
                                    class="dialogInput"
                                    style="width: 200px"
                                    type="number"
                                    :min="0"
                                    placeholder="请输入套保比例"
                                    v-model:value="formState.needhedgeratio"
                                />
                                <div class="backUnit">%</div>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </fieldset>
                <fieldset class="formFieldSet">
                    <legend>其他信息</legend>
                    <a-row :gutter="24">
                        <a-col :span="24">
                            <a-form-item label="备注">
                                <span class="white">{{selctedMG.mg.remark}}</span>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </fieldset>
            </a-form>
        </a-spin>
    </a-modal>
</template>

<script lang="ts">
import { defineComponent, PropType, ref } from 'vue';
import { closeModal } from '@/common/setup/modal/index';
import { initData } from '@/common/methods/index';
import { initMG } from '../../list/hedging-variety/setup';
import { Ermcp3MiddleGoodsDetail2 } from '@/services/go/ermcp/goodsInfo/interface';
import { getGoodsUnitEnumItemName } from '@/common/constants/enumsName';
import { handleForm } from './setup';
import { ermsMiddelGoodsEdit } from '@/services/proto/delivery';
import { ErmsMiddelGoodsEditReq } from '@/services/proto/delivery/interface';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { validateAction } from '@/common/setup/form';
import { FormState } from './interface';

export default defineComponent({
    name: 'modify-custom',
    components: {},
    props: {
        selctedMG: {
            default: initMG,
            type: Object as PropType<Ermcp3MiddleGoodsDetail2>,
        },
    },

    setup(props, context) {
        const { visible, cancel } = closeModal('goods_info_hedge_normal_modify');

        const { rules, formState, formRef } = handleForm();

        const loading = ref<boolean>(false);
        function submit() {
            validateAction<FormState>(formRef, formState).then(() => {
                const { middlegoodsid, goodsunitid, relatedgoodsid, evaluateratio, qtydecimalplace, relatedgoodstype, goodsgroupid, remark } = props.selctedMG.mg;
                const param: ErmsMiddelGoodsEditReq = {
                    middlegoodsid, // uint64 套保品种ID
                    goodsunitid, // int32 单位ID
                    relatedgoodsid, // uint64 关联交易商品ID
                    evaluateratio, // double 估价系数
                    qtydecimalplace, // int32 数量小数位
                    // modifierid,// uint64 修改人
                    relatedgoodstype, // int32 关联商品类型 - 1:期货合约 2:现货品种
                    needhedgeratio: (formState.needhedgeratio as number) / 100, // double 套保比率
                    // areauserid?,// uint64 机构用户ID
                    goodsgroupid, // uint64 关联期货品种ID
                    remark, // string 备注
                };
                requestResultLoadingAndInfo(ermsMiddelGoodsEdit, param, loading, ['修改成功', '修改失败:']).then(() => {
                    cancel();
                    context.emit('refresh');
                });
            });
        }
        initData(() => {});
        return {
            visible,
            cancel,
            submit,
            loading,
            maskClosableFlag: false,
            getGoodsUnitEnumItemName,
            rules,
            formState,
            formRef,
        };
    },
});
</script>

<style lang="less">
.modify-custom {
    .upload {
        display: inline-flex;
        .ant-btn.uploadBtn {
            width: 60px;
            height: 30px;
            background: @m-blue0;
            border: 0;
            padding: 0;
            text-align: center;
            font-size: 14px;
            color: @m-white0;
            .rounded-corners(3px);
            &:hover {
                background: @m-blue0-hover;
                color: @m-white0-hover;
            }
        }
        .look {
            color: @m-blue0;
            font-size: 14px;
            margin-left: 10px;
            cursor: pointer;
        }
    }
}
</style
>;
