<!-- 查询管理-内部订单查询-交收委托查询-详情 -->
<template>
    <app-drawer :title="t('query.internal.deliveryorder.details.title')" width="960" v-model:show="show">
        <app-table-details :title="t('query.internal.deliveryorder.details.subtitle1')" :data="data" :label-width="200" :cell-props="detailProps1" :column="2" />
        <app-table-details :title="t('query.internal.deliveryorder.details.subtitle2')" :data="data" :label-width="200" :cell-props="detailProps2" :column="2" />
        <app-table-details :title="t('query.internal.deliveryorder.details.subtitle3')" :data="data" :label-width="200" :cell-props="detailProps3" :column="2" />
        <app-table-details :title="t('query.internal.deliveryorder.details.subtitle4')" :data="data" :label-width="200" :cell-props="detailProps4" :column="2"
            v-if="!!data?.ppricemode" />
        <app-table-details :title="t('query.internal.deliveryorder.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 } 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: 'query.internal.deliveryorder.details.deliveryticket' },
        { prop: 'deliveryorderid', label: 'query.internal.deliveryorder.details.deliveryorderid' },
        { prop: 'tradedate', label: 'query.internal.deliveryorder.details.tradedate', formatValue: () => formatDate(histradedate || tradedate, 'YYYY-MM-DD') },
        { prop: 'accountid', label: 'query.internal.deliveryorder.details.accountid', formatValue: (val) => val && `${accountname}(${accountid})` },
        { prop: 'matchaccountid', label: 'query.internal.deliveryorder.details.matchaccountid', formatValue: (val) => val && `${matchaccountname}(${matchaccountid})` },
        { prop: 'deliveryorderstatus', label: 'query.internal.deliveryorder.details.deliveryorderstatus', formatValue: (val) => deliveryorderstatus1Enum.getEnumTypeName(val) },
        { prop: 'ordertime', label: 'query.internal.deliveryorder.details.ordertime', formatValue: (val) => formatDate(val) },
        { prop: 'auditname', label: 'query.internal.deliveryorder.details.auditname', show: [2, 3].includes(deliveryorderstatus) },
        { prop: 'audittime', label: 'query.internal.deliveryorder.details.audittime', formatValue: (val) => formatDate(val), show: [2, 3].includes(deliveryorderstatus) },
        { prop: 'auditremark', label: 'query.internal.deliveryorder.details.auditremark', show: deliveryorderstatus === 3 },
        { prop: 'retcode', label: 'query.internal.deliveryorder.details.retcode', show: deliveryorderstatus === 5 }
    ]

    return result
})

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

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

    return result
})

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

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

    return result
})

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

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

    return result
})

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