<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="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="机构名称" prop="identifyId">
                    <el-select v-model="ruleForm.identifyId" placeholder="请选择">
                        <el-option v-for="item in tempList" :key="item.id" :label="item.identifyName" :value="item.id">
                        </el-option>
                    </el-select>
                </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 {
    deptIdentifyList,
} from "@/api/officialSeal/officialSeal.js";
export default {
    props: ["uploadVisable", "uploadData", "queryParams"],
    dicts: ["template_type"],
    data() {
        return {
            fileList: [],
            data: [],
            tempList: [],
            isImg:false,
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            filedata: {},
            flagBtn: false,
            ruleForm: {},
            rules: {
                temName: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ],
                temType: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ],
                identifyId: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ]
            }
        };
    },
    watch: {
        uploadVisable(val) {
            if (val) {
                let queryParams = {
                    pageNum: 1,
                    pageSize: 10000000000000000000000,
                };
                deptIdentifyList(queryParams).then(res => {
                    this.tempList = res.rows;
                })
                this.ruleForm = {}
            }
        }
    },
    created() {

    },
    methods: {
        cancel() {
            this.$emit("cancelUpload");
        },
        handleChange(file, fileList) {
            this.isImg = file.type === '.doc' || '.docx'
        },
        UploadUrl() {
            return window.location.origin + "/API/deptIdentify/insertTemplate";
        },
        submitUpload() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.filedata.identifyId = this.ruleForm.identifyId;
                    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) {
            // debugger
            // this.isImg = file.type === '.doc' || '.docx'
        },
        handleSuccess() {
            this.$message.success('上传成功');
            this.$emit("cancelUpload");
            this.$emit('getList', this.queryParams);
        }
    },
};
</script>
  
<style lang="scss" scoped></style>