<!-- 查询管理-内部订单查询-交收成交查询-详情 -->
<template>
    <app-drawer :title="t('query.internal.deliverytrade.details.title')" width="960" v-model:show="show">
        <app-table-details :title="t('query.internal.deliverytrade.details.subtitle1')" :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="t('query.internal.deliverytrade.details.subtitle2')" :data="data" :label-width="200"
            :cell-props="detailProps2" :column="2" />
        <app-table-details :title="t('query.internal.deliverytrade.details.subtitle3')" :data="data" :label-width="200"
            :cell-props="detailProps3" :column="2" />
        <app-table-details :title="t('query.internal.deliverytrade.details.subtitle4')" :data="data" :label-width="200"
            :cell-props="detailProps4" :column="2" v-if="!!data?.ppricemode" />
        <app-table-details :title="t('query.internal.deliverytrade.details.subtitle5')" :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 { 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 buyOrSellEnum = useEnum('buyOrSell') // 方向
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 } = 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: 'query.internal.deliverytrade.details.deliveryid' },
        { prop: 'tradedate', label: 'query.internal.deliverytrade.details.tradedate', formatValue: () => formatDate(histradedate || tradedate, 'YYYY-MM-DD') },
        { prop: 'buyorsell', label: 'query.internal.deliverytrade.details.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
        { prop: 'accountid', label: 'query.internal.deliverytrade.details.accountid', formatValue: (val) => val && buyer },
        { prop: 'matchaccountid', label: 'query.internal.deliverytrade.details.matchaccountid', formatValue: (val) => val && seller },
        { prop: 'deliverytotalamount', label: 'query.internal.deliverytrade.details.deliverytotalamount' },
        { prop: 'deliveryprice', label: 'query.internal.deliverytrade.details.deliveryprice' },
        { prop: 'deliverycharge', label: 'query.internal.deliverytrade.details.deliverycharge' },
        { prop: 'deliverypricemove', label: 'query.internal.deliverytrade.details.deliverypricemove' },
        { prop: 'remaintotalamount', label: 'query.internal.deliverytrade.details.remaintotalamount' },
        { prop: 'deliverystatus', label: 'query.internal.deliverytrade.details.deliverystatus', formatValue: (val) => deliveryorderstatus1Enum.getEnumTypeName(val) },
        { prop: 'deliverytime', label: 'query.internal.deliverytrade.details.deliverytime', formatValue: (val) => formatDate(val) },
        { prop: 'feealgorithm', label: 'query.internal.deliverytrade.details.feealgorithm', formatValue: (val) => getFeeAlgorithmName(val) },
        { prop: 'memberchargevalue', label: 'query.internal.deliverytrade.details.memberchargevalue' },
        { prop: 'exchchargevalue', label: 'query.internal.deliverytrade.details.exchchargevalue' },
        { prop: 'deliveryorderid', label: 'query.internal.deliverytrade.details.deliveryorderid' },
    ]

    return result
})

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

    const result: CellProp[] = [
        { prop: 'wrstandardcode', label: 'query.internal.deliverytrade.details.wrstandardcode', formatValue: (val) => val && `${wrstandardcode}/${wrstandardname}` },
        { prop: 'deliveryqty', label: 'query.internal.deliverytrade.details.deliveryqty', formatValue: () => deliveryqty + deliverygoodsunitname },
        { prop: 'deliveryrtradeqty', label: 'query.internal.deliverytrade.details.deliveryrtradeqty', 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: 'query.internal.deliverytrade.details.xgoodsname' },
        { prop: 'xdeliveryprice', label: 'query.internal.deliverytrade.details.xdeliveryprice', decimal: 2 },
        { prop: 'xdeliverycloseqty', label: 'query.internal.deliverytrade.details.xdeliverycloseqty' },
    ]

    return result
})

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

    const result: CellProp[] = [
        { prop: 'ppricemode', label: 'query.internal.deliverytrade.details.ppricemode' },
        { prop: 'pgoodsname', label: 'query.internal.deliverytrade.details.pgoodsname', show: ppricemode === 1 },
        { prop: 'pdeliveryprice', label: 'query.internal.deliverytrade.details.pdeliveryprice', decimal: 2 },
        { prop: 'pdeliverycloseqty', label: 'query.internal.deliverytrade.details.pdeliverycloseqty' },
    ]

    return result
})

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

    const result: CellProp[] = [
        { prop: 'p2pricemode', label: 'query.internal.deliverytrade.details.p2pricemode' },
        { prop: 'p2goodsname', label: 'query.internal.deliverytrade.details.p2goodsname', show: p2pricemode === 1 },
        { prop: 'p2deliveryprice', label: 'query.internal.deliverytrade.details.p2deliveryprice', decimal: 2 },
        { prop: 'p2deliverycloseqty', label: 'query.internal.deliverytrade.details.p2deliverycloseqty' },
    ]

    return result
})

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