<!-- 查询管理-订单管理-交收单管理-失败 -->
<template>
    <app-drawer :title="t('query.order.delivery.cancel.title')" width="960" v-model:show="show">
        <app-table-details :data="data" :label-width="160" :cell-props="detailProps1" :column="2">
            <template #auditremark>
                <el-input ref="inputRef" type="textarea" :rows="3" :placeholder="t('common.require')"
                    v-model="formData.auditremark" />
            </template>
        </app-table-details>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('query.order.delivery.cancel.confirm') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, reactive, PropType } from 'vue'
import { ElMessage, InputInstance } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { queryOfflineDetail, offlineFail } 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 inputRef = shallowRef<InputInstance>()

const formData = reactive<Model.OfflineFailReq>({
    auditremark: '',
    deliveryorderid: props.record.deliveryorderid,
    marketid: props.record.marketid
})

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

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

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

const onSubmit = () => {
    if (formData.auditremark) {
        loading.value = true
        offlineFail({
            data: formData
        }).then(() => {
            ElMessage.success(t('common.tips1'))
            onCancel(true)
        }).catch((err) => {
            ElMessage.error(t('common.tips2') + err)
            onCancel()
        }).finally(() => {
            loading.value = false
        })
    } else {
        inputRef.value?.focus()
        ElMessage.warning(t('query.order.delivery.cancel.tips1'))
    }
}
</script>