<template>
    <div class="notification">
        <div class="notificationBtn">
            <div class="collectSeach">
                <div class="exceed">
                    <span class="exceedSpan">通告标题：</span>
                    <el-input v-model="noticeTitle" placeholder="请输入通告标题"></el-input>
                </div>
                <el-button type="primary" style="margin-left: 10px;" @click="getSeachData">搜索</el-button>
                <el-button style="margin-left: 10px;" @click="resetSeach">重置</el-button>
            </div>
            <div class="PeriodBtn">
                <btns
                :showImport="false"
                :showExport="false"
                :showSave="true"
                :showAdd="true"
                :showDelete="true"
                :disSave="changeDateSelect.length === 0 || changeDateSelect.length>1"
                :disDelete="changeDateSelect.length === 0"
                :disImport="false"
                :disExport="false"
                @handleAdd="handleAdd"
                @handleEdit="handleEdit"
                @handleDelete="handleDelete"
                ></btns>
            </div>
            <div class="notificationTableData">
                <el-table :data="notificationData" style="width: 100%" @select="rowClick" @select-all="rowClick">
                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                    <el-table-column label="序号" prop="serialNumber" align="center" width="80"></el-table-column>
                    <el-table-column label="通告标题" width="300">
                        <template #default="scope">
                            <el-tooltip
                                class="box-item"
                                effect="customized"
                                :content="scope.row.noticeTitle"
                                placement="right"
                            >
                                <span class="tooltipCC">{{scope.row.noticeTitle}}</span>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="通告内容" width="400">
                        <template #default="scope">
                            <el-tooltip
                                class="box-item"
                                effect="customized"
                                :content="scope.row.noticeContent"
                                placement="right"
                            >
                                <span class="tooltipCC">{{scope.row.noticeContent}}</span>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="通告对象" prop="sendTo" />
                    <el-table-column label="个别部门" prop="deptNames" />
                    <el-table-column label="发布状态" prop="releaseState" />
                </el-table>
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="page.currentPage"
                    :page-size="page.pagesize"
                    layout="total, prev, pager, next, jumper"
                    :total="page.total">
                </el-pagination>
            </div>
            <el-dialog :title="title" custom-class="startToDia" v-model="dialogVisible" width="600px" :close-on-click-modal="false">
                <div class="periodFrom">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" :validate-on-rule-change="false">      
                        <el-form-item label="通告标题" prop="noticeTitle">
                            <el-input v-model="ruleForm.noticeTitle"  placeholder="请输入通告标题" />
                        </el-form-item>
                        <el-form-item label="通告内容" prop="noticeContent">
                            <el-input v-model="ruleForm.noticeContent"
                                type="textarea" :rows="3" placeholder="请输入通告内容"></el-input>
                        </el-form-item>
                        <el-form-item label="通告对象" prop="sendTo">
                            <el-select v-model="ruleForm.sendTo" placeholder="请选择通告对象">
                                <el-option
                                v-for="item in sendToData"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="个别部门" prop="sendToContent" v-if="ruleForm.sendTo === '个别部门'">
                            <el-select v-model="ruleForm.sendToContent" multiple collapse-tags placeholder="请选择部门">
                                <el-option
                                v-for="item in departData"
                                :key="item.id"
                                :label="item.keyName"
                                :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="排序">
                            <el-input-number v-model="ruleForm.serialNumber" :min="1" />
                        </el-form-item>
                        <el-form-item label="上传附件">
                            <div>
                                <el-upload
                                    :file-list="filesList"
                                    multiple
                                    limit="3"
                                    :auto-upload="false"
                                    :show-file-list="false"
                                    >
                                    <el-button type="primary">选择上传文件</el-button>
                                    <template #tip>
                                        <div class="el-upload__tip">
                                            最多上传3个文件
                                        </div>
                                    </template>
                                </el-upload>
                                <div v-if="!isSave">
                                    <div v-for="it in filesList" :key="it.uid">
                                        <div>
                                            <span>{{it.name}}</span>
                                            <el-icon :size="20" color="#3B7AD1"
                                            @click="removeFile(it)"
                                             style="position: relative;top: 5px;left: 10px;cursor:pointer"><CircleClose /></el-icon>
                                        </div>
                                    </div>
                                </div>
                                <div v-else>
                                    <div v-for="it in filesList" :key="it.uid">
                                        <div>
                                            <span>{{it.name}}</span>
                                            <el-icon :size="20" color="#3B7AD1"
                                            @click="removeFile(it)"
                                            style="position: relative;top: 5px;left: 10px;cursor:pointer"><CircleClose /></el-icon>
                                        </div>
                                    </div>
                                    <div v-for="it in filesListEdit" :key="it">
                                        <div>
                                            <span>{{it}}</span>
                                            <el-icon :size="20" color="#3B7AD1"
                                            @click="removeFileFn(it)"
                                            style="position: relative;top: 5px;left: 10px;cursor:pointer"><CircleClose /></el-icon>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </el-form-item>
                    </el-form>
                </div>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button type="warning" @click="saveevaluaStart('ruleForm', '草稿')">草 稿</el-button>
                        <el-button type="primary" @click="saveevaluaStart('ruleForm', '发布')">发 布</el-button>
                        <el-button @click="saveevaluaStart('ruleForm', '取消')">取 消</el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import btns from '../elbuttonS.vue'
import {apiGetevaluationdeptnoticeList, apiGetevaluationdeptnoticeSave,apievaluationdeptnoticeDelete,
apiGetdatadictionaryList, apiPostevaluationdeptnoticeDelete} from '../../api/api'
export default {
    components: {
        btns
    },
    data() {
        return {
            dialogVisible: false,
            isSave: false,
            title: '',
            evalradio: {},
            changeDateSelect: [],
            notificationData:[],
            departData: [],
            sendToData: [],
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            },
            ruleForm: {
                serialNumber: 1,
                noticeTitle: '',
                noticeContent: '',
                sendTo: '',
                sendToContent: [],
                releaseDept: '',
                releasePeople: '',
            },
            filesList: [],
            filesListEdit: [],
            rules: {
                noticeTitle: [
                    { required: true, message: '请输入通告标题', trigger: 'blur' }
                ],
                noticeContent: [
                    { required: true, message: '请输入通告内容', trigger: 'blur' }
                ],
                sendTo: [
                    { required: true, message: '请选择通告对象', trigger: 'change' }
                ],
                sendToContent: [
                    { required: true, message: '请选择个别部门', trigger: 'change' }
                ]
            },
            noticeTitle: '',
            headers: { "Content-Type": "multipart/form-data" },
            fileType: [ "pdf", "doc", "docx", "xls", "xlsx","txt","png","jpg", "bmp", "jpeg"]
        }
    },
    created() {
        this.sendToData = [
            {
                label: '所有单位',
                value: '所有单位'
            },
            {
                label: '本部部门',
                value: '本部部门'
            },
            {
                label: '个别部门',
                value: '个别部门'
            }
        ]
        this.getevalunotificationList()
        this.getDepartmentFromTreeData()
    },
    methods:{
        // 查询列表页面
        getevalunotificationList() {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize,
                noticeTitle: that.noticeTitle
            }
            apiGetevaluationdeptnoticeList(params).then(datas =>{
                if (datas && datas.data) {
                    that.notificationData = datas.data.records
                    that.page.total = datas.data.total
                }
            })
        },
        // 查询部门
        getDepartmentFromTreeData() {
            let that = this
            let params = {
                superKey: 'BM0001'
            }
            apiGetdatadictionaryList(params).then(datas =>{
                if (datas && datas.data) {
                    that.departData = datas.data
                }
            })
        },
        notificationFn() {
            if (this.notificationData.length>0) {
                notificationData.slice((page.currentPage-1)*page.pagesize, page.currentPage*page.pagesize)
            }
        },
        saveevaluaStart(formName, type) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditnotificationData(type)
                }
            });
        },
        //新增/修改考评启动数据
        saveAndEditnotificationData(type) {
            let that = this
            // let userMes = JSON.parse(window.sessionStorage.getItem('user'))
            let params = {
                serialNumber: that.ruleForm.serialNumber,
                noticeTitle: that.ruleForm.noticeTitle,
                noticeContent: that.ruleForm.noticeContent,
                sendTo: that.ruleForm.sendTo,
                sendToContent: that.ruleForm.sendToContent.join(','),
                releaseState: type === '草稿'? '草稿': type === '发布'?'已发布':'未发布',
                // releaseDept: userMes.deptId,
                // releasePeople: userMes.id
            }
            debugger
            const formData = new FormData()
            if (that.filesList.length>0) {
                that.filesList.forEach(item =>{
                    formData.append('files', item.raw)
                })
            }
            if (that.isSave) {
                params.id = that.evalradio.id
                formData.append('deptNames', 'deptNames')
            }
            apiGetevaluationdeptnoticeSave(params, formData).then(datas =>{
                if (!datas.success) {
                    that.$message({
                        message: datas.message,
                        type: 'error'
                    });
                } else {
                    if (!that.isSave) {
                        that.$message({
                            message: '通告新增成功',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            message: '通告修改成功',
                            type: 'success'
                        });
                    }
                    that.dialogVisible = false
                    that.changeDateSelect = []
                    that.getevalunotificationList()
                }
            })
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getevalunotificationList()
        },
        resetSeach() {
            this.page.currentPage = 1
            this.noticeTitle = ''
            this.getevalunotificationList()
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getevalunotificationList()
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getevalunotificationList()
        },
        rowClick(selection, row) {
            this.changeDateSelect = selection
        },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新建通告'
            this.$nextTick(() =>{
                this.$refs['ruleForm'].resetFields()
                this.filesList = []
                this.ruleForm = {
                    serialNumber: 1,
                    noticeTitle: '',
                    noticeContent: '',
                    sendTo: '',
                    sendToContent: [],
                    releaseDept: '',
                    releasePeople: ''
                }
            })
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改通告'
            this.filesList = []
            this.filesListEdit = []
            this.evalradio = this.changeDateSelect[0]
            this.ruleForm = {
                serialNumber: this.evalradio.serialNumber,
                noticeTitle: this.evalradio.noticeTitle,
                noticeContent: this.evalradio.noticeContent,
                sendTo: this.evalradio.sendTo,
                sendToContent: this.evalradio.sendToContent.split(','),
                releaseDept: this.evalradio.releaseDept,
                releasePeople: this.evalradio.releasePeople
            }
            let filesArr = this.evalradio.noticeAnnex.split(',')
            filesArr.forEach(item =>{
                let str = item.substring(item.lastIndexOf('/')+1, item.length)
                this.filesListEdit.push(str)
            })
        },
        //删除
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                apiPostevaluationdeptnoticeDelete(paramsArr.join(',')).then(datas =>{
                    that.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    that.getevalunotificationList()
                    that.changeDateSelect = []
                })
            })
        },
        //上传文件之前验证文件类型和大小
        beforeUpload(file) {
            // this.ruleForm.files = []
            if (file.type != "" || file.type != null || file.type != undefined){
                //截取文件的后缀，判断文件类型
                const FileExt = file.name.replace(/.+\./, "").toLowerCase();
                //计算文件的大小
                const isLt5M = file.size / 1024 / 1024 < 5; //这里做文件大小限制
                //如果大于5M
                if (!isLt5M) {
                    this.$message({
                    message: '上传文件大小不能超过 5MB!',
                    type: 'info'
                    });
                    return false;
                }
                //如果文件类型不在允许上传的范围内
                if(this.fileType.includes(FileExt)){
                    return true;
                }
                else {
                    this.$message.error("上传文件格式不正确!");
                    return false;
                }
            }
        },
        //移除文件
        removeFile(item) {
            for (var i in this.filesList) {
                if (item.name == this.filesList[i].name) {
                    this.filesList.splice(i, 1)
                }
            }
        },
        removeFileFn(item) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let params = {
                    id: that.evalradio.id,
                    noticeAnnex: item
                }
                apievaluationdeptnoticeDelete(params).then(datas =>{
                    if (datas.success) {
                        that.filesListEdit.forEach((it, index) =>{
                            if (it === item) {
                                that.filesListEdit.splice(index, 1)
                            }
                        })
                    } else {
                        this.$message({
                            message: datas.message,
                            type: 'error'
                        });
                    }
                })
            })
        }
    }
}
</script>

<style lang="less">
.notification{
    .notificationBtn{
        .collectSeach{
            display: flex;
            padding: 24px 20px;
            border-bottom: 1px solid#D6DBEA;
            .exceed{
                display: flex;
                .exceedSpan{
                    width: 100px;
                    height: 12px;
                    font-size: 14px;
                    font-family: Microsoft YaHei;
                    font-weight: 400;
                    color: #8991B0;
                    line-height: 12px;
                    margin-top: 14px;
                }
                .el-input{
                    margin-right:10px;
                    height: 40px;
                    .el-input__inner{
                        height:40px;
                    }
                    .el-input__suffix{
                        .el-select__caret{
                            line-height:30px;
                        }
                    }
                }
            }
            .el-select{
                margin-right:10px;
                .el-input__inner{
                    height:30px;
                }
                .el-input__suffix{
                    .el-select__caret{
                        line-height:30px;
                    }
                }
            }
        }
        span{
            font-size:14px;
        }
        .PeriodBtn{
            display: flex;
            justify-content: end;
            padding: 20px 0;
        }
        .el-button{
            height: 30px;
            // width:100px;
            padding: 0 20px ;
            // padding-top: 8px;
            span{
                margin:0;
            }
        }
        .notificationTableData{
            .el-table{
                .el-table__body-wrapper{
                    height: 60vh !important;
                }
                .el-input__inner{
                    height: 30px !important;
                }
                .el-radio__label{
                    display: none;
                }
                .tooltipCC{
                    width: 300px !important;
                    display: inline-block;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }
                .tooltipCCDes{
                    width: 50px;
                    display: inline-block;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }
                .indicitem{
                    color: #409EFF;
                    font-size: 12px;
                    margin-right: 20px;
                    cursor:pointer;
                    &:hover{
                        text-decoration: underline;
                    }
                }
            }
            .el-pagination{
                margin-top: 20px;
                text-align: end;
                position: relative;
            }
        }
        .el-overlay{
            .startToDia{
                .el-dialog__body{
                    padding: 30px 60px 30px 20px !important;
                    .periodFrom{
                        .el-select, .el-input{
                            width: 100%;
                        }
                        .el-input{
                            height: 30px;
                        }
                    }
                    .flowSty{
                        width: 100%;
                        height: 70vh;
                    }
                }
            }
        }
    }
    
}
</style>