<!-- 保税仓业务-上传 -->
<template>
    <app-drawer title="上传发票/装箱单" :width="480" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item label="单据编号">
                {{ selectedRow.orderid }}
            </el-form-item>
            <el-form-item label="盖章发票" prop="invoiceFile">
                <app-upload :file-types="['pdf', 'word', 'excel']" @change="onUploadInvoice">
                    <template #tip>
                        <span>支持Word、excel、pdf格式</span>
                    </template>
                </app-upload>
            </el-form-item>
            <el-form-item label="盖章装箱单" prop="orderFile">
                <app-upload :file-types="['pdf', 'word', 'excel']" @change="onUploadOrder">
                    <template #tip>
                        <span>支持Word、excel、pdf格式</span>
                    </template>
                </app-upload>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)" plain>取消</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 type { FormInstance, FormRules } from 'element-plus'
import { useBscUploadFile } from '@/business/bonded'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppUpload from '@pc/components/base/upload/index.vue'

const props = defineProps({
    selectedRow: {
        type: Object as PropType<Ermcp.GzbscinOutOrderRsp>,
        required: true
    },
})

const { loading, formData, formSubmit } = useBscUploadFile(props.selectedRow.orderid)
const show = shallowRef(true)
const refresh = shallowRef(false)
const formRef = shallowRef<FormInstance>()
const invoiceFile = shallowRef<Proto.FileDetail>() // 盖章发票
const orderFile = shallowRef<Proto.FileDetail>() // 盖章装箱单

const formRules: FormRules = {
    invoiceFile: [{
        required: true,
        validator: (rule, value, callback) => {
            if (invoiceFile.value?.FilePath) {
                callback()
            } else {
                callback(new Error('请上传盖章发票'))
            }
        }
    }],
    orderFile: [{
        required: true,
        validator: (rule, value, callback) => {
            if (orderFile.value?.FilePath) {
                callback()
            } else {
                callback(new Error('请上传盖章装箱单'))
            }
        }
    }],
}

// 上传盖章发票
const onUploadInvoice = (file: { originalName: string, filePath: string }) => {
    invoiceFile.value = {
        FileName: file.originalName,
        FilePath: file.filePath,
    }
    formRef.value?.validateField('invoiceFile')
}

// 上传盖章装箱单
const onUploadOrder = (file: { originalName: string, filePath: string }) => {
    orderFile.value = {
        FileName: file.originalName,
        FilePath: file.filePath,
    }
    formRef.value?.validateField('orderFile')
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            if (invoiceFile.value && orderFile.value) {
                formData.FileDetails = [invoiceFile.value, orderFile.value]
            }
            formSubmit().then(() => {
                ElMessage.success('提交成功')
                onCancel(true)
            }).catch((err) => {
                ElMessage.error('提交失败：' + err)
            })
        }
    })
}
</script>