<template>
<div class=''>
    <el-upload
    size="mini"
        class="upload-demo"
        action="#"
        drag
        :limit="limit"
        :on-preview="handlePreview"
        :file-list="fileList"
        :auto-upload="false"
        :before-upload="beforeUpload"
        :on-remove="handleRemove"
        :on-exceed="handleExceed"
        :on-change="handleChange"
        :on-success="handleSuccess"
        :on-error="handleError"
    >
    <i class="el-icon-upload" style="font-size:60px;line-height:0px;"></i>
    <div class="el-upload__text">将Excel文件拖到此处，或<em>点击上传</em></div>
    </el-upload>

    <p style="text-align: right;margin-top:18px;">
    <el-button type="primary" size="mini"  @click="onSubmit">导入信息</el-button>
    </p>
</div>
</template>

<script>
import { postMethodCommon } from '../../../api/common';


export default {
   components: {},
   props:[],
   data() {
    return {
        limit:1,
        fileList:[],
        form:{},
    };
   },
   computed: {},
   watch: {},
   methods: {
        //文件上传时的钩子
        handlePreview(file) {

        },
        // 删除文件
        handleRemove(file, fileList) {
        this.fileList = fileList
        },
        // 文件上传成功
        handleSuccess(res, file, fileList) {
        this.$message.success('文件上传成功')
        },
        // 文件超出个数限制
        handleExceed(files, fileList) {
        this.$message.warning(`只能选择 ${this.limit} 个文件进行上传!!`)
        },
        // 文件状态改变
        handleChange(file, fileList) {
            this.form.name = file.name.split(".")[0]
            if (file) {
                this.fileList = fileList
            }
        },
        // 文件上传失败
        handleError(err, file, fileList) {
            this.$message.error('文件上传失败')
        },
        beforeUpload(file) {
            let extension = file.name.substring(file.name.lastIndexOf('.') + 1)
            let size = file.size / 1024 / 1024;
            if (extension !== 'apk') {

                this.$message.warning('只能上传后缀是.apk的文件')
                return false
            }
            if (size > 100) {
                this.$message.warning('文件大小不得超过100M')
                return false
            }
            return true
        },
        onSubmit() {
            let _this = this
            let formData = new FormData();
            this.fileList.forEach(item => {
                formData.append("files", item.raw);
            });
            if (this.fileList.length === 0) {
                this.$message.warning(`请选择上传文件!!`)
                return
            }
            postMethodCommon("/Files/Upload", formData).then(res => {
                if (res.code === 200) {
                    _this.form.filepath = res.result[0].filePath

                    let param = {
                        "file_path": res.result[0].filePath,
                        "token": ""
                    }
                    postMethodCommon('/FireCodeApp/BatchExportRanger',param).then(res2 => {

                        _this.$emit("importSuccess")
                      
                        _this.fileList = []

                    })
                } else {
                    _this.$message.warning(`接口错误!!`)
                }
            })
        },
   },
   created() {
   
   },
   mounted() {
   
   },
   destroyed() {
   
   },
}
</script>

<style scoped>

</style>