<template>
    <div>
        <div class="container">
            <div class="container-box">
                <div class="adv-type">
                    <div v-for="item in typeList" :key="item.dataCode" @click="advmaterial(item)" v-if="companyFlag == 0||item.dataCode!=1">
                        <span><i class="el-icon-folder-opened"></i></span>
                        <span>{{item.dataName}}</span>
                    </div>
                </div>
                <div class="adv-box">
                    <div class="adv-tit" @click="jumpadv('1')">
                        <i class="el-icon-loading"></i>
                        <span>进行中 </span>
                    </div>
                    <div class="adv-tit" @click="jumpadv('2')">
                        <i class="el-icon-notebook-1"></i>
                        <span>已结束 </span>
                    </div>
                    <div class="adv-tit adv-add" @click="advadd">
                        <i class="el-icon-plus"></i>
                        <span>申请</span>
                    </div>
                </div>
            </div>
        </div>
        <el-dialog title="添加" :visible.sync="dialogStatus" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false">
            <el-form :model="infolist" ref="infolist" :rules="rules">
                <el-form-item prop="taskName" class="info-line online">
                    <span>统计名称</span>
                    <el-input v-model="infolist.taskName" placeholder="统计名称"></el-input>
                </el-form-item>

                <el-form-item prop="time" class="info-line online">
                    <span>统计周期</span>
                    <el-date-picker v-model="infolist.time" type="daterange"></el-date-picker>
                </el-form-item>

                <el-form-item prop="tempId" class="info-line online">
                    <span>填报模板</span>
                    <el-select v-model="infolist.tempId" placeholder="填报模板">
                        <el-option v-for="item in typeOptions" :key="item.tempId" :label="item.tempName" :value="item.tempId">
                        </el-option>
                    </el-select>
                </el-form-item>

               <!-- <el-form-item class="info-line online">
                    <span>审批说明</span>
                    <el-input v-model="infolist.remark" placeholder="审批说明" type="textarea"
                        :autosize="{ minRows: 4, maxRows: 10}"></el-input>
                </el-form-item> -->
            </el-form>
            <div style="text-align: right;margin-top: 20px">
                <el-button @click="dialogCli(1)" type="primary">确 定</el-button>
                <el-button @click="dialogCli(2)">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="汇总" :visible.sync="dialogall" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false">
            <el-form :model="infolist" ref="infolist" :rules="rules">
                <el-form-item prop="woName" class="info-line online">
                    <span>选择模板</span>
                    <el-select v-model="visiblec" placeholder="选择模板" multiple>
                        <el-option v-for="item in mubanopt" :key="item.tempId" :label="item.tempName+' 总数： '+item.cnt" :value="item.tempId">
                        </el-option>
                    </el-select>
                </el-form-item>

                <!-- <el-form-item prop="time" class="info-line online">
                    <span>选择任务</span>
                    <el-select v-model="visiblecc" placeholder="选择任务" multiple>
                        <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>
            <div style="text-align: right;margin-top: 20px">
                <el-button @click="dialogCliall" type="primary">导 出</el-button>
                <el-button @click="dialogall = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import deptTree from "../../../components/deptTreeUser.vue"
    import deptTreep from "../../../components/deptTreeP.vue"
    export default {
        components: {
            deptTree,
            deptTreep
        },
        data() {
            const taskName = (rule, value, callback) => {
                if (!this.infolist.taskName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const time = (rule, value, callback) => {
                if (!this.infolist.time) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const tempId = (rule, value, callback) => {
                if (!this.infolist.tempId) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    taskName: [{
                        required: true,
                        trigger: 'blur',
                        validator: taskName
                    }],
                    time: [{
                        required: true,
                        trigger: 'change',
                        validator: time
                    }],
                    tempId: [{
                        required: true,
                        trigger: 'change',
                        validator: tempId
                    }],
                },
                typeList: [{
                        dataCode: '1',
                        dataName: '模板管理'
                    },
                    {
                        dataCode: '2',
                        dataName: '信息汇总'
                    },
                    //                {
                    // 	dataCode: '3',
                    // 	dataName: '催办策略配置'
                    // }
                ],
                dialogStatus: false,
                disableStatus: false,
                dialogStatusp: false,
                infolist: {},
                treeList: [],
                treeListp: [],
                treeListponly: [],
                closeList: [],
                defaultList: [],
                depttype: 0,
                fadept: {},
                deptList: [],
                dialogall: false,
                typeOptions:[],
                visiblec:[],
                mubanopt:[],
                companyFlag:''
            }
        },
        methods: {
            //导出前判断
            dialogCliall() {
                if (this.visiblec.length == 0) {
                    this.$message({
                        message: '请先选择任务！',
                        type: 'error'
                    });
                    return
                }else{
                    let allnum = 0;
                    for(let i=0;i<this.visiblec.length;i++){
                        for(let ii=0;ii<this.mubanopt.length;ii++){
                            if(this.mubanopt[ii].tempId == this.visiblec[i]){
                                allnum += this.mubanopt[ii].cnt*1
                            }
                        }
                    }
                    if (allnum>1000000) {
                        this.$message({
                            message: '任务总条数不可超出100万条，请重新选择！',
                            type: 'error'
                        });
                        return
                    }
                }
                let visiblec = '';
                for(let i=0;i<this.visiblec.length;i++){
                    if(i==0){
                        visiblec = this.visiblec[i];
                    }else{
                        visiblec = visiblec+','+this.visiblec[i]
                    }
                }
                let param = new FormData();
                param.append("tempIds", visiblec);
                this.$http({
                    url: '/market/cChannelInfo/cIllegalCallTask/excelCollect',
                    method: "post",
                    responseType: 'blob',
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: param,
                }).then((response) => {
                    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                        let blob = new Blob([response.data], {
                            type: response.data.type
                        });
                        window.navigator.msSaveOrOpenBlob(blob, '汇总数据.xlsx');
                    } else {
                        /* 火狐谷歌的文件下载方式 */
                        var blob = new Blob([response.data])
                        var downloadElement = document.createElement('a')
                        var href = window.URL.createObjectURL(blob);
                        downloadElement.href = href;
                        downloadElement.download = '汇总数据.xlsx';
                        document.body.appendChild(downloadElement);
                        downloadElement.click();
                        document.body.removeChild(downloadElement);
                        window.URL.revokeObjectURL(href);
                    }
                    this.dialogall = false;
                    this.visiblec = [];
                });
            },
            //添加新任务
            dialogCli(v) {
                let _this = this;
                if (v === 1) {
                    let param = {};
                    param.taskName = this.infolist.taskName;
                    param.tempId = this.infolist.tempId;
                    for (let i = 0; i < this.typeOptions.length; i++) {
                        if(this.typeOptions[i].tempId == param.tempId){
                            param.tempName = this.typeOptions[i].tempName;
                        }
                    }
                    this.infolist.time ? param.startTime = this.$formatDate(this.infolist.time[0], "YYYY-MM-DD") : '';
                    this.infolist.time ? param.endTime = this.$formatDate(this.infolist.time[1], "YYYY-MM-DD") : '';
                    this.$refs.infolist.validate(valid => {
                        if (valid) {
                            this.$http({
                                url: "/market/cChannelInfo/cIllegalCallTask/add",
                                method: "post",
                                headers: {
                                    "Content-Type": "application/json",
                                },
                                data: param,
                            }).then((res) => {
                                if (res.data.result === 1) {
                                    _this.$message({
                                        message: res.data.desc,
                                        type: 'error'
                                    });
                                } else {
                                    _this.$message({
                                        message: '成功',
                                        type: 'success'
                                    });
                                    _this.infolist = {};
                                    _this.treeList = [];
                                    _this.treeListp = [];
                                    _this.dialogStatus = false;
                                }
                            });
                        }
                    })
                } else if (v === 2) {
                    this.infolist = {};
                    this.treeList = [];
                    this.treeListp = [];
                    this.dialogStatus = false;
                }
            },
            advadd() {
                this.dialogStatus = true;
                //获取模板
                this.$http({
                    url: "/sysmgr/csysdept/queryRegionDeptList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {},
                }).then((res) => {
                    this.deptList = res.data[0].children;
                });
            },
            jumpadv(v) {
                if (v == 1) {
                    this.$router.push({
                        path: "/underwayc",
                    });
                    this.setabList("进行中任务", "/underwayc");
                } else {
                    this.$router.push({
                        path: "/punishInfosumc",
                    });
                    this.setabList("已完成", "/punishInfosumc");
                }
            },
            advmaterial(v) {
                if (v.dataCode == 1) {
                    this.$router.push({
                        path: "/punishTemplatec",
                    });
                    this.setabList("模板管理", "/punishTemplatec");
                } else if (v.dataCode == 2) {
                    this.dialogall = true;
                } else if (v.dataCode == 3) {
                    // this.$router.push({
                    // 	path: "/dataStatistics",
                    // });
                    // this.setabList("数据统计", "/dataStatistics");
                }
            },
            setabList(n, p) {
                let params = {
                    children: "",
                    name: n,
                    rountPath: p,
                    target: "_self",
                };
                for (let i = 0; i < this.$store.state.tabList.length; i++) {
                    if (this.$store.state.tabList[i].name === params.name) {
                        this.$store.state.tabList[i] = params;
                    }
                }
                let set = new Set([...this.$store.state.tabList, params]);
                set.add(params);
                this.$store.commit("setDefaultActive", params.rountPath);
                this.$store.commit("setTabList", Array.from(set));
            },
            getlist() {
               this.$http({
                   url: "/market/cChannelInfo/cIllegalCallTemp/queryPage",
                   method: "post",
                   headers: {
                       "Content-Type": "application/json",
                       "page": '{"pageNo":"' + 1 + '","pageSize":"100"}'
                   },
                   data: {},
               }).then((res) => {
                   this.typeOptions = res.data.data;
               });
               this.$http({
                   url: "/market/cChannelInfo/cIllegalCallTemp/queryCollectList",
                   method: "post",
                   headers: {
                       "Content-Type": "application/json",
                       "page": '{"pageNo":"' + 1 + '","pageSize":"100"}'
                   },
                   data: {},
               }).then((res) => {
                   this.mubanopt = res.data;
               });
            },

            getUser() {
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);
                this.$http({
                    url: "/sysmgr/csysuserinfo/queryUserInfoByLoginNo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {},
                }).then((res) => {
                    this.companyFlag = res.data.companyFlag
                });
            },
        },
        mounted() {
            this.getUser();
            this.getlist();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .container-box {
        padding: 0 30px;
        height: calc(100% - 40px);
        overflow-y: scroll;
        padding-bottom: 20px;
    }

    .adv-box {
        margin-top: 20px;
        display: flex;
        flex-wrap: nowrap;
        // justify-content: space-between;

        div {
            display: inline-block;
            cursor: pointer;

            .el-card {
                width: 100%;
            }

            i {
                margin-right: 10px;
            }
        }

        .adv-tit {
            width: 28%;
            border: 1px solid transparent;
            border-radius: 5px;
            height: 60px;
            line-height: 60px;
            padding-left: 20px;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
            overflow: hidden;
            margin-right: 2%;

            em {
                float: right;
                margin-right: 20px;
                font-style: normal;
                color: #0074D9;
            }
        }

        .adv-tit:hover {
            border: 1px solid #CFE8FC;
            background: #CFE8FC;
        }

        .adv-add {
            width: 100px;
        }

    }

    .adv-type {
        margin-top: 20px;
        display: flex;
        // justify-content: space-between;
        flex-wrap: wrap;
        // border: 1px solid #ddd;
        border-radius: 5px;
        padding: 20px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);

        div {
            width: 24%;
            text-align: center;
            height: 80px;
            overflow: hidden;
            min-width: 100px;
            cursor: pointer;
            padding-top: 10px;
            margin: 10px 0;
        }

        div:hover {
            background: #CFE8FC;
            border-radius: 5px;
        }

        span {
            width: 100%;
            display: inline-block;
            height: 40px;

            // line-height: 40px;
            i {
                color: #0074D9;
                font-size: 36px;
            }
        }
    }

    .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(50% - 60px);
            display: inline-block;
            margin-right: 20px;
            height: 300px;
            overflow-y: scroll;

            p {
                overflow: hidden;
                display: flex;
                justify-content: space-between;
            }

            .p-btn {
                width: 50px !important;
                cursor: pointer;
                color: #0074DA;
            }

            span {
                display: inline-block;
                width: calc(100% - 100px);
            }

            .el-icon-error {
                float: right;
                font-size: 20px;
                margin-top: 9px;
                cursor: pointer;
            }
        }

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

    .treeUserp {
        p {
            padding: 5px;
        }

        p:hover {
            background: #01BEF0;
            color: #fff;
            cursor: pointer;
        }
    }

    .online {
        width: 100%;

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

        span {
            vertical-align: top;
        }

        .el-textarea,
        .el-date-editor {
            width: calc(100% - 100px);
            display: inline-flex;
        }
    }
</style>
