<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="170px"
                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="自然人调解申请书模板" v-if="ruleForm.personApplicationFile && ruleForm.personApplicationFile.fileName">
                    <span style="color: #0e5ce3;cursor:pointer" @click="openUrl(ruleForm.personApplicationFile.temOrigPath)">{{
                        ruleForm.personApplicationFile.fileName ? ruleForm.personApplicationFile.fileName : ""
                    }}</span>
                </el-form-item>
                <el-form-item label="机构调解申请书模板" v-if="ruleForm.applicationFile.fileName">
                    <span style="color: #0e5ce3;cursor:pointer" @click="openUrl(ruleForm.applicationFile.temOrigPath)">{{
                        ruleForm.applicationFile.fileName ? ruleForm.applicationFile.fileName : ""
                    }}</span>
                </el-form-item>
                <el-form-item label="和解协议模板" v-if="ruleForm.agreementFile.fileName">
                    <!-- <span style="color: #0e5ce3;cursor:pointer" @click="openUrl(editData.temOrigPath)">{{ editData.fileName
                    }}</span> -->
                    <span style="color: #0e5ce3;cursor:pointer" @click="openUrl(ruleForm.agreementFile.temOrigPath)">{{
                        ruleForm.agreementFile.fileName ? ruleForm.agreementFile.fileName : ""
                    }}</span>
                </el-form-item>
                <el-form-item label="调解书模板" v-if="ruleForm.bookFile.fileName">
                    <span style="color: #0e5ce3;cursor:pointer" @click="openUrl(ruleForm.bookFile.temOrigPath)">{{
                        ruleForm.bookFile.fileName ? ruleForm.bookFile.fileName : ""
                    }}</span>
                </el-form-item>
            </el-form>
            <el-upload class="avatar-uploader" :on-success="handleSuccessC" ref="upload" :action="UploadUrl()"
                :headers="headers" :data="filedataC" :on-remove="handleRemoveC" :on-change="handleChangeC" accept=".doc,.docx"
                :file-list="fileListC" :limit="1">
                <el-button size="small" type="primary">选择自然人调解申请书模板</el-button>
            </el-upload>
            <el-upload class="avatar-uploader" :on-success="handleSuccess" ref="upload" :action="UploadUrl()"
                :headers="headers" :data="filedata" :on-remove="handleRemove" :on-change="handleChange" accept=".doc,.docx"
                :file-list="fileList" :limit="1">
                <el-button size="small" type="primary">选择机构调解申请书模板</el-button>
            </el-upload>
            <el-upload class="avatar-uploader" :on-success="handleSuccessA" ref="uploadA" :action="UploadUrl()"
                :headers="headers" :data="filedataA" :on-remove="handleRemoveA" :on-change="handleChangeA"
                accept=".doc,.docx" :file-list="fileListA" :limit="1">
                <el-button size="small" type="primary">选择和解协议模板</el-button>
            </el-upload>
            <el-upload class="avatar-uploader" :on-success="handleSuccessB" ref="upload" :action="UploadUrl()"
                :headers="headers" :data="filedataB" :on-remove="handleRemoveB" :on-change="handleChangeB"
                accept=".doc,.docx" :file-list="fileListB" :limit="1">
                <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, getTemplateDetail } from "@/api/officialSeal/officialSeal"
export default {
    props: ["editVisable", "editData", "queryParams"],
    dicts: ["template_type"],
    data() {
        return {
            fileList: [],
            fileListA: [],
            fileListB: [],
            fileListC: [],
            data: [],
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            isImg: true,
            filedata: {
                annexType: 1
            },
            filedataA: {
                annexType: 2
            },
            filedataB: {
                annexType: 3
            },
            filedataC: {
                annexType: 4
            },
            flagBtn: false,
            ruleForm: {
                agreementFile:{},
                applicationFile:{},
                bookFile:{}
            },
            rules: {
                temName: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ],
            },
            submitForm: {
                annexIds: []
            }
        };
    },
    watch: {
        editData(val) {
            if (val) {
                this.fileList = [];
            }
        },
        editVisable(val){
            if(val){
                this.getTemplateDetailFn({ id: this.editData.id });
                this.submitForm.annexIds = []
            }
        }
    },
    created() {

    },
    methods: {
        // 打开链接
        openUrl(urlTemp) {
            let headPath = window.location.origin + "/API/";
            window.open(headPath + urlTemp);
        },
        cancel() {
            this.$emit("cancelEdit");
        },
        UploadUrl() {
            return window.location.origin + "/API/deptIdentify/updateTemplate";
        },
        /**根据id查看详情*/
        getTemplateDetailFn(data) {
            getTemplateDetail(data).then(res => {
                this.ruleForm = res;
                // this.submitForm.annexIds = [this.ruleForm.agreementFile.fileId,this.ruleForm.applicationFile.fileId,this.ruleForm.bookFile.fileId]
            })
        },
        submitUpload() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.$refs['ruleForm'].validate((valid) => {
                        if (valid) {
                            this.submitForm.identifyId = this.ruleForm.identifyId;
                            this.submitForm.temName = this.ruleForm.temName;
                            this.submitForm.id = this.ruleForm.id;
                            console.log(this.submitForm,'ooooooooooooooooooooooooooooo');
                            updateTemplate(this.submitForm).then(res => {
                                this.$message.success('修改成功');
                                this.$emit("cancelEdit");
                                this.$emit('getList', this.queryParams);
                            })
                        }
                    });
                }
            });
        },
        handleChange(file, fileList) {
            this.isImg = file.type === '.doc' || '.docx'
        },
        UploadUrl() {
            return window.location.origin + "/API/deptIdentify/upload";
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        // 机构
        handleSuccess(res) {
        console.log(res,'机构上传成功');
            this.$message.success('上传成功');
            // this.submitForm.annexIds.push(res.annexId);
            this.submitForm.annexIds.push(res.annexId);
        },
        // 自然人
        handleSuccessC(res) {
        console.log(res,'自然人上传成功');
            this.$message.success('上传成功');
            this.submitForm.annexIds.push(res.annexId);
        },
        handleRemoveC(file, fileList) {
            console.log(file, fileList);
        },
        handleChangeC(file, fileList) {
            this.isImg = file.type === '.doc' || '.docx'
        },
        handleChangeA(file, fileList) {
            this.isImg = file.type === '.doc' || '.docx'
        },
        UploadUrl() {
            return window.location.origin + "/API/deptIdentify/upload";
        },
        handleRemoveA(file, fileList) {
            console.log(file, fileList);
        },
        // 和解协议
        handleSuccessA(res) {
            this.$message.success('上传成功');
            // this.submitForm.annexIds.push(res.annexId);
            this.submitForm.annexIds.push(res.annexId);
        },

        handleChangeB(file, fileList) {
            this.isImg = file.type === '.doc' || '.docx'
        },
        UploadUrl() {
            return window.location.origin + "/API/deptIdentify/upload";
        },
        handleRemoveB(file, fileList) {
            console.log(file, fileList);
        },
        // 调解书
        handleSuccessB(res) {
            this.$message.success('上传成功');
            // this.submitForm.annexIds.push(res.annexId);
            this.submitForm.annexIds.push(res.annexId);
        },
        // handleSuccess() {
        //     this.$message.success('修改成功');
        //     this.$emit("cancelEdit");
        //     this.$emit('getList', this.queryParams);
        // }
    },
};
</script>
  
<style lang="scss" scoped>
.avatar-uploader {
    margin-bottom: 20px;
}</style>