<!-- 查询管理-内部订单查询-线下交收单查询-确认 -->
<template>
    <app-drawer 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">确认交收</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 { getBuyOrSellName } from '@/constants/order'
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 { 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: '交收单号：' },
    { prop: 'username', label: '申请人：' },
    { prop: 'goodsdisplay', label: '交收商品：' },
    { prop: 'buyorsell', label: '方向：', formatValue: (val) => getBuyOrSellName(val) },
    { prop: 'deliveryqty', label: '交收数量：' },
    { prop: 'deliveryprice', label: '交收价格：' },
    { prop: 'deliveryamount', label: '交收货款：' },
    { prop: 'matchname', label: '对手方：' },
    { prop: 'deliveryinfo', label: '交收信息：' },
    { prop: 'reqtime', label: '申请时间：', formatValue: (val) => formatDate(val) },
    { prop: 'orderstatusdisplay', label: '交收状态：' }
]

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>