<template>
    <!-- 自定义表单 -->
    <BasicModal v-bind="$attrs" @register="registerModal" title="上传文件" width="1200px" :showOkBtn="false"
        :showCancelBtn="false">
        <el-form ref="importFormRef">
            <el-form-item label="上传文件:">
                <el-upload class="upload-demo" ref="upload" action :http-request="httpRequest"
                    :before-upload="beforeUpload" :on-exceed="handleExceed" :limit="10" :on-remove="removeFile">
                    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                    <div slot="tip" class="el-upload__tip">文件大小且不超过500M</div>
                    <div slot="tip" class="el-upload__tip">必须上传《部门联审意见》</div>
                </el-upload>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitImportForm">开始导入</el-button>
                <el-button type="info" @click="dialogVisible">关闭窗口</el-button>
            </el-form-item>
        </el-form>
    </BasicModal>
</template>
<script lang="ts" name="setProjectWorkReport" setup>
import { reactive, defineEmits, onMounted, ref } from 'vue';
import { ElMessage } from 'element-plus';

import { useModalInner, BasicModal } from '@/components/Modal';
import { uploadURTFile } from '@/views/projectLib/projectPlan/projectPlan.api';
import { queryProjectInfoById } from '@/views/projectLib/projectInfo/projectInfo.api';
const [registerModal, { closeModal }] = useModalInner(init);
let fileList = reactive<Array<any>>([]);
let projectid = ref("")
async function init(data) {
    console.log('datadatadata', data);
    projectid.value = data.projectid;
}

const emit = defineEmits(['close']);
//加载项目数据
onMounted(async () => {
});

function httpRequest(option) {
    fileList.push(option);
    console.log(fileList, option);
}
function removeFile(option) {
    for (let i = 0; i < fileList.length; i++) {
        if (fileList[i].file.name == option.name) {
            fileList.splice(i, 1);
        }
    }
    console.log(fileList, option);
}
// 上传前处理
function beforeUpload(file, item) {
    console.log('file,item', file, item);
    let fileSize = file.size;
    const FIVE_M = 500 * 1024 * 1024;
    //大于5M，不允许上传
    if (fileSize > FIVE_M) {
        ElMessage.error('最大上传500M');
        return false;
    }
    // let geShi = ['xlx', 'xlsx', 'docx', 'doc', 'pdf'] as Array<string>;
    // if (geShi.indexOf(file.name.substring(file.name.indexOf('.') + 1)) == -1) {
    //     ElMessage.error('文件格式错误！仅支持' + 'xlx,xlsx, docx, doc, pdf');
    //     console.log('文件格式错误！仅支持' + 'xlsx', 'docx', 'doc', 'pdf');
    //     return false;
    // }
    // if (item.indexOf(file.name.substring(0, file.name.indexOf('.'))) == -1) {
    //     ElMessage.error('请上传文件:' + item);
    //     return false;
    // }
    console.log('文件上传成功');

    return true;
}
// 文件数量过多时提醒
function handleExceed() {
    ElMessage.warning('最多只能上传十个文件');
}
//导入Excel病种信息数据
async function submitImportForm() {
    if (fileList.length == 0) {
        ElMessage({
            message: '未选择任何文件，无法上传！',
            type: 'error',
        });
        return;
    }
    let cunzai = false;
    fileList.forEach((x) => {
        console.log('xxxxxxxxxx', x, x.file);
       if(x.file.name.includes('部门联审意见')){
           cunzai = true;
       }
    });
    if(!cunzai){
        ElMessage.error('请上传《部门联审意见》');
        return;
    }
    // // 使用form表单的数据格式
    const params = new FormData();
    // 将上传文件数组依次添加到参数paramsData中
    fileList.forEach((x) => {
        console.log('xxxxxxxxxx', x, x.file);
        params.append('file', x.file);
    });
    params.append('projectid', projectid.value);
    uploadURTFile(params).then(() => {
        emit('close');
    });
}
function dialogVisible() {
    emit('close');
}
</script>
<style></style>
