<template>
    <div class="container">
        <fullscreen :fullscreen.sync="fullscreen" 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%">

                    <el-table-column prop="msgTit" label="字典类型">
                    </el-table-column>
                    <el-table-column prop="msgTypeName" label="字典名称">
                    </el-table-column>
                    <el-table-column prop="msgTypeName" label="字典编码">
                    </el-table-column>

                    <el-table-column label="操作" width="160px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(2,scope.row)">查看</el-button>
                            <el-button size="mini" type="primary" @click="dialogCheck(3,scope.row)">修改</el-button>
                            <el-button size="mini" 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>
        </fullscreen>
        <el-dialog :title="titname + '信息上报'" :visible.sync="dialogStatus" width="50%" :close-on-press-escape="false"
            :show-close="false" :destroy-on-close="true" :modal-append-to-body="false" :close-on-click-modal="false">
            <el-form :model="infolist" ref="infolist" :rules="rules">


                <div class="info-line">
                    <el-form-item prop="msgTit">
                        <span>字典类型</span>
                        <el-input v-model="infolist.msgTit" placeholder="字典类型" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item prop="msgType">
                        <span>字典名称</span>
                       <el-input v-model="infolist.msgTit" placeholder="字典名称" :disabled="disableStatus"></el-input>
                       </el-form-item>
                    </el-form-item>
                </div>
                <div class="info-line">
                    <el-form-item prop="msgTit">
                        <span>字典类型</span>
                        <el-input v-model="infolist.msgTit" placeholder="字典类型" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item prop="msgType">
                        <span>字典编码</span>
                       <el-input v-model="infolist.msgTit" placeholder="字典名称" :disabled="disableStatus"></el-input>
                       </el-form-item>
                    </el-form-item>
                </div>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogCli(1)">确 定</el-button>
                <el-button @click="dialogCli(2)">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import mySearch from '../../../components/search'
    import toolList from '../../../components/toolList'

    export default {
        components: {
            mySearch,
            toolList
        },
        data() {
            const msgTit = (rule, value, callback) => {
                if (!this.infolist.msgTit) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const msgType = (rule, value, callback) => {
                if (!this.infolist.msgType) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const remark = (rule, value, callback) => {
                if (!this.infolist.remark) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    msgTit: [{
                        required: true,
                        trigger: 'blur',
                        validator: msgTit
                    }],
                    msgType: [{
                        required: true,
                        trigger: 'change',
                        validator: msgType
                    }],
                    remark: [{
                        required: true,
                        trigger: 'blur',
                        validator: remark
                    }]
                },
                tooltit:'字典管理',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                infolist: {},
                typeOptions: [],
                titname: '',
                disableStatus: false,
                fileList: [],
                visiblec: [],
                visibleList: [],
                attList: [],
                uploadstatus: false,
                userInfo: {},
                searchList: [
                    {
                        type: 'sel',
                        tit: '字典类型',
                        value: '',
                        width: '48%',
                        options:[]
                    },
                    {
                        type: 'input',
                        tit: '字典名称',
                        value: '',
                        width: '48%'
                    }
                ],
                params: {},
                btnctrlStatus:true
            }
        },
        methods: {
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.noticeTitle = v[0] : '';
                v[1] ? this.params.opTimeFrom = v[1] : '';
                this.getList(this.params, this.pageSize);
            },
            //获取列表
            getList(v, n) {
                // this.pageSize = n;
                // let _this = this;
                // this.$http({
                //     url: "/sysmgr/notice/queryPage",
                //     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;
                // });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //查看 修改 新增
            dialogCheck(v, n) {
                this.dialogStatus = true;
                this.disableStatus = false;
                this.visiblec = [];
                this.infolist = {};
                if (v === 1) {
                    this.titname = '新建';
                    this.getUser();
                    return
                } else if (v === 2) {
                    this.titname = '查看';
                    this.disableStatus = true;
                } else if (v === 3) {
                    this.titname = '修改';
                }
                this.infolist = Object.assign({}, n); //拷贝
                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.infolist.deptList = res.data.deptList;
                    this.fileInfo.fileList = [];
                    this.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
                        });
                    })
                    res.data.deptList.forEach(item => {
                        this.visiblec.push(item.deptCode);
                    })
                });
            },
            dialogCli(v) {
                this.uploadstatus = false;
                if (v === 2) {
                    this.infolist = {};
                    this.dialogStatus = false;
                    return
                }
                this.infolist.pubTime = new Date().getTime();
                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.visibleList = this.visibleList;
                        this.infolist.sts = "0";
                        this.infolist.stsDesc = "草稿";
                        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
                            });
                        }
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: {
                                noticeInfo: this.infolist,
                                attList: attList,
                                deptList: this.visibleList
                            },
                        }).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.infolist.deptCode = '';
                                _this.dialogStatus = false;
                                _this.getList(this.params, this.pageSize);
                            }

                        });
                    }
                })
            },
            //删除
            delLine(v) {
                let _this = this;
                this.$confirm('即将删除此条数据, 是否删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    _this.$http({
                        url: "/sysmgr/cnotice/del",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            noticeInfo: {
                                noticeId: v.noticeId
                            }
                        }
                    }).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);
                        }
                    });
                })
            },
            //处理所需数据
            verifcheck(v) {
                if (v === 2) {
                    for (let i = 0; i < this.typeOptions.length; i++) {
                        if (this.typeOptions[i].id === this.infolist.deptCode) {
                            this.infolist.deptName = this.typeOptions[i].ou
                        }
                    }
                }
            },
            //处理所需数据
            verifall(v) {
                this.visibleList = [];
                for (let i = 0; i < v.length; i++) {
                    for (let j = 0; j < this.typeOptions.length; j++) {
                        if (this.typeOptions[j].id === v[i]) {
                            this.visibleList.push({
                                deptCode: this.typeOptions[j].id,
                                deptName: this.typeOptions[j].ou
                            });
                        }
                    }
                }
            },
            //文件返回值
            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;
                });
            },
            ctrlbtn(v) {
                for (let i = 0; i < v.length; i++) {
                    if (v[i].children.length > 0) {
                        this.ctrlbtn(v[i].children);
                    } else {
                        if (v[i].rountPath === this.$route.path && v[i].systemflag === '1') {
                            this.btnctrlStatus = false;
                            return
                        }
                    }
                }
            },
            getUser() {
                let menus = JSON.parse(window.sessionStorage.menus);
                this.ctrlbtn(menus);
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);
                this.infolist = {
                    opNo: '',
                    regionName: '',
                    regionCode: '',
                    deptName: '',
                    deptCode: '',
                    opName: '',
                    phoneNo: '',
                };
                this.infolist.opNo = this.userInfo.loginNo;
                // this.infolist.regionName = '';
                // this.infolist.regionCode = '';
                // this.infolist.deptName = '';
                // this.infolist.deptCode = '';
                this.infolist.opName = this.userInfo.loginName;
                this.infolist.phoneNo = this.userInfo.phoneNo;
            }
        },
        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: 15px;
    }

    .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);
        }
    }

    .online {
        width: 100%;

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

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 100px);
        }
    }
</style>
