<template>
    <div>
        <el-dialog title="上传公章" :visible="uploadVisable" v-if="uploadVisable" @close="cancel" width="600px" center>
            <el-form :model="ruleForm" label-position="left" :rules="rules" ref="ruleForm" label-width="90px"
                class="demo-ruleForm">
                <el-form-item label="印章名称" prop="sealName">
                    <el-input v-model="ruleForm.sealName"></el-input>
                </el-form-item>
            </el-form>
            <el-upload class="avatar-uploader" :before-upload="beforeUpload" :on-success="handleSuccess" ref="upload"
                :action="UploadUrl()" :headers="headers" :data="filedata" :on-remove="handleRemove"
                :on-change="handleChange" :show-file-list="false" :file-list="fileList" :auto-upload="false">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel" class="endbutton"><span>取 消</span></el-button>
                <el-button type="primary" @click="submitUpload" class="endbutton"><span>确认</span></el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
    props: ["uploadVisable", "uploadData"],
    data() {
        return {
            fileList: [],
            data: [],
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            filedata: {
                id: this.uploadData.id
            },
            flagBtn: false,
            imageUrl: "",
            ruleForm: {},
            rules: {
                sealName: [
                    { required: true, message: '请输入印章名称', trigger: 'blur' },
                ],
            }
        };
    },
    watch: {
        uploadVisable(val) {
            if (val) {
                this.imageUrl = ""
            }
        }
    },
    created() {

    },
    methods: {
        cancel() {
            this.$emit("cancelUpload");
        },
        handleChange(file, fileList) {
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        UploadUrl() {
            return window.location.origin + "/API/deptIdentify/sealUpload";
        },
        submitUpload() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.filedata.id = this.uploadData.id;
                    this.filedata.sealName = this.ruleForm.sealName;
                    this.$refs.upload.submit();
                }
            });
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        beforeUpload(file) {
            const isImg = file.type === 'image/png'
            if (!isImg) {
                this.$message.error('只能上传图片格式的文件')
            }
            return isImg
        },
        handleSuccess(response) {
            if (response.code == 200) {
                this.$message.success('上传成功')
            } else {
                this.$message.error(response.msg);
            }
        }
    },
};
</script>
  
<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
    width: 178px;
    height: 178px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    margin-left: 90px;
}

::v-deep .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

::v-deep .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

::v-deep .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>