<template>
    <!-- 套期交易-期货成交关联-补单关联 -->
    <a-modal class="commonModal custom-detail" title="补单关联" v-model:visible="visible" centered @cancel="cancel(false)"
        :maskClosable="false" width="980px">
        <a-form class="inlineForm" ref="formElement" :model="formData" :rules="rules">
            <fieldset class="formFieldSet">
                <legend>成交单信息</legend>
                <a-row :gutter="24">
                    <a-col :span="12">
                        <a-form-item label="期货合约" name="GoodsID">
                            <a-select class="inlineFormSelect" placeholder="请选择合约" :filterOption="filterOption"
                                v-model:value="formData.GoodsID" @change="changeGoods" show-search style="width:200px">
                                <a-select-option v-for="item in goodsList" :value="item.goodsid" :key="item.goodsid">{{
                                        item.goodsname
                                }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="方向" name="BuyOrSell">
                            <a-select class="inlineFormSelect" placeholder="请选择方向" v-model:value="formData.BuyOrSell"
                                style="width:200px">
                                <a-select-option :value="BuyOrSell.buy">买</a-select-option>
                                <a-select-option :value="BuyOrSell.sell">卖</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="期货子账户" name="AccountID">
                            <a-select class="inlineFormSelect" placeholder="请选择账户" v-model:value="formData.AccountID"
                                style="width:200px">
                                <a-select-option v-for="item in accountList" :value="item.accountid"
                                    :key="item.accountid">
                                    {{ item.accountid }}/{{ item.accountname }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="建平" name="ChannelBuildType">
                            <a-select class="inlineFormSelect" placeholder="请选择下单类型"
                                v-model:value="formData.ChannelBuildType" style="width:200px">
                                <a-select-option :value="BuildType.open">建仓</a-select-option>
                                <a-select-option :value="BuildType.close">平仓</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="成交价" name="TradePrice">
                            <a-input-number class="dialogInput" placeholder="请输入价格" v-model:value="formData.TradePrice"
                                style="width:200px" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="成交数量" name="TradeQty">
                            <span class="white">
                                <a-input-number class="dialogInput" placeholder="请输入数量" :precision="0"
                                    v-model:value="formData.TradeQty" style="width:200px" />
                            </span>
                        </a-form-item>
                    </a-col>
                </a-row>
            </fieldset>
            <fieldset class="formFieldSet">
                <legend>关联信息</legend>
                <div class="tableDatas">
                    <a-table class="dialogTable" :columns="columns" :data-source="tableList" :pagination="false"
                        :loading="loading" :rowKey="(record, index) => index" :row-selection="rowSelection">
                        <template #wrstandardname="{ record }">
                            <span>{{ record.deliverygoodsname }}/{{ record.wrstandardname }}</span>
                        </template>
                        <template #unexehedgeqty="{ text }">
                            <span>{{ getDecimalsNum(text) }}</span>
                        </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 { formatValue } from '@/common/methods'
import { getChannelBuildName, getBuyOrSellName } from '@/common/constants/enumsName'
import { InternalUncorrelatedTradeDetailRsp } from '@/services/go/ermcp/hedgedItem/interface'
import { useForm } from './form'
import { BuildType, BuyOrSell } from '@/common/constants/enumCommon'
import { getDecimalsNum } from '@/utils/number'

export default defineComponent({
    emits: ['cancel'],
    props: {
        selectedRow: {
            type: Object as PropType<InternalUncorrelatedTradeDetailRsp>,
            required: true
        },
    },
    setup(props, context) {
        const { visible, cancel } = _closeModal(context);
        const { formData, rules, formElement, loading, tableList, accountList, goodsList, columns, selectedRowKeys, rowSelection, changeGoods, formSubmit } = useForm();

        // 搜索商品合约
        function filterOption(input: string, option: any) {
            return option.children[0].children.includes(input);
        }

        return {
            visible,
            loading,
            tableList,
            rules,
            columns,
            cancel,
            formatValue,
            goodsList,
            accountList,
            formData,
            selectedRowKeys,
            rowSelection,
            formElement,
            BuildType,
            BuyOrSell,
            formSubmit,
            filterOption,
            changeGoods,
            getDecimalsNum,
            getBuyOrSellName,
            getChannelBuildName,
        }
    }
})
</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>