<template>
    <div>
        <el-dialog title="修改模板" :visible="editVisable" v-if="editVisable" @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="temName">
                    <el-input v-model="ruleForm.temName"></el-input>
                </el-form-item>
                <el-form-item label="当前模板">
                    <span style="color: #0e5ce3;cursor:pointer" @click="openUrl(editData.temOrigPath)">{{ editData.fileName
                    }}</span>
                </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" accept=".doc,.docx" :file-list="fileList" :auto-upload="false">
                <el-button size="small" type="primary">选择模板文件</el-button>
            </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: ["editVisable", "editData", "queryParams"],
    data() {
        return {
            fileList: [],
            data: [],
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            isImg: false,
            filedata: {},
            flagBtn: false,
            ruleForm: {},
            rules: {
                temName: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ],
            }
        };
    },
    watch: {
        editData(val) {
            if (val) {
                this.ruleForm = val;
            }
        }
    },
    created() {

    },
    methods: {
        // 打开链接
        openUrl(urlTemp) {
            let headPath = window.location.origin + "/API/";
            window.open(headPath + urlTemp);
        },
        cancel() {
            this.$emit("cancelEdit");
        },
        handleChange(file, fileList) {
            debugger
            this.isImg = (file.type === '.doc' || file.type === '.docx') ? true:false;
        },
        UploadUrl() {
            return window.location.origin + "/API/deptIdentify/updateTemplate";
        },
        submitUpload() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.filedata.id = this.editData.id;
                    this.filedata.temName = this.ruleForm.temName;
                    if (this.isImg) {
                        this.$refs.upload.submit();
                    }else{
                        this.$message.error('只能上传doc,docx格式的文件')
                    }
                }
            });
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        beforeUpload(file) {
            this.isImg = file.type === '.doc' || '.docx'
        },
        handleSuccess() {
            this.$message.success('上传成功');
            this.$emit("cancelEdit");
            this.$emit('getList', this.queryParams);
        }
    },
};
</script>
  
<style lang="scss" scoped></style>