<!-- 查询管理-内部订单查询-持仓单查询-详情 -->
<template>
    <app-drawer :title="t('query.internal.position.details.title')" width="900" v-model:show="show">
        <app-table-details :data="data" :label-width="120" :cell-props="detailProps" :column="2" />
        <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 } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { queryPositionSheetDetail } from '@/services/api/order'
import { CellProp } from '@pc/components/base/table-details/types'
import AppDrawer from '@pc/components/base/drawer/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.PositionSheetRsp>,
        required: true
    }
})

const buyOrSellEnum = useEnum('buyOrSell') // 方向

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

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

const detailProps = computed<CellProp[]>(() => {
    const { trademode = 0, expiretype, accountname } = data.value ?? {}
    const isTradeMode46 = trademode === 46
    const isTradeMode18Or26 = [18, 26].includes(trademode)

    return [
        { prop: 'tradeid', label: 'query.internal.position.details.tradeid' },
        { prop: 'tradedate', label: 'query.internal.position.details.tradedate' },
        { prop: 'accountid', label: 'query.internal.position.details.accountid', formatValue: (val) => val && `${accountname} (${val})` },
        { prop: 'parentusername', label: 'query.internal.position.details.parentusername' },
        { prop: 'memberusername', label: 'query.internal.position.details.memberusername' },
        { prop: 'marketname', label: 'query.internal.position.details.marketname' },
        { prop: 'goodscode', label: 'query.internal.position.details.goodsname' },
        { prop: 'optiontype', label: 'query.internal.position.details.optiontype', show: isTradeMode18Or26 },
        { prop: 'buyorsell', label: 'query.internal.position.details.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
        { prop: 'holderdays', label: 'query.internal.position.details.holderdays', show: !isTradeMode18Or26 },
        { prop: 'tradetime', label: 'query.internal.position.details.tradetime', formatValue: (val) => formatDate(val) },
        { prop: 'openprice', label: 'query.internal.position.details.openprice', show: !isTradeMode18Or26 },
        { prop: 'holderprice', label: 'query.internal.position.details.holderprice', show: !isTradeMode18Or26 },
        { prop: 'openprice', label: 'query.internal.position.details.openprice1', show: isTradeMode18Or26 },
        { prop: 'premium', label: 'query.internal.position.details.premium', show: isTradeMode18Or26 },
        { prop: 'openqty', label: 'query.internal.position.details.openqty' },
        { prop: 'holderqty', label: 'query.internal.position.details.holderqty' },
        { prop: 'expiretype', label: 'query.internal.position.details.expiretype', show: isTradeMode18Or26 },
        { prop: 'expirecycle', label: 'query.internal.position.details.expirecycle', show: isTradeMode18Or26 && expiretype === 1, formatValue: (val) => val && `${val}天` },
        { prop: 'expiredate', label: 'query.internal.position.details.expiredate', show: isTradeMode18Or26 && expiretype === 2 },
        { prop: 'refgoodsid', label: 'query.internal.position.details.refgoodsid', show: isTradeMode46 },
        { prop: 'swapdays', label: 'query.internal.position.details.swapdays', show: isTradeMode46 },
    ]
})

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