<!-- 查询管理-内部订单查询-线下交收单查询-失败 -->
<template>
    <app-drawer :title="t('query.internal.offlinedelivery.cancel.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 #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.internal.offlinedelivery.cancel.submit')
                }}</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.OfflineRsp>,
        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.internal.offlinedelivery.cancel.deliveryorderid' },
    { prop: 'username', label: 'query.internal.offlinedelivery.cancel.username' },
    { prop: 'goodsdisplay', label: 'query.internal.offlinedelivery.cancel.goodsdisplay' },
    { prop: 'buyorsell', label: 'query.internal.offlinedelivery.cancel.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { prop: 'deliveryqty', label: 'query.internal.offlinedelivery.cancel.deliveryqty' },
    { prop: 'deliveryprice', label: 'query.internal.offlinedelivery.cancel.deliveryprice' },
    { prop: 'deliveryamount', label: 'query.internal.offlinedelivery.cancel.deliveryamount' },
    { prop: 'matchname', label: 'query.internal.offlinedelivery.cancel.matchname' },
    { prop: 'deliveryinfo', label: 'query.internal.offlinedelivery.cancel.deliveryinfo' },
    { prop: 'reqtime', label: 'query.internal.offlinedelivery.cancel.reqtime', formatValue: (val) => formatDate(val) },
    { prop: 'orderstatusdisplay', label: 'query.internal.offlinedelivery.cancel.orderstatusdisplay' },
    { prop: 'auditremark', label: 'query.internal.offlinedelivery.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.internal.offlinedelivery.cancel.tips'))
    }
}
</script>