<template>
    <!-- 套期交易-套期项目关联-合同关联项目 -->
    <a-modal class="commonModal custom-detail" title="关联套期项目" v-model:visible="visible" centered @cancel="cancel(false)"
        :maskClosable="false" width="890px">
        <a-form class="inlineForm">
            <fieldset class="formFieldSet">
                <legend>合同信息</legend>
                <a-row :gutter="24">
                    <a-col :span="12">
                        <a-form-item label="合同编号">
                            <span class="white">{{ formatValue(selectedRow.contractno) }}</span>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="定价类型">
                            <span class="white">{{ getPriceTypeName(selectedRow.pricetype) }}</span>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="对手方">
                            <span class="white">{{ formatValue(selectedRow.customername) }}</span>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="点价合约">
                            <span class="white">{{ formatValue(selectedRow.goodscode) }}</span>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="现货商品">
                            <span class="white">{{ formatValue(selectedRow.wrstandardname) }}</span>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="套期主体">
                            <span class="white">{{ formatValue(selectedRow.accountname) }}</span>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="合同数量">
                            <span class="white">{{ formatValue(selectedRow.qty) }}</span>
                        </a-form-item>
                    </a-col>
                </a-row>
            </fieldset>
            <fieldset class="formFieldSet">
                <legend>关联信息</legend>
                <a-row :gutter="24">
                    <a-col :span="12">
                        <a-form-item label="当前关联数量">
                            <span class="up-quote-color">{{ currentQty }}</span>
                        </a-form-item>
                    </a-col>
                </a-row>
                <div class="tableDatas">
                    <a-table class="dialogTable" :columns="columns" :data-source="tableList" :pagination="false"
                        :loading="loading" :rowKey="(record, index) => index" :row-selection="rowSelection">
                        <!-- 套期类型 -->
                        <template #hedgedtype="{ text }">
                            <span>{{ getHedgedTypeName(text) }}</span>
                        </template>
                        <template #relatedqty="{ record, index }">
                            <a-input-number class="dialogInput" size="small"
                                :disabled="!selectedRowKeys.includes(index)" :precision="0"
                                v-model:value="record.relatedqty" style="width:100px" />
                        </template>
                    </a-table>
                </div>
            </fieldset>
        </a-form>
        <template #footer>
            <a-button key="submit" class="cancelBtn" :loading="loading" @click="cancel(false)">取消</a-button>
            <a-button key="submit" type="primary" :loading="loading" @click="formSubmit(() => cancel(true))">确定
            </a-button>
        </template>
    </a-modal>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { _closeModal } from '@/common/setup/modal/modal'
import { getPriceTypeName } from '@/@next/constants/enum/priceType'
import { formatValue } from '@/common/methods'
import { UnLinkSpotContractRsp } from '@/services/go/ermcp/hedgedItem/interface'
import { getHedgedTypeName } from '@/@next/constants/enum/hedgedType'
import { useForm } from './form'

export default defineComponent({
    emits: ['cancel'],
    props: {
        selectedRow: {
            type: Object as PropType<UnLinkSpotContractRsp>,
            required: true
        },
    },
    setup(props, context) {
        const { visible, cancel } = _closeModal(context);
        const { loading, tableList, columns, currentQty, selectedRowKeys, rowSelection, formSubmit } = useForm(props.selectedRow);

        return {
            visible,
            loading,
            tableList,
            columns,
            selectedRowKeys,
            cancel,
            formatValue,
            currentQty,
            getHedgedTypeName,
            formSubmit,
            rowSelection,
            getPriceTypeName,
        }
    }
})
</script>

<style lang="less">
.custom-detail {
    .ant-modal-body {
        padding-top: 0;
        padding-left: 0;
        padding-right: 0;
    }

    .tableDatas {
        margin-top: 26px;
        padding: 0;
        overflow: hidden;

        .dialogTable {
            width: 100%;
            max-height: 230px;
            overflow-x: auto !important;
        }
    }

    .ant-form.inlineForm {
        margin-top: 20px;
        padding: 0 24px;
    }
}
</style>