<!-- 交易服务-保税服务-上传盖章单据 -->
<template>
    <app-drawer title="上传盖章单据" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" label-width="80px" :model="formData" :rules="formRules">
            <el-form-item label="单据编号">
                {{ selectedRow.orderid }}
            </el-form-item>
            <el-form-item label="盖章" prop="FileDetails">
                <app-upload :limit="5" :file-types="['pdf']" @change="onUploadChange">
                    <template #tip>
                        <span>只支持pdf格式</span>
                    </template>
                </app-upload>
            </el-form-item>
        </el-form>
        <div style="font-size: 12px;color: #999;border: 1px solid #eee;padding: 15px;">
            请下载文件“出境检测发票装箱单OJK(GIA发，运营收).pdf”, 盖章后上传。</div>
        <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 { useBSFWMemberOperate } from '@/business/customs/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.GZBSFWOrderRsp>,
        required: true
    },
})

const { loading, formData, formSubmit } = useBSFWMemberOperate(props.selectedRow.orderid)
const show = shallowRef(true)
const refresh = shallowRef(false)
const formRef = shallowRef<FormInstance>()

const formRules: FormRules = {
    FileDetails: [{
        required: true,
        validator: (rule, value, callback) => {
            if (formData.FileDetails.length) {
                callback()
            } else {
                callback(new Error('请上传盖章单据'))
            }
        }
    }],
}

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

const onUploadChange = (files: { originalName: string, filePath: string }[]) => {
    formData.FileDetails = files.map((e) => ({
        FileName: e.originalName,
        FilePath: e.filePath,
    }))
}

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