<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%" v-loading="loading">
                    <el-table-column prop="taskTitle" label="任务标题">
                    </el-table-column>
                    <el-table-column prop="sendTimeShow" label="任务周期">
                    </el-table-column>
                    <el-table-column prop="opNo" label="定制工号">
                    </el-table-column>
                    <el-table-column prop="opName" label="定制姓名">
                        <template slot-scope="scope">
                              <span>{{$desensitization(scope.row.opName,1)}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="opTime" 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>
                        </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="true" :before-close="closedia" :destroy-on-close="true" :modal-append-to-body="false"
            :close-on-click-modal="false" :fullscreen="true">
            <div v-loading="loadinged">
                <el-form :model="infolist" ref="infolist" :rules="rules" style="max-height: calc(100vh - 180px);overflow-y: scroll;">


                    <div class="info-line">
                        <el-form-item prop="taskTitle">
                            <span>任务标题</span>
                            <el-input v-model="infolist.taskTitle" placeholder="任务标题" :disabled="disableStatus"></el-input>
                        </el-form-item>
                        <el-form-item prop="dataweek">
                            <span>派发周期</span>
                            <el-select v-model="dataweek" placeholder="派发周期" clearable :disabled="disableStatus"
                                @change="crontimes = ''">
                                <el-option v-for="item in dataoption" :key="item.id" :label="item.value" :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </div>

                    <div class="info-line" v-if="titname == '查看'">
                        <el-form-item>
                            <span>填报日期</span>
                            <el-input v-model="infolist.sendTimeShow" placeholder="填报日期" disabled></el-input>
                        </el-form-item>
                    </div>
                    <div class="info-line" v-if="titname != '查看'">
                        <el-form-item v-if="dataweek == 0" prop="crontimes">
                            <span>填报日期</span>
                            <el-date-picker v-model="crontimes" type="date" placeholder="任意时间点" :disabled="disableStatus">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item v-if="dataweek == 1" prop="crontimes">
                            <span>填报日期</span>
                            <el-select v-model="crontimes" placeholder="任意时间点" clearable :disabled="disableStatus">
                                <el-option v-for="item in weekopt" :key="item.id" :label="item.value" :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item v-if="dataweek == 2" prop="crontimes">
                            <span>填报日期</span>
                            <el-select v-model="crontimes" placeholder="任意时间点" clearable :disabled="disableStatus">
                                <el-option v-for="item in mouthopt" :key="item.id" :label="item.value" :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item prop="crontime">
                            <span>填报时间</span>

                            <el-time-picker v-model="crontime" placeholder="任意时间点" :disabled="disableStatus"></el-time-picker>

                        </el-form-item>
                    </div>


                    <el-form-item class="info-line online" v-if="!disableStatus">
                        <span>上报人员</span>
                        <deptTreeOnly class="tree" @treeCheck="treeCheck"></deptTreeOnly>
                        <div class="tree treeUser">
                            <p v-for="item in treeList" :key="item.receiveNo">{{item.receiveName}}</p>
                        </div>
                    </el-form-item>
                    
                    <el-form-item class="info-line online" prop="msgInfo">
                        <span>上报内容</span>
                        <el-input v-model="infolist.msgInfo" 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 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 @click="dialogCli(2)">取 消</el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown'
    import mySearch from '../../../components/search'
    import toolList from '../../../components/toolList'
    import deptTreeOnly from "../../../components/deptTreeUser.vue"

    export default {
        components: {
            myUpload,
            uploadDown,
            mySearch,
            toolList,
            deptTreeOnly
        },
        data() {
            const taskTitle = (rule, value, callback) => {
                if (!this.infolist.taskTitle) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const dataweek = (rule, value, callback) => {
                if (!this.dataweek && this.dataweek!== 0) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
             const crontimes = (rule, value, callback) => {
                if (!this.crontimes) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const crontime = (rule, value, callback) => {
                if (!this.crontime) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    taskTitle: [{
                        required: true,
                        trigger: 'blur',
                        validator: taskTitle
                    }],
                    dataweek: [{
                        required: true,
                        trigger: 'change',
                        validator: dataweek
                    }],
                    crontimes: [{
                        required: true,
                        trigger: 'change',
                        validator: crontimes
                    }],
                    crontime: [{
                        required: true,
                        trigger: 'change',
                        validator: crontime
                    }],
                },
                tooltit: '定制上报管理',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                infolist: {},
                typeOptions: [],
                titname: '',
                disableStatus: false,
                fileList: [],
                visiblec: [],
                visibleList: [],
                attList: [],
                fileInfo: {
                    limit: 10,
                    url: '/market/cwo/upload',
                    fileList: []
                },
                datalist: {
                    url: '/sysmgr/noticeatt/downfile',
                    type: 1
                },
                uploadstatus: false,
                userInfo: {},
                searchList: [
					// {
     //                    type: 'sel',
     //                    tit: '信息分类',
     //                    value: '',
     //                    width: '48%',
     //                    options: []
     //                },
                    {
                        type: 'input',
                        tit: '请输入标题',
                        value: '',
                        width: '98%'
                    },
                    // {
                    //     type: 'sel',
                    //     tit: '状态',
                    //     value: '',
                    //     width: '32%',
                    //     options: []
                    // }

                ],
                params: {},
                btnctrlStatus: true,
                dataoption: [{
                        id: 0,
                        value: '一次派发'
                    },
                    {
                        id: 1,
                        value: '按周派发'
                    },
                    {
                        id: 2,
                        value: '按月派发'
                    },
                    {
                        id: 3,
                        value: '按天派发'
                    }
                ],
                weekopt: [{
                        id: 1,
                        value: '周一'
                    },
                    {
                        id: 2,
                        value: '周二'
                    },
                    {
                        id: 3,
                        value: '周三'
                    },
                    {
                        id: 4,
                        value: '周四'
                    },
                    {
                        id: 5,
                        value: '周五'
                    },
                    {
                        id: 6,
                        value: '周六'
                    },
                    {
                        id: 7,
                        value: '周日'
                    }
                ],
                mouthopt: [],
                dataweek: '',
                crontimes: '',
                crontime: new Date(),
                loading: false,
                loadinged: false,
                treeList: [],
                defaultList:[],
                depttype:0,
                treeListonly:{},
            }
        },
        methods: {
            treeCheckonly(v) {
                this.treeListonly = v;
            },
            treeCheck(v){
                this.treeList = v;
            },
            closedia() {
                this.datalist.attList = [];
                this.uploadstatus = false;
                this.fileInfo.fileList = [];
                this.infolist = {};
                this.treeListonly = {};
                this.treeList = [];
                this.infolist.deptCode = '';
                this.dialogStatus = false;
                this.dataweek = '';
                this.crontimes = '';
                this.crontime = '';
                this.defaultList = [];
                this.depttype = 0;
            },
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.taskTitle = v[0] : '';
                this.getList(this.params, this.pageSize);
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.loading = true;
                this.tableData = [];
                this.$http({
                    url: "/market/custommsg/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;
                    this.loading = false;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //查看 修改 新增
            dialogCheck(v, n) {
                this.dialogStatus = true;
                this.disableStatus = false;
                this.visiblec = [];
                this.infolist = {};
                this.defaultList = [];
                if (v === 1) {
                    this.titname = '新建';
                    this.depttype = 1;
                    this.getUser();
                    return
                } else if (v === 2) {
                    this.titname = '查看';
                    this.disableStatus = true;
                } else if (v === 3) {
                    this.titname = '修改';
                    this.loadinged = true;
                }
                this.$http({
                    url: "/market/ccustommsg/queryInfo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        id: n.id
                    },
                }).then((res) => {
                    this.infolist = res.data;
                    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;
                    this.dataweek = res.data.cycleType * 1;
                    if (res.data.cycleType == 0) {
                        this.crontimes = res.data.sendTimeShow;
                        this.crontime = res.data.sendTimeShow;
                    } else if (res.data.cycleType == 1) {
                        this.crontimes = res.data.sendTime.split(" ")[5] * 1;
                        this.crontime = "2020-02-02 " + res.data.sendTimeShow.split(" ")[1];
                    } else if (res.data.cycleType == 2) {
                        this.crontimes = res.data.sendTime.split(" ")[3] * 1;
                        this.crontime = "2020-02-02 " + res.data.sendTimeShow.split(" ")[1];
                    } else if (res.data.cycleType == 3) {
                        this.crontimes = '';
                        this.crontime = "2020-02-02 " + res.data.sendTimeShow;
                    }
                    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.loadinged = false;
                });
            },
            dialogCli(v) {
                this.datalist.attList = [];
                this.uploadstatus = false;
                if (v === 2) {
                    this.fileInfo.fileList = [];
                    this.infolist = {};
                    this.treeListonly = {};
                    this.treeList = [];
                    this.infolist.deptCode = '';
                    this.dialogStatus = false;
                    this.dataweek = '';
                    this.crontimes = '';
                    this.crontime = '';
                    this.defaultList = [];
                    this.depttype = 0;
                    return
                }
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        this.infolist.opTime = this.$formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
                        this.infolist.opNo = this.userInfo.loginNo;
                        this.infolist.opName = this.userInfo.loginName;
                        this.infolist.cycleType = this.dataweek;
                        this.infolist.deptList = this.treeList;
                        this.infolist.sts = "1";
                        this.infolist.stsDesc = "已发布";
                        this.infolist.sendTime = '';
                        this.infolist.attList = [];
                        if (this.infolist.cycleType == 0) {
                            let time = this.$formatDate(this.crontimes, 'YYYY-MM-DD');
                            let timet = this.$formatDate(this.crontime, 'HH:mm:ss');
                            this.infolist.sendTimeShow = time + ' ' + timet;
                            time = time.split('-');
                            timet = timet.split(':');
                            if (timet[2][0] == 0) {
                                timet[2] = timet[2][1];
                            }
                            if (timet[1][0] == 0) {
                                timet[1] = timet[1][1];
                            }
                            if (timet[0][0] == 0) {
                                timet[0] = timet[0][1];
                            }
                            if (time[2][0] == 0) {
                                time[2] = time[2][1];
                            }
                            if (time[1][0] == 0) {
                                time[1] = time[1][1];
                            }
                            this.infolist.sendTime = timet[2] + ' ' + timet[1] + ' ' + timet[0] + ' ' + time[2] + ' ' + time[1] +
                                ' ' + '?';
                        } else if (this.infolist.cycleType == 1) {
                            let time = this.crontimes;
                            let timet = this.$formatDate(this.crontime, 'HH:mm:ss');
                            this.infolist.sendTimeShow = '周' + time + ' ' + timet;
                            timet = timet.split(':');
                            if (timet[2][0] == 0) {
                                timet[2] = timet[2][1];
                            }
                            if (timet[1][0] == 0) {
                                timet[1] = timet[1][1];
                            }
                            if (timet[0][0] == 0) {
                                timet[0] = timet[0][1];
                            }
                            this.infolist.sendTime = timet[2] + ' ' + timet[1] + ' ' + timet[0] + ' ' + '?' + ' ' + '*' + ' ' +
                                time;
                        } else if (this.infolist.cycleType == 2) {
                            let time = this.crontimes;
                            let timet = this.$formatDate(this.crontime, 'HH:mm:ss');
                            this.infolist.sendTimeShow = time + '号 ' + timet;
                            timet = timet.split(':');
                            if (timet[2][0] == 0) {
                                timet[2] = timet[2][1];
                            }
                            if (timet[1][0] == 0) {
                                timet[1] = timet[1][1];
                            }
                            if (timet[0][0] == 0) {
                                timet[0] = timet[0][1];
                            }
                            this.infolist.sendTime = timet[2] + ' ' + timet[1] + ' ' + timet[0] + ' ' + time + ' ' + '* ?';
                        } else if (this.infolist.cycleType == 3) {
                            let timet = this.$formatDate(this.crontime, 'HH:mm:ss');
                            this.infolist.sendTimeShow = timet;
                            timet = timet.split(':');
                            if (timet[2][0] == 0) {
                                timet[2] = timet[2][1];
                            }
                            if (timet[1][0] == 0) {
                                timet[1] = timet[1][1];
                            }
                            if (timet[0][0] == 0) {
                                timet[0] = timet[0][1];
                            }
                            this.infolist.sendTime = timet[2] + ' ' + timet[1] + ' ' + timet[0] + ' ' + '* * ?';
                        }

                        if (this.titname === '新建') {
                            this.submitInfo("/market/ccustommsg/addInfo", v);
                        } else if (this.titname === '修改') {
                            this.submitInfo("/market/ccustommsg/updateInfo", v);
                        }
                    }
                })

            },
            submitInfo(u, v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        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,
                                noticId: this.infolist.id,
                            });
                        }
                        this.infolist.attList = attList;
                        this.loadinged = true;
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: this.infolist
                        }).then((res) => {
                            this.loadinged = false;
                            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.treeListonly = {};
                                _this.infolist.deptCode = '';
                                _this.dialogStatus = false;
                                _this.getList(this.params, this.pageSize);
                                _this.defaultList = [];
                                _this.depttype = 0;
                            }

                        });
                    }
                })
            },
            //文件返回值
            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 = {};
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.opName = this.userInfo.loginName;
            },
            addmouthopt() {
                for (let i = 1; i < 29; i++) {
                    this.mouthopt.push({
                        id: i,
                        value: i + '号'
                    })
                }
            },
            getType() {
                this.$http({
                    url: "/market/qatype/queryPage",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + 1 + '","pageSize":"100"}'
                    },
                    data: {},
                }).then((res) => {
                    let list = [];
                    res.data.data.forEach(item => {
                        list.push({
                            dataCode: item.id,
                            dataName: item.msgType,
                        })
                    })
                    this.searchList[0].options = list;
                });
            }
        },
        mounted() {
            this.getList(this.params, this.pageSize);
            this.getDepot();
            this.getUser();
            this.getType();
            this.addmouthopt();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .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);
        }
        .tree {
            width: calc(100% - 100px);
        }
    }

    .online {
        width: 100%;

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

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 100px);
        }
        .tree {
            width: calc(50% - 60px);
            display: inline-block;
            margin-right: 20px;
            height: 300px;
            overflow-y: scroll;
        }

        .treeUser {
            margin: 0;
            border: 1px solid #ddd;

            p {
                background: #f4f4f4;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }

        .treeUserb {
            width: calc(100% - 100px);
            border: 1px solid #ddd;
            background: #f4f4f4;
            border-radius: 3px;
            height: auto;
            overflow: hidden;

            p {
                display: inline-block;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }
    }
</style>
