<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container">
        <div class="container-box">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div class="search">
                <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch>
                <el-button class="btn-check" size="medium" type="primary" @click="dialogCheck(1)">添加
                </el-button>
            </div>
            <div class="tabbox">
                <el-table height="calc(100% - 40px)" class="com-table" ref="multipleTable" :data="tableData"
                    tooltip-effect="dark" size="small" border style="width: 100%" v-loading="loading">
                    <el-table-column prop="noticeTitle" label="公告标题" width="120">
                    </el-table-column>
                    <el-table-column label="公告内容">
                        <template slot-scope="scope">
                            <el-tooltip class="item" effect="light" :content="scope.row.noticeContent" placement="bottom">
                                <span class="tab-long">{{scope.row.noticeContent}}</span>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column prop="opName" label="发布姓名" width="100">
                        <template slot-scope="scope">
                            <span>{{$desensitization(scope.row.opName,1)}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="deptName" label="发布部门">
                    </el-table-column>
                    <el-table-column prop="opTime" label="发布时间" width="140">
                    </el-table-column>
                    <el-table-column prop="stsDesc" label="状态">
                    </el-table-column>
                    <el-table-column prop="auditNo" label="审核工号">
                    </el-table-column>
                    <el-table-column prop="auditName" label="审核姓名">
                        <template slot-scope="scope">
                            <span>{{$desensitization(scope.row.auditName,1)}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="审核备注">
                        <template slot-scope="scope">
                            <el-tooltip class="item" effect="light" :content="scope.row.auditRemark" placement="bottom">
                                <span class="tab-long">{{scope.row.auditRemark}}</span>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column prop="auditResultDesc" label="审核结果">
                    </el-table-column>
                    <el-table-column prop="auditTime" label="审核时间" width="140">
                    </el-table-column>

                    <el-table-column label="操作" width="160px" align="center" fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(2,scope.row)">查看</el-button>
                            <el-button size="mini" v-if="scope.row.sts != 2" type="primary" @click="dialogCheck(3,scope.row)">修改</el-button>
                            <el-button size="mini" v-if="scope.row.sts != 2" type="danger" @click="delLine(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination class="pageBox" @current-change="currchange" layout="prev, pager, next" background
                    :total="total">
                </el-pagination>
            </div>
            <el-dialog :title="titname + '公告'" :visible.sync="dialogStatus" width="50%" :close-on-press-escape="false"
                :show-close="true" :before-close="closedia" :destroy-on-close="true" :modal-append-to-body="false"
                :close-on-click-modal="false">
                <div v-loading="loading">
                    <el-form :model="infolist" ref="infolist" :rules="rules" style="height: 50vh;overflow-y: scroll;">
                        <div class="info-line">
                            <el-form-item prop="noticeTitle">
                                <span>公告标题</span>
                                <el-input v-model="infolist.noticeTitle" placeholder="标题" :disabled="disableStatus"></el-input>
                            </el-form-item>
                            <el-form-item prop="opName">
                                <span>发布姓名</span>
                                <el-input v-model="infolist.opName" placeholder="姓名" disabled="disabled"></el-input>
                            </el-form-item>
                        </div>
                        <div class="info-line" v-if="titname == '查看'">
                            <el-form-item>
                                <span>电话</span>
                                <el-input v-model="infolist.phoneNo" placeholder="电话" disabled></el-input>
                            </el-form-item>
                            <el-form-item>
                                <span>发布部门</span>
                                <el-input v-model="infolist.deptName" placeholder="发布部门" disabled></el-input>
                            </el-form-item>
                        </div>
                        <el-form-item prop="visiblec" class="info-line online">
                            <span>可见部门</span>
                            <deptTree class="tree" @treeCheck="treeCheck" :defaultList="defaultList" :type="depttype"></deptTree>
                        </el-form-item>
                        <el-form-item class="info-line online">
                            <span>发布内容</span>
                            <el-input :disabled="disableStatus" v-model="infolist.noticeContent" placeholder="发布内容" type="textarea" :rows="3"></el-input>
                        </el-form-item>
                        <div style="padding-left: 80px" v-if="!disableStatus">
                            <myUpload @uploadBack="uploadBack" :fileInfo="fileInfo" :fileList="fileInfo.fileList"></myUpload>
                        </div>
                        <div style="padding:0 20px 0 100px" v-if="disableStatus">
                            <uploadDown :datalist="datalist" :dialogStatus="uploadstatus"></uploadDown>
                        </div>
                    </el-form>
                    <div slot="footer" class="dialog-footer myfooter">
                        <el-button @click="dialogCli(2)" v-if="disableStatus">确 定</el-button>
                        <el-button type="primary" @click="dialogCli(1)" v-if="!disableStatus">保 存</el-button>
                        <el-button type="primary" @click="dialogCli(3)" v-if="!disableStatus">发 布</el-button>
                        <el-button @click="dialogCli(2)">取 消</el-button>
                    </div>
                </div>
            </el-dialog>

            <el-dialog title="公告查看" :visible.sync="dialogInfo" width="70%" :close-on-press-escape="false"
                :show-close="true" :before-close="closedia" :destroy-on-close="true" :modal-append-to-body="false"
                :close-on-click-modal="false" :fullscreen="true">
                <div style="height: calc(100vh - 180px);overflow-y: scroll;">
                    <notiCheck :infolist="infolist" :datalist="datalist" :dialogInfo="dialogInfo"></notiCheck>
                </div>
                <div class="dialog-footer myfooter">
                    <el-button @click="dialogInfo = false">确 定</el-button>
                    <el-button @click="dialogInfo = false">取 消</el-button>
                </div>
            </el-dialog>

        </div>
        <myMessage :messTit='messTit' @closeMessage="closeMessage" :centerDialogVisible="centerDialogVisible" v-if="centerDialogVisible"></myMessage>
    </fullscreen>
</template>
<script>
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown'
    import mySearch from '../../../components/search'
    import toolList from '../../../components/toolList'
    import myMessage from "../../../components/myMessage.vue"
    import deptTree from "../../../components/deptTree.vue"
    import notiCheck from "../../../components/notiCheck.vue"

    export default {
        components: {
            myUpload,
            uploadDown,
            mySearch,
            toolList,
            myMessage,
            deptTree,
            notiCheck
        },
        data() {
            const noticeTitle = (rule, value, callback) => {
                if (!this.infolist.noticeTitle) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const opName = (rule, value, callback) => {
                if (!this.infolist.opName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    noticeTitle: [{
                        required: true,
                        trigger: 'blur',
                        validator: noticeTitle
                    }],
                    opName: [{
                        required: true,
                        trigger: 'blur',
                        validator: opName
                    }],
                },
                tooltit: '公告管理',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                infolist: {},
                typeOptions: [],
                titname: '',
                disableStatus: false,
                fileList: [],
                visiblec: [],
                visibleList: [],
                attList: [],
                fileInfo: {
                    limit: 10,
                    url: '/sysmgr/noticeatt/upload',
                    fileList: []
                },
                datalist: {
                    url: '/sysmgr/noticeatt/downfile',
                    type: 2,
                    attList:[]
                },
                uploadstatus: false,
                userInfo: {},
                searchList: [{
                        type: 'input',
                        tit: '请输入标题',
                        value: '',
                        width: '32%'
                    },
                    {
                        type: 'date',
                        tit: '开始时间',
                        value: '',
                        width: '32%',
                    },
                    {
                        type: 'date',
                        tit: '结束时间',
                        value: '',
                        width: '32%',
                    },
                ],
                params: {},
                centerDialogVisible: false,
                messTit: '',
                delid: '',
                loading:false,
                treeList: [],
                defaultList:[],
				depttype:0,
                dialogInfo:false
            }
        },
        methods: {
            treeCheck(v){
                this.treeList = v;
            },
            closedia() {
                this.infolist = {};
                this.dialogCli(2);
                this.dialogStatus = false;
                this.defaultList = [];
                 this.dialogInfo = false;
				this.depttype = 0;
            },
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.noticeTitle = v[0] : '';
                v[1] ? this.params.opTimeFrom = this.$formatDate(v[1], "YYYY-MM-DD") : '';
                v[2] ? this.params.opTimeTo = this.$formatDate(v[2], "YYYY-MM-DD") : '';
                this.getList(this.params, this.pageSize);
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.tableData = [];
                this.loading = true;
                this.$http({
                    url: "/sysmgr/cnotice/queryListByDept",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                    },
                    data: v,
                }).then((res) => {
                    this.tableData = res.data.data;
                    this.total = res.data.totalRecord;
                    this.loading = false;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //查看 修改 新增
            dialogCheck(v, n) {
                this.visiblec = [];
                this.infolist = {};
                this.defaultList = [];
                if (v === 1) {
                    this.titname = '新建';
                    this.getUser();
                    this.depttype = 1;
                    this.dialogStatus = true;
                    this.disableStatus = false;
                    return
                } else if (v === 2) {
                    this.titname = '查看';
                } else if (v === 3) {
                    this.dialogStatus = true;
                    this.disableStatus = false;
                    this.titname = '修改';
                    this.loading = true;
                }
                this.infolist = Object.assign({}, n); //拷贝
                this.infolist.opName = this.$desensitization(this.infolist.opName,1);
                this.infolist.phoneNo = this.$desensitization(this.infolist.phoneNo,2);
                this.$http({
                    url: "/sysmgr/cnotice/queryInfo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        noticeId: n.noticeId
                    },
                }).then((res) => {
                    this.datalist.attList = res.data.attList;
                    this.uploadstatus = true;
                    this.infolist.attList = res.data.attList;
                    this.fileInfo.fileList = [];
                    this.attList = [];
                    this.treeList = res.data.deptList;
                    if (v === 2) {
                        this.dialogInfo = true;
                    }
                    if (res.data.attList) {
                        res.data.attList.forEach(item => {
                            this.fileInfo.fileList.push({
                                name: item.fileName,
                                url: '',
                                id: item.id,
                                fileName: item.fileName
                            });
                            this.attList.push({
                                name: item.fileName,
                                url: '',
                                id: item.id,
                                fileName: item.fileName
                            });
                        })
                    }
                    if (res.data.deptList) {
                        res.data.deptList.forEach(item => {
                            this.defaultList.push(item.deptCode);
                        })
                    }
					this.depttype = 1;
                    this.loading = false;
                });
            },
            dialogCli(v) {
                this.datalist.attList = [];
                this.uploadstatus = false;
                if (v === 2) {
                    this.fileInfo.fileList = [];
                    this.infolist = {};
                    this.dialogStatus = false;
					this.defaultList = [];
					this.depttype = 0;
                    return
                }
                this.infolist.pubTime = this.$formatDate(new Date(), "YYYY-MM-DD");
                this.infolist.opTime = this.infolist.pubTime;
                if (this.titname === '新建') {
                    this.submitInfo("/sysmgr/cnotice/save", v);
                } else if (this.titname === '修改') {
                    this.submitInfo("/sysmgr/cnotice/update", v);
                }
            },
            submitInfo(u, v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        this.infolist.sts = "0";
                        this.infolist.stsDesc = "草稿";
                        this.infolist.opNo = this.userInfo.loginNo;
                        this.infolist.opName = this.userInfo.loginName;
                        this.infolist.phoneNo = this.userInfo.phoneNo;
                        if (v === 3) {
                            this.infolist.sts = "1";
                            this.infolist.stsDesc = "待审核";
                        }
                        let attList = [];
                        for (let i = 0; i < this.attList.length; i++) {
                            attList.push({
								id: this.attList[i].id,
								fileCode: this.attList[i].fileCode,
								fileName: this.attList[i].fileName,
								opName: this.attList[i].opName,
								opNo: this.attList[i].opNo,
								opTime: this.attList[i].opTime,
                                noticeId: this.infolist.noticeId
                            });
                        }
                        let list = [];
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: {
                                noticeInfo: this.infolist,
                                attList: attList,
                                deptList: this.treeList
                            },
                        }).then((res) => {
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.fileInfo.fileList = [];
                                _this.infolist = {};
                                _this.dialogStatus = false;
                                _this.getList(this.params, this.pageSize);
                                _this.defaultList = [];
								_this.depttype = 0;
                            }

                        });
                    }
                })
            },
            closeMessage(v) {
                this.centerDialogVisible = false;
                let _this = this;
                if (v === 1) {
                    _this.$http({
                        url: "/sysmgr/cnotice/del",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            noticeInfo: {
                                noticeId: this.delid
                            }
                        }
                    }).then((res) => {
                        if (res.data.result === 1) {
                            _this.$message({
                                message: res.data.desc,
                                type: 'error'
                            });
                        } else {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.getList(this.params, this.pageSize);
                        }
                    });
                }
            },
            //删除
            delLine(v) {
                this.centerDialogVisible = true;
                this.messTit = '即将删除此条数据, 是否删除?';
                this.delid = v.noticeId;
            },
            //文件返回值
            uploadBack(v) {
                this.attList = v;
            },
            //功能栏
            iconCli(v) {
                if (v === 1) {
                    this.getList(this.params, this.pageSize);
                }
                if (v === 2) {
                    this.fullscreen = !this.fullscreen
                }
            },
            //获取部门
            getDepot() {
                this.$http({
                    url: "/sysmgr/sysdept/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {},
                }).then((res) => {
                    this.typeOptions = res.data;
                });
            },
            getUser() {
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);
                this.infolist = {};
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.opName = this.userInfo.loginName;
            }
        },
        mounted() {
            this.getList(this.params, this.pageSize);
            this.getDepot();
            this.getUser();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    @import "../../../assets/style";

    .tab-long {
        height: 22px;
        float: left;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        /* 将对象作为弹性伸缩盒子模型显示 */
        -webkit-line-clamp: 1;
        /* 控制最多显示几行 */
        -webkit-box-orient: vertical;
        /* 设置或检索伸缩盒对象的子元素的排列方式 */
    }

    .titbox {
        div {
            float: right;

            i {
                font-size: 22px;
                margin-left: 20px;
                cursor: pointer;
            }
        }
    }

    .tabbox {
        margin-top: 16px;
        font-size: 14px !important;
    }

    .pageBox {
        text-align: right;
        margin-top: 10px;
    }

    .info-line {
        width: 100%;
        display: block;
        padding-left: 20px;

        div {
            width: 50%;
            display: inline-block;
        }

        span {
            width: 80px;
            display: inline-block;
            text-align: left;

            i {
                color: red;
                display: inline-block;
                padding-right: 5px;
            }
        }

        .el-select,
        .el-input {
            width: calc(100% - 100px);
        }
        .tree{
            width: calc(100% - 100px);
        }
    }

    .online {
        width: 100%;

        .el-select {
            width: calc(100% - 100px);
        }

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 100px);
        }
    }
    ::v-deep .el-table--mini, .el-table--small, .el-table__expand-icon{
        font-size: 14px;
    }
</style>
