<template>
    <el-form :inline="true" ref="formRef" :model="addData" label-width="auto" class="form-one" :rules="rules">
        <el-form-item label="区域:">
            <Area ref="areaRef" v-model="addData.orgNo" />
        </el-form-item>

        <el-form-item label="终端编号:" prop="assetNumber">
            <el-input v-model="addData.assetNumber" clearable placeholder="请输入关键字"></el-input>
        </el-form-item>

<!--        <el-form-item label="资产编号:" prop="devNumber">-->
<!--            <el-input v-model="addData.devNumber" clearable placeholder="请输入关键字"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item class="item" label="报告:" style="margin-top: 10px" prop="fileList">-->
<!--            <el-upload-->
<!--                v-model:file-list="addData.fileList"-->
<!--                ref="upload"-->
<!--                action=""-->
<!--                :limit="1"-->
<!--                :on-exceed="handleExceed"-->
<!--                :auto-upload="false"-->
<!--            >-->
<!--                <template #trigger>-->
<!--                    <el-button type="primary">上传文件</el-button>-->
<!--                </template>-->
<!--            </el-upload>-->
<!--        </el-form-item>-->

<!--        <el-form-item label="选择审核人:" prop="checker">-->
<!--            <el-select v-model="addData.checker" clearable placeholder="请选择审核人">-->
<!--                <el-option v-for="item in auditList" :key="item.id" :label="item.name" :value="item.id"></el-option>-->
<!--            </el-select>-->
<!--        </el-form-item>-->
    </el-form>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useDictData } from '@/stores/dictData'
import Area from '@/components/form/area/index.vue'
import { uploadFile, addFlow, update } from '@/api/process-boot/retire'
import { getUserByRoleType } from '@/api/user-boot/user'
import { UploadProps, UploadRawFile, genFileId, ElMessage } from 'element-plus'

const dictData = useDictData()

const interferenceType = dictData.getBasicData('Interference_Source')
const emit = defineEmits(['onSubmit'])
const rules = reactive({
    assetNumber: [{ required: true, message: '请输入终端编号', trigger: 'blur' }],
    devNumber: [{ required: true, message: '请输入资产编号', trigger: 'blur' }],

    checker: [{ required: true, message: '请选择审核人', trigger: 'change' }],
    fileList: [{ required: true, message: '请选择文件', trigger: 'change' }]
})
const title: any = ref('')
const auditList: any = ref([])

const addData: any = ref({
    orgNo: dictData.state.area[0].id,
    assetNumber: '',
    devNumber: '',
    fileList: [],
    checker: '',
    applyType: 4,
    type: 5
})
const formRef = ref()
// 新增
const config = () => {
    formRef.value.validate(async (valid: any) => {
        if (valid) {
            if (title.value == '新增终端退役申请单') {
                let form = new FormData()
                form.append('file', addData.value.fileList[0].raw)
                await uploadFile(form).then((res: any) => {
                    addData.value.fileName = res.data.minFileName
                    addData.value.filePath = res.data.minFileUrl
                })
                addData.value.applicationFormText = JSON.stringify({
                    assetNumber: addData.value.assetNumber,
                    devNumber: addData.value.devNumber
                })
                addData.value.operate = 1
                addData.value.checkerName = auditList.value.find((item: any) => item.id == addData.value.checker).name
                addFlow(addData.value).then((res: any) => {
                    ElMessage.success('新增成功!')
                    cancel()
                })
            } else if (title.value == '编辑终端退役申请单') {
                if (addData.value.fileList[0].raw != undefined) {
                    let form = new FormData()
                    form.append('file', addData.value.fileList[0].raw)
                    await uploadFile(form).then((res: any) => {
                        addData.value.fileName = res.data.minFileName
                        addData.value.filePath = res.data.minFileUrl
                    })
                }
                addData.value.applicationFormText = JSON.stringify({
                    assetNumber: addData.value.assetNumber,
                    devNumber: addData.value.devNumber
                })
                addData.value.operate = 1
                addData.value.checkerName = auditList.value.find((item: any) => item.id == addData.value.checker).name
                update(addData.value).then((res: any) => {
                    ElMessage.success('修改成功!')

                    cancel()
                })
            }
        }
    })
}
// 审核
const audit = () => {}
// 重置
const cancel = () => {
    formRef.value.resetFields()
    emit('onSubmit')
}
const open = (text: string, row?: any) => {
    if (row) {
        addData.value = JSON.parse(JSON.stringify(row))
        addData.value.assetNumber = row.applyForm.assetNumber
        addData.value.devNumber = row.applyForm.devNumber
        addData.value.fileList = [{ name: row.fileName, url: row.filePath }]
    } else {
        addData.value = {
            orgNo: dictData.state.area[0].id,
            assetNumber: '',
            devNumber: '',
            fileList: [],
            checker: '',
            applyType: 4,
            type: 5
        }
    }
    formRef.value.resetFields()
    title.value = text
}

// 取消
onMounted(() => {
    getUserByRoleType(3).then(res => {
        auditList.value = res.data
    })
})
const upload = ref()

// 上传
const handleExceed: UploadProps['onExceed'] = files => {
    upload.value!.clearFiles()
    const file = files[0] as UploadRawFile
    file.uid = genFileId()
    upload.value!.handleStart(file)
}

defineExpose({ open, config, audit })
</script>
<style lang="scss" scoped>
:deep(.el-upload-list__item) {
    width: 300px;
}
</style>
