<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="模板类型" prop="temType">
                    <el-select v-model="ruleForm.temType" placeholder="请选择">
                        <el-option v-for="dict in dict.type.template_type" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </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";
import { updateTemplate } from "@/api/officialSeal/officialSeal"
export default {
    props: ["editVisable", "editData", "queryParams"],
    dicts: ["template_type"],
    data() {
        return {
            fileList: [],
            data: [],
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            isImg: true,
            filedata: {},
            flagBtn: false,
            ruleForm: {},
            rules: {
                temName: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ],
                temType: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ],
            }
        };
    },
    watch: {
        editData(val) {
            if (val) {
                this.ruleForm = val;
                this.fileList = [];
                this.ruleForm.temType = this.ruleForm.temType + '';
            }
        }
    },
    created() {

    },
    methods: {
        // 打开链接
        openUrl(urlTemp) {
            let headPath = window.location.origin + "/API/";
            window.open(headPath + urlTemp);
        },
        cancel() {
            this.$emit("cancelEdit");
        },
        handleChange(file, fileList) {
            this.isImg = file.type === '.doc' || '.docx';
            this.fileList = fileList;
        },
        UploadUrl() {
            return window.location.origin + "/API/deptIdentify/updateTemplate";
        },
        submitUpload() {
            let that = this;
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    if (that.fileList.length > 0) {
                        that.filedata.id = that.editData.id;
                        that.filedata.temName = that.ruleForm.temName;
                        if (that.isImg) {
                            that.$refs.upload.submit();
                        } else {
                            that.$message.error('只能上传doc,docx格式的文件')
                        }
                    } else {
                        updateTemplate({
                            id: that.editData.id,
                            temName: that.ruleForm.temName
                        }).then(res => {
                            that.$message.success('修改成功');
                            that.$emit("cancelEdit");
                            that.$emit('getList', that.queryParams);
                        })
                    }
                }
            });
        },
        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>