<!-- 查询管理-内部订单查询-成交单查询-详情 -->
<template>
    <app-drawer :title="t('query.internal.trade.details.title')" width="960" v-model:show="show">
        <app-table-details :title="t('query.internal.trade.details.subtitle1')" :data="data" :label-width="200"
            :cell-props="detailProps1" :column="2" />
        <app-table-details :title="t('query.internal.trade.details.subtitle2')" :data="data" :label-width="200"
            :cell-props="detailProps2" :column="2" />
        <app-table :data="data.resultList" :columns="tableColumns" v-if="data?.buildtype === 2">
            <template #headerLeft>
                <b>{{ t('query.internal.trade.details.subtitle3') }}</b>
            </template>
        </app-table>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate, parseTenThousand } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { queryContractSheetDetailDetail } from '@/services/api/order'
import { CellProp } from '@pc/components/base/table-details/types'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppTable from '@pc/components/base/table/index.vue'
import AppTableDetails from '@pc/components/base/table-details/index.vue'
import { i18n } from '@/stores'

const props = defineProps({
    record: {
        type: Object as PropType<Model.ContractSheetRsp>,
        required: true
    }
})

const { global: { t } } = i18n
const show = shallowRef(true)
const refresh = shallowRef(false)

const buyOrSellEnum = useEnum('buyOrSell') // 方向
const buildtypeEnum = useEnum('buildtype') // 成交单类型
const tradetypeEnum = useEnum('tradetype') // 成交类别

const { data } = useRequest(queryContractSheetDetailDetail, {
    params: {
        ishis: props.record.ishis,
        tradeid: props.record.tradeid,
        buyorsell: props.record.buyorsell
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const detailProps1 = computed<CellProp[]>(() => {
    const { trademode = 0, nameType, membername, accountname, tradetype3, matchaccountid } = data.value ?? {}
    const isTradeMode46 = trademode === 46
    const isTradeMode18Or26 = [18, 26].includes(trademode)

    return [
        { prop: 'tradeid', label: 'query.internal.trade.details.tradeid' },
        { prop: 'tradedate', label: 'query.internal.trade.details.tradedate' },
        { prop: 'accountid', label: 'query.internal.trade.details.accountid', formatValue: (val) => `${nameType === 2 ? membername : accountname} (${val})` },
        { prop: 'orderid', label: 'query.internal.trade.details.orderid' },
        { prop: 'parentusername', label: 'query.internal.trade.details.parentusername' },
        { prop: 'membername', label: 'query.internal.trade.details.membername' },
        { prop: 'marketname', label: 'query.internal.trade.details.marketname' },
        { prop: 'goodscode', label: 'query.internal.trade.details.goodsname' },
        { prop: 'buildtype', label: 'query.internal.trade.details.buildtype', formatValue: (val) => tradetype3 === 1 && val === 3 ? '执行交割' : buildtypeEnum.getEnumTypeName(val) },
        { prop: 'buyorsell', label: 'query.internal.trade.details.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
        { prop: 'tradeprice', label: 'query.internal.trade.details.tradeprice', show: !isTradeMode18Or26 },
        { prop: 'optiontype', label: 'query.internal.trade.details.optiontype', show: isTradeMode18Or26 },
        { prop: 'isconfirmexercise', label: 'query.internal.trade.details.isconfirmexercise', show: isTradeMode18Or26 },
        { prop: 'tradeprice', label: 'query.internal.trade.details.tradeprice1', show: isTradeMode18Or26 },
        { prop: 'premium', label: 'query.internal.trade.details.premium', show: isTradeMode18Or26 },
        { prop: 'tradeqty', label: 'query.internal.trade.details.tradeqty' },
        { prop: 'tradeamount', label: 'query.internal.trade.details.tradeamount' },
        { prop: 'closepl', label: 'query.internal.trade.details.closepl' },
        { prop: 'tradetype', label: 'query.internal.trade.details.tradetype', formatValue: (val) => tradetypeEnum.getEnumTypeName(val) },
        { prop: 'tradetime', label: 'query.internal.trade.details.tradetime', formatValue: (val) => formatDate(val) },
        { prop: 'matchaccountname', label: 'query.internal.trade.details.matchaccountname', formatValue: (val) => val && `${val} (${matchaccountid})` },
        { prop: 'refgoodsid', label: 'query.internal.trade.details.refgoodsid', show: isTradeMode46 },
        { prop: 'swapdays', label: 'query.internal.trade.details.swapdays', show: isTradeMode46 },
    ]
})

const detailProps2 = computed<CellProp[]>(() => {
    const { trademode = 0, buildtype = 0, tradetype = 0, isconfirmexercise, openfeealgorithm, closefeealgorithm, closepl = 0, premium = 0, tradeqty = 0, buyorsell = 0 } = data.value ?? {}
    const isTradeMode18Or26 = [18, 26].includes(trademode)
    const result: CellProp[] = []

    if (buildtype === 1) {
        result.push(
            { prop: 'opencharge', label: 'query.internal.trade.details.opencharge' },
            {
                prop: 'openexchagechargevalue',
                label: 'query.internal.trade.details.openexchagechargevalue',
                formatValue: (val) => openfeealgorithm ? openfeealgorithm === 1 ? parseTenThousand(val) : val : '',
            },
            {
                prop: 'openmemberchargevalue',
                label: 'query.internal.trade.details.openmemberchargevalue',
                formatValue: (val) => openfeealgorithm ? openfeealgorithm === 1 ? parseTenThousand(val) : val : '',
            },
        )
    }

    if (buildtype === 2) {
        result.push(
            { prop: 'closepl', label: 'query.internal.trade.details.closepl' },
            {
                prop: 'actuallypl',
                label: 'query.internal.trade.details.actuallypl',
                formatValue: () => {
                    if (isconfirmexercise === 1) return 0
                    const direction = buyorsell ? -1 : 1
                    // 实际盈亏 = 行权盈亏+ (定货金*数量*方向(买 1;卖-1))
                    return (closepl * 10000 + (premium * tradeqty * direction) * 10000) / 10000
                },
                show: isTradeMode18Or26
            },
        )
        if (tradetype !== 3) {
            result.push(
                { prop: 'closecharge', label: 'query.internal.trade.details.closecharge' },
                {
                    prop: 'closeexchagechargevalue',
                    label: 'query.internal.trade.details.closeexchagechargevalue',
                    formatValue: (val) => closefeealgorithm ? closefeealgorithm === 1 ? parseTenThousand(val) : val : '',
                },
                {
                    prop: 'closememberchargevalue',
                    label: 'query.internal.trade.details.closememberchargevalue',
                    formatValue: (val) => closefeealgorithm ? closefeealgorithm === 1 ? parseTenThousand(val) : val : '',
                },
            )
        }
    }

    if (buildtype === 3) {
        result.push(
            { prop: 'closeqty', label: 'query.internal.trade.details.closeqty' },
            { prop: 'openqty', label: 'query.internal.trade.details.openqty' },
            { prop: 'closepl', label: 'query.internal.trade.details.closepl' },
            { prop: 'opencharge', label: 'query.internal.trade.details.opencharge' },
            { prop: 'closecharge', label: 'query.internal.trade.details.closecharge' },
            { prop: 'openfeealgorithm', label: 'query.internal.trade.details.openfeealgorithm' },
            {
                prop: 'openmemberchargevalue',
                label: 'query.internal.trade.details.openmemberchargevalue',
                formatValue: (val) => openfeealgorithm ? openfeealgorithm === 1 ? parseTenThousand(val) : val : '',
            },
            {
                prop: 'openmemberchargevalue',
                label: 'query.internal.trade.details.openmemberchargevalue',
                formatValue: (val) => openfeealgorithm ? openfeealgorithm === 1 ? parseTenThousand(val) : val : '',
            },
            { prop: 'closefeealgorithm', label: 'query.internal.trade.details.closefeealgorithm' },
            {
                prop: 'closeexchagechargevalue',
                label: 'query.internal.trade.details.closeexchagechargevalue',
                formatValue: (val) => closefeealgorithm ? closefeealgorithm === 1 ? parseTenThousand(val) : val : '',
            },
            {
                prop: 'closememberchargevalue',
                label: 'query.internal.trade.details.closememberchargevalue',
                formatValue: (val) => closefeealgorithm ? closefeealgorithm === 1 ? parseTenThousand(val) : val : '',
            },
        )
    }

    return result
})

const tableColumns = computed<Model.TableColumn[]>(() => {
    const { trademode = 0 } = data.value ?? {}
    const isTradeMode18Or26 = [18, 26].includes(trademode)

    return [
        { field: 'opentradeid', label: 'query.internal.trade.details.opentradeid' },
        { field: 'buyorsell', label: 'query.internal.trade.details.buyorsell1', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
        { field: 'openprice', label: 'query.internal.trade.details.openprice', show: !isTradeMode18Or26 },
        { field: 'holderprice', label: 'query.internal.trade.details.holderprice', show: !isTradeMode18Or26 },
        { field: 'closeprice', label: 'query.internal.trade.details.closeprice', show: isTradeMode18Or26 },
        { field: 'closetime', label: 'query.internal.trade.details.closetime', formatValue: (val) => formatDate(val) },
    ]
})

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}
</script>