<template>
    <div
        element-loading-text="文件上传中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255,255,255, 0.5)"
        v-loading.fullscreen.lock="loading"
    >
        <el-dialog
            title="文件上传提示"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose"
            top="25vh"
            modal
        >
            <div class="el-message-box__container">
                <div class="el-message-box__status el-icon-warning"></div>
                <div class="el-message-box__message">
                    <p>文件是否涉密？若文件涉密请通过内网邮件上传。</p>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <span class="upload-btn">
                    <el-button @click="setAipher" size="mini" v-if="isClassified"
                        >文件涉密，内网邮件上传</el-button
                    >
                    <el-upload
                        name="files"
                        multiple
                        :action="url"
                        :data="uploadData"
                        :headers="header"
                        :accept="accept"
                        :on-success="beforeUpload"
                        :on-error="uploadError"
                        :show-file-list="false"
                        :before-upload="openLoading"
                    >
                        <el-button
                            class="updload_btn"
                            size="mini"
                            type="primary"
                        >
                            立即上传</el-button
                        >
                    </el-upload>
                </span>
            </span>
        </el-dialog>
    </div>
</template>

<script>
const portal = window.context.portal
export default {
    props: {
        isClassified: {
            type:Boolean,
            default: true
        }
    },
    data() {
        return {
            accept: '.bmp,.jpg,.png,.tif,.gif,.pcx,.tga,.exif,.fpx,.svg,.psd,.cdr,.pcd,.dxf,.ufo,.eps,.ai,.raw,.WMF,.webp,.avif,.apng,.xls,.docx,.docm,.doc,.dotx,.pptx,.pptm,.ppt,.pdf,.xps,.potx,.txt,.xls,.xlsx,.xps,.zip,.rar,.7z',
            url: `${portal}/file/v1/uploadFileForConfig`,
            previewUrl: `${portal}/file/onlinePreviewController/v1/getFileById_`,
            header: {
                Authorization: `Bearer ${this.$store.state.login.currentUser.token}`,
            },
            uploadData: {
                bizCode: '',
                bizId: '',
                bizType: '',
            },
            dialogVisible: false,
            loading: false,
        }
    },
    methods: {
        handleClose() {
            this.dialogVisible = false
        },
        open(bizCode) {
            if (!bizCode) {
                this.$message.warning('bizCode必传，不能为空！')
                return
            }
            this.uploadData.bizCode = bizCode
            this.dialogVisible = true
        },
        openLoading() {
            this.dialogVisible = false
            this.loading = true
        },
        beforeUpload(res, file, fileList) {
            if (res.success) {
                this.$emit('setFileList', file)
                this.$message.success('附件上传成功')
                this.loading = false
            } else {
                this.$message.error('附件上传失败')
                this.loading = false
            }
        },
        uploadError(err) {
            this.$message.error('附件上传失败')
            this.loading = false
        },
        setAipher() {
            this.dialogVisible = false
            this.$emit('setAipher', true)
        },
    },
}
</script>

<style lang="scss" scoped>
/deep/ .el-message-box__message {
  user-select: none;
}
/deep/ .el-dialog__header {
  background: #fff;
  border-bottom: 1px solid #f5f5f5;
}
.upload-btn {
  display: flex;
  justify-content: flex-end;
  > button {
    margin-right: 10px;
    background-color: #f5f5f5;
    color: #999;
  }
  button {
    border: none;
  }
}
</style>
