<!-- 查询管理-订单管理-交收单管理-确认 -->
<template>
    <app-drawer :title="t('query.order.delivery.confirm.title')" width="960" v-model:show="show">
        <app-table-details :data="data" :label-width="160" :cell-props="detailProps1" :column="2" />
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('query.order.delivery.confirm.confirm') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { queryOfflineDetail, offlineClose } 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.DeliveryOrderListRsp>,
        required: true
    }
})

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

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

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

const detailProps1: CellProp[] = [
    { prop: 'deliveryorderid', label: 'query.order.delivery.confirm.deliveryorderid' },
    { prop: 'username', label: 'query.order.delivery.confirm.username' },
    { prop: 'goodsdisplay', label: 'query.order.delivery.confirm.goodsdisplay' },
    { prop: 'buyorsell', label: 'query.order.delivery.confirm.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { prop: 'deliveryqty', label: 'query.order.delivery.confirm.deliveryqty' },
    { prop: 'deliveryprice', label: 'query.order.delivery.confirm.deliveryprice' },
    { prop: 'deliveryamount', label: 'query.order.delivery.confirm.deliveryamount' },
    { prop: 'matchname', label: 'query.order.delivery.confirm.matchname' },
    { prop: 'deliveryinfo', label: 'query.order.delivery.confirm.deliveryinfo' },
    { prop: 'reqtime', label: 'query.order.delivery.confirm.reqtime', formatValue: (val) => formatDate(val) },
    { prop: 'orderstatusdisplay', label: 'query.order.delivery.confirm.orderstatusdisplay' }
]

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

const onSubmit = () => {
    loading.value = true
    offlineClose({
        data: {
            deliveryorderid: props.record.deliveryorderid,
            marketid: props.record.marketid
        }
    }).then(() => {
        ElMessage.success(t('common.tips1'))
        onCancel(true)
    }).catch((err) => {
        ElMessage.error(t('common.tips2') + err)
        onCancel()
    }).finally(() => {
        loading.value = false
    })
}
</script>