<!-- 查询管理-内部订单查询-交收委托查询-详情 -->
<template>
    <app-drawer title="交收委托(点选式)详细信息" width="960" v-model:show="show">
        <app-table-details title="申报信息" :data="data" :label-width="200" :cell-props="detailProps1" :column="2" />
        <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 } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { queryDeliveryOrderDetail } 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.DeliveryOrderRsp>,
        required: true
    }
})

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

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

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

const detailProps1 = computed(() => {
    const { deliveryorderstatus = 0, histradedate, tradedate, accountname, accountid, matchaccountname, matchaccountid } = data.value ?? {}

    const result: CellProp[] = [
        { prop: 'deliveryticket', label: '申报流水号：' },
        { prop: 'deliveryorderid', label: '委托单号：' },
        { prop: 'tradedate', label: '交易日：', formatValue: () => formatDate(histradedate || tradedate, 'YYYY-MM-DD') },
        { prop: 'accountid', label: '申报人(账号)：', formatValue: (val) => val && `${accountname}(${accountid})` },
        { prop: 'matchaccountid', label: '对手(账号)：', formatValue: (val) => val && `${matchaccountname}(${matchaccountid})` },
        { prop: 'deliveryorderstatus', label: '交收状态：', formatValue: (val) => deliveryorderstatus1Enum.getEnumTypeName(val) },
        { prop: 'ordertime', label: '申报时间：', formatValue: (val) => formatDate(val) },
        { prop: 'auditname', label: '审核人：', show: [2, 3].includes(deliveryorderstatus) },
        { prop: 'audittime', label: '审核时间：', formatValue: (val) => formatDate(val), show: [2, 3].includes(deliveryorderstatus) },
        { prop: 'auditremark', label: '备注：', show: deliveryorderstatus === 3 },
        { prop: 'retcode', label: '错误信息：', show: deliveryorderstatus === 5 }
    ]

    return result
})

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

    const result: CellProp[] = [
        { prop: 'wrstandardcode', label: '现货商品：', formatValue: (val) => val && `${wrstandardcode}/${wrstandardname}` },
        { prop: 'deliveryqty', label: '现货数量：', formatValue: () => deliveryqty + 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: 'xdeliveryqty', 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, show: ppricemode === 2 },
        { prop: 'pdeliveryqty', 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, show: p2pricemode === 2 },
        { prop: 'p2deliveryqty', label: '合约手数：' },
    ]

    return result
})

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