//人员大数据统计
function user() {
    $.get('/api/Project/user',{orgid:orgid},function (data) {
        if(data.status == 0){
            if(data.data.length > 0){
                // 保洁
                var cleaning = echarts.init(document.getElementById('cleaning'));
                cleaningOption = {
                    color: ['#2b5e8e', '#40edfe'],
                    title: {
                        text: '保洁人员',
                        textStyle: {
                            color: '#fff',
                            fontSize: 14,
                        },
                        y: 'center',
                        x: 'right',

                    },
                    series: [{
                        name: '保洁人员',
                        type: 'pie',
                        radius: ['70%', '80%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center',
                                textStyle: {
                                    fontSize: 14,
                                    color: '#eac304',
                                },
                                formatter: '{c}%'
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            {
                                value: 100 - data.data[2]['percent'],
                                color: '#40edfe',
                                label: {
                                    normal: {
                                        show: false
                                    }
                                }
                            },
                            {
                                value: data.data[2]['percent'],
                                color: '#2b5e8e',
                                label: {
                                    normal: {
                                        show: true,
                                        position: 'center',
                                        textStyle: {
                                            fontSize: 14,
                                            color: '#eac304',
                                        },
                                        formatter: '{c}%'
                                    }
                                }
                            }
                        ]
                    }]
                };
                cleaning.setOption(cleaningOption);

                // 保安
                var security = echarts.init(document.getElementById('security'));
                securityOption = {
                    color: ['#2b5e8e', '#40edfe'],
                    title: {
                        text: '保安人员',
                        textStyle: {
                            color: '#fff',
                            fontSize: 14,
                        },
                        y: 'center',
                        x: 'right',

                    },
                    series: [{
                        name: '保安人员',
                        type: 'pie',
                        radius: ['70%', '80%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                textStyle: {
                                    fontSize: 14,
                                    color: '#eac304',
                                },
                                formatter: '{c}%'
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [{
                            value: 100 - data.data[3]['percent'],
                            color: '#40edfe',
                            label: {
                                normal: {
                                    show: false
                                }
                            }
                        }, {
                            value: data.data[3]['percent'],
                            color: '#2b5e8e',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'center',
                                    textStyle: {
                                        fontSize: 14,
                                        color: '#eac304',
                                    },
                                    formatter: '{c}%'
                                }
                            }
                        },

                        ]
                    }]
                };
                security.setOption(securityOption);

                // 维修
                var repair = echarts.init(document.getElementById('repair'));
                repairOption = {
                    color: ['#2b5e8e', '#40edfe'],
                    title: {
                        text: '维修人员',
                        textStyle: {
                            color: '#fff',
                            fontSize: 14,
                        },
                        y: 'center',
                        x: 'right',

                    },
                    series: [{
                        name: '维修人员',
                        type: 'pie',
                        radius: ['70%', '80%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                textStyle: {
                                    fontSize: 14,
                                    color: '#eac304',
                                },
                                formatter: '{c}%'
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [{
                            value: 100 - data.data[1]['percent'],
                            color: '#40edfe',
                            label: {
                                normal: {
                                    show: false
                                }
                            }
                        }, {
                            value: data.data[1]['percent'],
                            color: '#2b5e8e',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'center',
                                    textStyle: {
                                        fontSize: 14,
                                        color: '#eac304',
                                    },
                                    formatter: '{c}%'
                                }
                            }
                        },

                        ]
                    }]
                };
                repair.setOption(repairOption);

                // 运送
                var transport = echarts.init(document.getElementById('transport'));
                transportOption = {
                    color: ['#2b5e8e', '#40edfe'],
                    title: {
                        text: '运送人员',
                        textStyle: {
                            color: '#fff',
                            fontSize: 14,
                        },
                        y: 'center',
                        x: 'right',

                    },
                    series: [{
                        name: '运送人员',
                        type: 'pie',
                        radius: ['70%', '80%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                textStyle: {
                                    fontSize: 14,
                                    color: '#eac304',
                                },
                                formatter: '{c}%'
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [{
                            value: 100 - data.data[0]['percent'],
                            color: '#40edfe',
                            label: {
                                normal: {
                                    show: false
                                }
                            }
                        }, {
                            value: data.data[0]['percent'],
                            color: '#2b5e8e',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'center',
                                    textStyle: {
                                        fontSize: 14,
                                        color: '#eac304',
                                    },
                                    formatter: '{c}%'
                                }
                            }
                        },

                        ]
                    }]
                };
                transport.setOption(transportOption);
            }
        }
    });
}

// 人员统计玫瑰图
function personnel() {
    $.get('/api/Project/personnel',{orgid:orgid},function (data) {
        if(data.status == 0){
            if(data.data.length > 0){
                var personnel = echarts.init(document.getElementById('personnel'));
                personnelOption = {
                    color:['#fdd100','#ec5400','#40ebff','#ff4874'],
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        x : 'left',
                        y : 'center',
                        left: 50,
                        textStyle: {
                            color: '#fff',
                            fontSize: 12,
                        },
                        orient: 'vertical',
                        data:['保洁','保安','维修','运送']
                    },

                    calculable : true,
                    series : [
                        {
                            name:'人员大数据统计',
                            type:'pie',
                            radius : [20, 70],
                            center : ['60%', '50%'],
                            roseType : 'radius',
                            data:data.data,
                            // data:[
                            //     {value:30, name:'保洁'},
                            //     {value:25, name:'保安'},
                            //     {value:20, name:'维修'},
                            //     {value:25, name:'运送'}
                            // ],
                            label: {
                                formatter: '{b}{d}%',
                                color: '#fff'
                            }
                        }
                    ]
                };
                personnel.setOption(personnelOption);
            }
        }
    });
}


// 工单统计
function todo(type) {
    $.get('/api/Project/todo',{orgid:orgid, type: type},function (data) {
        if(data.status == 0){
            if(data.data.list.length > 0){
                var ordersChart = echarts.init(document.getElementById("orders"));
                ordersOption = null;
                ordersOption = {
                    color:'#00c6ff',
                    tooltip: {},
                    dataset: {
                        dimensions: ['day', '工单量'],
                        source: data.data.list,
                    },
                    xAxis: {
                        type: 'category',
                        axisLabel: {
                            color:'#fff',
                        }
                    },
                    yAxis: {
                        axisLabel: {
                            color:'#fff',
                        }
                    },
                    series: [
                        {
                            type: 'bar',
                            barMaxWidth:'30',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top',
                                    color: '#fff'
                                }
                            },
                        }
                    ]
                };
                if (ordersOption && typeof ordersOption === "object") {
                    ordersChart.setOption(ordersOption, true);
                }
                $('.todo').removeClass('active');
                $('#todo'+type).addClass('active');
            }
        }
    });
}

//投诉大数据
function complaintList() {
    $.get('/api/Project/complaint',{orgid:orgid},function (res) {
        if(res.status == 0) {
            let list = res.data.data;
            if (list.length > 0) {
                var str = '';
                for (var i in list){
                    str += '<tr>';
                    str += '<td class="table-project-name">' + list[i]['CONTENT'] + '</td>';
                    str += '<td>' + list[i]['CREATTE_TIME'].replace(/(\d{4}).(\d{1,2}).(\d{1,2}).+/mg, '$1-$2-$3') + '</td>';
                    // str += '<td>' + list[i]['REAL_NAME'].replace(/(?<=.)./g, '*') + '</td>';
                    str += '</tr>';
                }
                $('#complaintList').html(str);
            }
        }
    })
}

// 投诉图表
function complaintChart() {
    $.get('/api/Project/complaintChart',{orgid: orgid},function (data) {
        if(data.status == 0){
            if(data.data.x.length > 0){
                var complaint = echarts.init(document.getElementById('complaint'));
                complaintOption = null;
                complaintOption = {
                    color:'#2ca2fc',
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    grid: {
                        top: '3%',
                        left: '3%',
                        right: '4%',
                        bottom: '0',
                        // minHeight: 200,
                        containLabel: true
                    },
                    xAxis: {
                        type: 'value',
                        boundaryGap: [0, 0.01],
                        axisLabel: {
                            color:'#fff',
                        }
                    },
                    yAxis: {
                        type: 'category',
                        data: data.data.y,
                        axisLabel: {
                            color:'#fff',
                        }
                    },
                    series: [
                        {
                            name: '投诉量',
                            type: 'bar',
                            barMaxWidth: 30,
                            label: {
                                normal: {
                                    show: true,
                                    position: 'right',
                                    color: '#fff'
                                }
                            },
                            data: data.data.x
                        }
                    ]
                };
                complaint.setOption(complaintOption);
            }
        }
    });
}

//任务大数据监控
function taskList() {
    $.get('/api/Project/task',{orgid:orgid},function (res) {
        if(res.status == 0) {
            let list = res.data.data;
            if (list.length > 0) {
                var str = '';
                for (var i in list){
                    str += '<tr>';
                    str += '<td class="table-project-name">' + list[i]['TITLE'] + list[i]['NAME'] + '</td>';
                    str += '<td>' + list[i]['CREATE_TIME'].replace(/(\d{4}).(\d{1,2}).(\d{1,2}).+/mg, '$1-$2-$3') + '</td>';
                    str += '<td>' + list[i]['NAME'] + '</td>';
                    str += '</tr>';
                }
                $('#taskList').html(str);
            }
        }
    })
}
function taskChart() {
    $.get('/api/Project/taskChart',{orgid: orgid},function (data) {
        if(data.status == 0){
            if(data.data.x.length > 0){
                var task = echarts.init(document.getElementById('task'));
                taskOption = {
                    color: ['#2ca2fc','#fdd100'],
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            crossStyle: {
                                color: '#999'
                            }
                        }
                    },
                    grid: {
                        top: '13%',
                        left: '3%',
                        right: '4%',
                        bottom: '-2%',
                        // minHeight: 120,
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data : data.data.x,
                            axisPointer: {
                                type: 'shadow'
                            },
                            axisLabel: {
                                color:'#fff',
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            axisLabel: {
                                formatter: '{value}',
                                color:'#fff',
                            },
                            axisLine: {
                                show: true,
                                lineStyle: {
                                    color: '#ccc'
                                }
                            }
                        },
                        // {
                        //     type: 'value',
                        //     axisLabel: {
                        //         formatter: '{value} %',
                        //         color:'#fff',
                        //     },
                        //     axisLine: {
                        //         show: true,
                        //         lineStyle: {
                        //             color: '#ccc'
                        //         }
                        //     }
                        // }
                    ],
                    series: [
                        {
                            name:'任务量',
                            type:'bar',
                            barWidth: 30,
                            data: data.data.y1,
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top',
                                    color: '#fff'
                                }
                            },
                        },
                        // {
                        //     name:'概率',
                        //     type:'line',
                        //     yAxisIndex: 1,
                        //     data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2]
                        // }
                    ]
                };
                task.setOption(taskOption);
            }
        }
    });
}


//安保
function hiddenDangerChart() {
    $.get('/api/Project/hiddenDangerChart',{orgid:orgid},function (data) {
        if(data.status == 0){
            if(data.data.x.length > 0){
                var hiddenDanger = echarts.init(document.getElementById('hidden-danger'));
                hiddenDangerOption = {
                    color:['#2ca2fc','#ffcb1d'],
                    tooltip : {
                        trigger: 'axis',
                        axisPointer : {
                            type : 'shadow'
                        }
                    },
                    grid: {
                        top: '10%',
                        left: '3%',
                        right: '4%',
                        bottom: '0',
                        containLabel: true
                    },
                    xAxis:  {
                        type: 'category',
                        axisLabel:{
                            show:true,
                            inside:false,
                            rotate:0,
                            margin: 8,
                            color:'#fff',
                        },
                        data: data.data.x,
                    },
                    yAxis: {
                        type: 'value',
                        axisLabel:{
                            show:true,
                            inside:false,
                            rotate:0,
                            margin: 8,
                            color:'#fff',
                        },
                        // data: ['0','500','1000','1500','2000']
                    },
                    series: [
                        {
                            name: '隐患数量',
                            type: 'bar',
                            stack: '总量',
                            barWidth:'30',
                            radius: ['70%', '100%'],
                            label: {
                                normal: {
                                    show: true,
                                    position: 'insideRight'
                                }
                            },
                            data: data.data.y1,
                        },
                        {
                            name: '转工单量',
                            type: 'bar',
                            stack: '总量',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'insideRight'
                                }
                            },
                            data: data.data.y2,
                        },

                    ]
                };
                hiddenDanger.setOption(hiddenDangerOption);
            }
        }
    });
}

// 安保表单
function hiddenDangerList() {
    $.get('/api/Project/hiddenDanger',{orgid:orgid},function (res) {
        if(res.status == 0) {
            let list = res.data.data;
            if (list.length > 0) {
                var str = '';
                for (var i in list){
                    if (list[i]['CURR_ORDER_MODE'] == 9 || list[i]['CURR_ORDER_MODE'] == 11) {
                        str += '<tr style="color: #adff2f;">';
                    } else {
                        str += '<tr>';
                    }
                    str += '<td class="table-project-name">' + list[i]['TITLE'] + list[i]['NAME'] +'</td>';
                    str += '<td>' + list[i]['CREATTE_TIME'].replace(/(\d{4}).(\d{1,2}).(\d{1,2}).+/mg, '$1-$2-$3') + '</td>';
                    str += '<td>' + list[i]['REAL_NAME'] + '</td>';
                    str += '</tr>';
                }
                $('#hiddenDangerList').html(str);
            }
        }
    })
}


// 巡更
function patrol() {
    $.get('/api/Project/patrol',{orgid:orgid},function (res) {
        if(res.status == 0) {
            var list = res.data.data;
            if (list != '') {
                var str = '';
                str += '<tr>';
                str += '<td>' + list['NAME'] +'地点' + '</td>';
                str += '<td>' + list['TITLE'] + '</td>';
                str += '</tr>';
                str += '<tr>';
                str += '<td>' + list['NAME'] +'时间' + '</td>';
                str += '<td>' + list['CREATE_TIME'].replace(/(\d{4}).(\d{1,2}).(\d{1,2}).+/mg, '$1-$2-$3') + '</td>';
                str += '</tr>';
                str += '<tr>';
                str += '<td>' + list['NAME'] +'人员' + '</td>';
                str += '<td>' + list['USER_NAME'] + '</td>';
                str += '</tr>';
                $('#patrol').html(str);
                $('#patrol-addr').html(list['TITLE']);

                $('#patrol-data-monitor').html(list['NAME']+'大数据监控');
                $('#patrol-data-title').html('最新'+ list['NAME']+'记录');

                var oImg = document.getElementById('patrol-img');
                var arr = list['IMAGES'];
                var num = 0;
                num++;
                if (num == arr.length) {
                    num = 0;
                }
                oImg.src = arr[num];
            }
            setTimeout("patrol()", 15000);
        }
    })
}

var timer = 1000*60*30;

todo(0);
user();
personnel();
taskList();
taskChart();
complaintList();
complaintChart();
hiddenDangerList();
hiddenDangerChart();
patrol(); //巡更

setInterval(function(){
    user();
    personnel();
    taskList();
    taskChart();
    complaintList();
    complaintChart();
    hiddenDangerList();
    hiddenDangerChart();
}, timer);

setInterval(function () {
    setTimeout(function(){
        todo(0);
    },5000);
    setTimeout(function(){
        todo(1);
    },10000);
    setTimeout(function(){
        todo(2);
    },15000);
},15000);

function locationIicon() {
    var id = document.getElementById("location-icon");
    var photo=$(".location-icon img");
    for(var i=0; photo.eq(i).val()==0;i++){
        var left = Math.floor( Math.random() * 100 + 200 );
        var top = Math.floor( Math.random() * 100 + 100);
        photo.eq(i).css({
            'position': 'absolute',
            'left':left +'px',
            'top' :top +'px',
        });
    }
}

setInterval(function () {
    locationIicon();
},5000);