<!-- 查询管理-内部订单查询-交收成交查询-详情 -->
<template>
    <app-drawer title="详细" width="960" v-model:show="show">
        <app-table-details title="交收单信息" :data="data" :label-width="200" :cell-props="detailProps1" :column="2">
            <!-- 会员服务费设置值 -->
            <template #memberchargevalue v-if="data">
                <template v-if="data.memberchargevalue">
                    <span v-if="data.feealgorithm === FeeAlgorithm.Ratio">
                        {{ parseTenThousand(data.memberchargevalue) }}
                    </span>
                    <span v-else>{{ data.memberchargevalue }}</span>
                </template>
                <template v-else>
                    {{ handleNoneValue() }}
                </template>
            </template>
            <!-- 平台服务费设置值 -->
            <template #exchchargevalue v-if="data">
                <template v-if="data.exchchargevalue">
                    <span v-if="data.feealgorithm === FeeAlgorithm.Ratio">
                        {{ parseTenThousand(data.exchchargevalue) }}
                    </span>
                    <span v-else>{{ data.exchchargevalue }}</span>
                </template>
                <template v-else>
                    {{ handleNoneValue() }}
                </template>
            </template>
        </app-table-details>
        <app-table-details title="仓单信息" :data="data" :label-width="200" :cell-props="detailProps2" :column="2" />
        <app-table-details title="交易合约" :data="data" :label-width="200" :cell-props="detailProps3" :column="2" />
        <app-table-details title="辅助合约一" :data="data" :label-width="200" :cell-props="detailProps4" :column="2"
            v-if="!!data?.ppricemode" />
        <app-table-details title="辅助合约二" :data="data" :label-width="200" :cell-props="detailProps5" :column="2"
            v-if="!!data?.p2pricemode" />
        <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, handleNoneValue } from '@/filters'
import { getBuyOrSellName, BuyOrSell, getFeeAlgorithmName, FeeAlgorithm } from '@/constants/order'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { querySettlementDealDetail } 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.SettlementDealRsp>,
        required: true
    }
})

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

const deliveryorderstatus1Enum = useEnum('deliveryorderstatus1') // 状态

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

const detailProps1 = computed(() => {
    const { buyorsell, histradedate, tradedate, accountname, accountid, matchaccountname, matchaccountid, feealgorithm } = data.value ?? {}
    const buyer = buyorsell === BuyOrSell.Buy ? `${accountname}(${accountid})` : `${matchaccountname}(${matchaccountid})`
    const seller = buyorsell === BuyOrSell.Buy ? `${matchaccountname}(${matchaccountid})` : `${accountname}(${accountid})`

    const result: CellProp[] = [
        { prop: 'deliveryid', label: '交收单号：' },
        { prop: 'tradedate', label: '交易日：', formatValue: () => formatDate(histradedate || tradedate, 'YYYY-MM-DD') },
        { prop: 'buyorsell', label: '方向：', formatValue: (val) => getBuyOrSellName(val) },
        { prop: 'accountid', label: '买方(账号)：', formatValue: (val) => val && buyer },
        { prop: 'matchaccountid', label: '卖方(账号)：', formatValue: (val) => val && seller },
        { prop: 'deliverytotalamount', label: '交收总金额：' },
        { prop: 'deliveryprice', label: '交收单价：' },
        { prop: 'deliverycharge', label: '交收服务费：' },
        { prop: 'deliverypricemove', label: '升贴水总额：' },
        { prop: 'remaintotalamount', label: '剩余总金额：' },
        { prop: 'deliverystatus', label: '状态：', formatValue: (val) => deliveryorderstatus1Enum.getEnumTypeName(val) },
        { prop: 'deliverytime', label: '成交时间：', formatValue: (val) => formatDate(val) },
        { prop: 'feealgorithm', label: '服务费收取方式：', formatValue: (val) => getFeeAlgorithmName(val) },
        { prop: 'memberchargevalue', label: '会员服务费设置值：' },
        { prop: 'exchchargevalue', label: '平台服务费设置值：' },
        { prop: 'deliveryorderid', label: '申报单号：' },
    ]

    return result
})

const detailProps2 = computed(() => {
    const { deliveryqty = 0, deliveryrtradeqty = 0, deliverygoodsunitname = '', wrstandardcode, wrstandardname } = data.value ?? {}

    const result: CellProp[] = [
        { prop: 'wrstandardcode', label: '现货商品：', formatValue: (val) => val && `${wrstandardcode}/${wrstandardname}` },
        { prop: 'deliveryqty', label: '申报数量：', formatValue: () => deliveryqty + deliverygoodsunitname },
        { prop: 'deliveryrtradeqty', label: '成交数量：', formatValue: () => deliveryrtradeqty + deliverygoodsunitname },
    ]

    data.value?.wrFactorTypeItemList.forEach((item) => {
        if (item.dgfactoryitemtypename) {
            result.push({ prop: item.dgfactoryitemid.toString(), label: item.dgfactoryitemtypename + '：', formatValue: () => item.dgfactoryitemname })
        }
    })

    return result
})

const detailProps3 = computed(() => {
    const result: CellProp[] = [
        { prop: 'xgoodsname', label: '交易合约：' },
        { prop: 'xdeliveryprice', label: '交收单价：', decimal: 2 },
        { prop: 'xdeliverycloseqty', label: '订单手数：' },
    ]

    return result
})

const detailProps4 = computed(() => {
    const { ppricemode } = data.value ?? {}

    const result: CellProp[] = [
        { prop: 'ppricemode', label: '价格方式：' },
        { prop: 'pgoodsname', label: '辅助合约一：', show: ppricemode === 1 },
        { prop: 'pdeliveryprice', label: '交收价格：', decimal: 2 },
        { prop: 'pdeliverycloseqty', label: '订单手数：' },
    ]

    return result
})

const detailProps5 = computed(() => {
    const { p2pricemode } = data.value ?? {}

    const result: CellProp[] = [
        { prop: 'p2pricemode', label: '价格方式：' },
        { prop: 'p2goodsname', label: '辅助合约二：', show: p2pricemode === 1 },
        { prop: 'p2deliveryprice', label: '交收价格：', decimal: 2 },
        { prop: 'p2deliverycloseqty', label: '订单手数：' },
    ]

    return result
})

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