<template>
    <div class="container">
        <fullscreen :fullscreen.sync="fullscreen" class="container-box">
            <div class="titbox">
                <h2>督办任务管理</h2>
                <div>
                    <i class="el-icon-refresh" @click="iconCli(1)"></i>
                    <i class="el-icon-full-screen" @click="iconCli(2)"></i>
                    <!--                    <i class="el-icon-folder-opened"></i>-->
                    <!--                    <i class="el-icon-view"></i>-->
                    <!--                    <i class="el-icon-more"></i>-->
                </div>
            </div>
            <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="noticeTitle" label="任务标题">
                    </el-table-column>
                    <el-table-column prop="opName" label="发布人">
                    </el-table-column>
                    <el-table-column prop="phoneNo" label="派发姓名">
                    </el-table-column>
                    <el-table-column prop="deptName" 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="noticeTitle">
                        <span>任务标题</span>
                        <el-input v-model="infolist.noticeTitle" placeholder="标题" :disabled="disableStatus"></el-input>
                    </el-form-item>
                    <el-form-item prop="BackTime">
                        <span>反馈时间</span>
                        <el-date-picker v-model="infolist.BackTime" type="date" placeholder="要求反馈时间" :disabled="disableStatus">
                        </el-date-picker>
                    </el-form-item>
                </div>
                <div class="info-line">
                    <el-form-item prop="visiblec">
                        <span>督办类别</span>
                        <el-select v-model="visiblec" placeholder="督办类别" multiple clearable :disabled="disableStatus">
                            <el-option v-for="item in typeOptions" :key="item.id" :label="item.ou" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="visiblec">
                        <span>地市回复</span>
                        <el-select v-model="visiblec" placeholder="地市回复" multiple clearable :disabled="disableStatus">
                            <el-option v-for="item in typeOptions" :key="item.id" :label="item.ou" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </div>
                <el-form-item prop="visiblec" class="info-line online">
                    <span>接收部门</span>
                    <el-select v-model="visiblec" placeholder="接收部门" multiple clearable :disabled="disableStatus"
                        @change="verifall">
                        <el-option v-for="item in typeOptions" :key="item.id" :label="item.ou" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item class="info-line online" prop="noticeContent">
                    <span>任务描述</span>
                    <el-input 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-left: 100px;padding-right: 20px;" v-if="disableStatus">
                    <uploadDown :datalist="datalist" :dialogStatus="uploadstatus"></uploadDown>
                </div>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogCli(2)">取 消</el-button>
                <el-button @click="dialogCli(2)" type="primary">确 定</el-button>
                <el-button @click="dialogCli(2)" type="primary">召 回</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown'
    import mySearch from '../../../components/search'

    export default {
        components: {
            myUpload,
            uploadDown,
            mySearch
        },
        data() {
            const noticeTitle = (rule, value, callback) => {
                if (!this.infolist.noticeTitle) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const BackTime = (rule, value, callback) => {
                if (!this.infolist.BackTime) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const noticeContent = (rule, value, callback) => {
                if (!this.infolist.noticeContent) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            // const opName = (rule, value, callback) => {
            //     if (!this.infolist.opName) {
            //         callback(new Error('不能为空'))
            //     } else {
            //         callback()
            //     }
            // }
            // const phoneNo = (rule, value, callback) => {
            //     let x = this.infolist.phoneNo * 1;
            //     if (!(/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(x))) {
            //         callback(new Error('请输入正确的电话号码'))
            //     } else {
            //         callback()
            //     }
            // }
            // const deptCode = (rule, value, callback) => {
            //     if (!this.infolist.deptCode) {
            //         callback(new Error('不能为空'))
            //     } else {
            //         callback()
            //     }
            // }
            return {
                rules: {
                    noticeTitle: [{
                        required: true,
                        trigger: 'blur',
                        validator: noticeTitle
                    }],
                    BackTime: [{
                        required: true,
                        trigger: 'change',
                        validator: BackTime
                    }],
                    noticeContent: [{
                        required: true,
                        trigger: 'blur',
                        validator: noticeContent
                    }],
                    // opName: [{
                    //     required: true,
                    //     trigger: 'blur',
                    //     validator: opName
                    // }],
                    // phoneNo: [{
                    //     required: true,
                    //     trigger: 'blur',
                    //     validator: phoneNo
                    // }],
                    // deptCode: [{
                    //     required: true,
                    //     trigger: 'change',
                    //     validator: deptCode
                    // }],
                },
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                infolist: {},
                typeOptions: [],
                titname: '',
                disableStatus: false,
                fileList: [],
                visiblec: [],
                visibleList: [],
                attList: [],
                fileInfo: {
                    limit: 1,
                    url: '/sysmgr/noticeatt/upload',
                    fileList: []
                },
                datalist: {
                    url: '/sysmgr/noticeatt/downfile',
                    type: 2
                },
                uploadstatus: false,
                userInfo: {},
                searchList: [{
                        type: 'input',
                        tit: '请输入标题',
                        value: '',
                        width: '48%'
                    },
                    {
                        type: 'input',
                        tit: '发起人',
                        value: '',
                        width: '48%',
                    },
                ],
                params: {}
            }
        },
        methods: {
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.noticeTitle = v[0] : '';
                v[1] ? this.params.opTimeFrom = v[1] : '';
                v[2] ? this.params.opTimeTo = v[2] : '';
                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;
                //     for (let i = 0; i < this.tableData.length; i++) {
                //         _this.tableData[i].opTime = _this.timeChange(_this.tableData[i].opTime);
                //     }
                // });
            },
            // 分页
            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.datalist.attList = [];
                this.uploadstatus = false;
                if (v === 2) {
                    this.fileInfo.fileList = [];
                    this.infolist = {};
                    this.infolist.deptCode = '';
                    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;
            },
            //时间转换
            timeChange(v) {
                var time = new Date(v * 1);
                var y = time.getFullYear();
                var m = time.getMonth() + 1;
                m < 10 ? '0' + m : m;
                var d = time.getDate();
                d < 10 ? '0' + d : d;
                var h = time.getHours();
                h < 10 ? '0' + h : h;
                var mm = time.getMinutes();
                mm < 10 ? '0' + mm : mm;
                var s = time.getSeconds();
                s < 10 ? '0' + s : s;
                return y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s;
            },
            //功能栏
            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 = {
                    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>
