<!-- 查询管理-内部订单查询-线下交收单查询-确认 -->
<template>
    <app-drawer :title="t('query.internal.offlinedelivery.confirm.title')" width="960" v-model:show="show"
        :refresh="refresh" :loading="loading">
        <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.internal.offlinedelivery.confirm.submit')
                }}</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.OfflineRsp>,
        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.internal.offlinedelivery.confirm.deliveryorderid' },
    { prop: 'username', label: 'query.internal.offlinedelivery.confirm.username' },
    { prop: 'goodsdisplay', label: 'query.internal.offlinedelivery.confirm.goodsdisplay' },
    { prop: 'buyorsell', label: 'query.internal.offlinedelivery.confirm.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { prop: 'deliveryqty', label: 'query.internal.offlinedelivery.confirm.deliveryqty' },
    { prop: 'deliveryprice', label: 'query.internal.offlinedelivery.confirm.deliveryprice' },
    { prop: 'deliveryamount', label: 'query.internal.offlinedelivery.confirm.deliveryamount' },
    { prop: 'matchname', label: 'query.internal.offlinedelivery.confirm.matchname' },
    { prop: 'deliveryinfo', label: 'query.internal.offlinedelivery.confirm.deliveryinfo' },
    { prop: 'reqtime', label: 'query.internal.offlinedelivery.confirm.reqtime', formatValue: (val) => formatDate(val) },
    { prop: 'orderstatusdisplay', label: 'query.internal.offlinedelivery.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('提交成功')
        onCancel(true)
    }).catch((err) => {
        ElMessage.error('提交失败：' + err)
        onCancel()
    }).finally(() => {
        loading.value = false
    })
}
</script>