<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" :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 {
    deptIdentifyList,
    insertTemplate
} from "@/api/officialSeal/officialSeal.js";
export default {
    props: ["uploadVisable", "uploadData", "queryParams"],
    dicts: ["template_type"],
    data() {
        return {
            fileList: [],
            fileListA: [],
            fileListB: [],
            fileListC: [],
            data: [],
            tempList: [],
            isImg: false,
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            filedata: {
                annexType: 1
            },
            filedataA: {
                annexType: 2
            },
            filedataB: {
                annexType: 3
            },
            filedataC: {
                annexType: 4
            },
            flagBtn: false,
            ruleForm: {},
            rules: {
                temName: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ],
                // temType: [
                //     { required: true, message: '请输入模板名称', trigger: 'blur' },
                // ],
                identifyId: [
                    { required: true, message: '请输入模板名称', trigger: 'blur' },
                ]
            },
            submitForm: {
                annexIds: []
            }
        };
    },
    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/upload";
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handleSuccess(res) {
            this.$message.success('上传成功');
            console.log(res, "OOOOOOOOOOOOOOOOO");
            this.submitForm.annexIds.push(res.annexId)
        },
        handleChangeA(file, fileList) {
            this.isImg = file.type === '.doc' || '.docx'
        },
        // 自然人
        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'
        },
        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)
        },

        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)
        },
        submitUpload() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    if(this.submitForm.annexIds.length < 4){
                        this.$message.error('四种类型模板必须上传');
                        return
                    }
                    this.submitForm.identifyId = this.ruleForm.identifyId;
                    this.submitForm.temName = this.ruleForm.temName;
                    insertTemplate(this.submitForm).then(res => {
                        this.$message.success('新增成功');
                        this.$emit("cancelUpload");
                        this.$emit('getList', this.queryParams);
                    })
                }
            });
        },
    },
};
</script>
  
<style lang="scss" scoped>
.avatar-uploader {
    margin-bottom: 20px;
}
</style>