layui.extend({
    common    :   'js/common',
    region    :   'layui/lay/mymodules/region'
}).use(['common','jquery','layer', 'element','laydate','form','region'], function () {
        var common = layui.common;
        var $ = layui.jquery;
        var layer = layui.layer;
        var element = layui.element;
        var laydate=layui.laydate;
        var form=layui.form;
        // 渲染销售额图表
        var myCharts3 = echarts.init(document.getElementById('xse'), myEchartsTheme);
        var option3 = {
            grid: {
                left: '0',
                right: '0',
                top: '30',
                containLabel: true
            },
            tooltip: {
                trigger: "axis",
                backgroundColor:'rgba(255,255,255,0.8)',//通过设置rgba调节背景颜色与透明度
                color:'black',
                borderWidth:'1',
                borderColor:'#dadada',
                textStyle:{
                    color:'#575757',
                    fontSize:'12'
                },
                extraCssText:'line-height:26px;padding:10px;box-shadow:0 0 10px rgba(0,0,0,0.3)',


            },
            legend: {
                data: ["截至当月总设备数", "当月新增设备","当月故障设备"],
                icon: "rect",
                itemWidth: 15,
                itemHeight: 4,
                x: 'center', // 'center' | 'left' | {number},

                y: '93%', // 'center' | 'bottom' | {number}
            },
            calculable: !0,
            xAxis: [{
                type: "category",
                axisPointer: {//指示线

                    snap: true,
                    lineStyle: {
                        show:false,
                        color: '#666',
                        opacity: 0.6,
                        width: .8
                    },

                },    axisLine: {
                    lineStyle: {
                        color: '#5a5a5a'
                    }
                },

                data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
            }],
            yAxis: [{
                type: "value",

                splitArea:{show:false},
                type: "value",
                axisTick:{       //y轴刻度线
                    "show":false
                }
                ,axisLine: {
                    show: false,
                    lineStyle: {

                        color: '#5a5a5a', // 颜色
                    }
                },
                name: "",
                axisLabel: {
                    formatter: "{value}"
                },
                splitLine :{    //网格线
                    lineStyle:{
                        type:'dashed',    //设置网格线类型 dotted：虚线   solid:实线
                        width:'0.8',

                    },
                    show:true //隐藏或显示
                }
            }],
            series: [{
                name: "截至当月总设备数",
                type: "line",
                data:[],
                itemStyle: {
                    normal: { color: "#1f93ff" },
                }

            }]
        };
        // 窗口大小改变事件
        window.onresize = function () {
            myCharts3.resize();
        };
         myCharts3.setOption(option3);
       //查询
        var queryChartFu=function(){
            var  xAxisArra=["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
            var currentDate=new Date();
            var year=$("#laydate-range-year").val();
            if(year==""){
                year=currentDate.getFullYear();
                $("#laydate-range-year").val(year);
            }

            //统计图数据类型
            var chartType= $("#chart-type-id").val();
            var statisticsType=0;
            var url="/statistics/statisticsDevice/statisticsMonthAddDeviceAndTotal";
            var seriesName="截至当月总设备数";
            if(chartType=="month-add"){
                seriesName="当月新增设备";
                statisticsType=1;
                url="/statistics/statisticsDevice/statisticsMonthAddDeviceAndTotal";
            }else if(chartType=="month-malfunction"){
                seriesName="当月故障设备";
                url="/statistics/sysDeviceLog/statisticsDeviceMalfunction";
            }
            var params={
                regionProvince: $('input[name="regionProvince"]').val(),
                regionCity: $('input[name="regionCity"]').val(),
                regionArea:$('input[name="regionArea"]').val(),
                year:year,
                statisticsType:statisticsType
            }
            var deviceCountSeries=new Array();
            var layerIndex = layer.load(1);
            //获取统计数据
            common.sendAsyncRequest(url,"POST",params,function(result) {
                layer.close(layerIndex);
                var resultMap=result.data;
                var deviceCountYear=resultMap['deviceCountYear'];
                var addDeviceCountList=resultMap['addDeviceCountList'];
                if(chartType=="month-malfunction"){
                    addDeviceCountList=resultMap;
                }
                var baseCount=0;
                if(statisticsType==0&&deviceCountYear){
                    baseCount=Number(deviceCountYear);
                }
                //格式化数据成图形需要的
                for(var a=0;a<=xAxisArra.length;a++){
                    for(var index in addDeviceCountList){
                            var map=addDeviceCountList[index];
                            var deviceCount=map['deviceCount'];
                            var month=map.month;
                            //图形x轴日期
                            var xMonth=a+1;
                            if(xMonth==month){
                                deviceCountSeries[a]=Number(deviceCount)+Number(baseCount);
                                if(statisticsType==0){
                                    baseCount=deviceCountSeries[a];
                                }
                                break;
                            }else{
                                    deviceCountSeries[a]=baseCount;
                            }
                    }
                }
                //重新加载统计图形
                myCharts3.setOption({
                    xAxis: {
                        data:xAxisArra
                    },
                    series: [{
                        name: seriesName,
                        type: "line",
                        data:deviceCountSeries
                    }]
                });
            });
        }
        //日期年份选择
        laydate.render({
            elem: '#laydate-range-year'
            ,type: 'year'
        });
          var  active = {
            query: function(){
                queryChartFu();
            },
            reset: function(){
                //清空条件
                var currentDate=new Date();
                $("#laydate-range-year").val(currentDate.getFullYear());
                $('input[name="regionProvince"]').val("");
                $('input[name="regionCity"]').val("");
                $('input[name="regionArea"]').val("");
                $("#region").val("");
                // 刷新select标签
                form.render('select');
                queryChartFu();
            },
              statisticsDeviceCount: function(){
              //统计页面头部数据
              $.ajax({
                  type: "POST",
                  url: "/statistics/statisticsDevice/statisticsDeviceTotal",
                  data: {},
                  success: function(msg){
                      var code=msg.code;
                      //状态
                      if(code==200){
                          var map=msg.data;
                          if(map){
                              //设备总数
                              var deviceTotalCount=map['deviceCount'];
                              if(deviceTotalCount){
                                  $("#deviceTotalCount").text(deviceTotalCount);
                              }
                              //小区门口设备总数
                              var residentialDeviceTotalCount=map['residentialDeviceCount'];
                              if(residentialDeviceTotalCount){
                                  $("#residentialDeviceTotalCount").text(residentialDeviceTotalCount);
                              }
                              //单元设备总数
                              var unitDeviceTotalCount=map['unitDeviceCount'];
                              if(unitDeviceTotalCount){
                                  $("#unitDeviceTotalCount").text(unitDeviceTotalCount);
                              }
                          }
                      }
                  }
              });
              }
        };
          //查询按钮
        $(document).on('click','.layui-btn', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
            return false;
        });

    //  初始化省市区
    layui.region.init({
        //  元素节点
        elem        :  '#region_body',
        //  触发此元素时显示
        trigger     :   '#region',
        //  选中省的回调
        provinceCallbackFn  :   function(data)  {
            var address = data.province.text;
            $('#region').val(address);
            $('input[name="regionProvince"]').val(data.province.id);
        },
        //  选中市的回调
        cityCallbackFn  :   function(data) {
            var address = data.province.text + '-' + data.city.text;
            $('#region').val(address);
            $('input[name="regionCity"]').val(data.city.id);
        },
        //  选中区域的回调
        areaCallbackFn  :   function(data) {
            var address = data.province.text + '-' + data.city.text + '-' +data.county.text;
            $('#region').val(address);
            $('input[name="regionArea"]').val(data.county.id);
        },
        //  关闭时的回调
        hideCallbackFn  :   function(data) {
        },
        //清空回调
        clearFn : function(){
            $node.regionInput.val('');
            $node.provinceInput.val('');
            $node.cityInput.val('');
            $node.areaInput.val('');
        }
    });
    //统计图切换
    $(document).on('click','.layui-btn.layui-btn-primary.layui-btn-sm', function(){
        var type = $(this).data('type');
        $(this).addClass('font-col').siblings().removeClass('font-col')
        switch (type) {
            case 'month-total':
                //截止当月设备总数
                $("#chart-type-id").val("month-total")
                queryChartFu();
                break;
            case 'month-add':
                //当月新增设备
                $("#chart-type-id").val("month-add");
                queryChartFu();
                break;
            case 'month-malfunction':
                //当月故障设备
                $("#chart-type-id").val("month-malfunction");
                queryChartFu();
                break;
        }
    });

    //初始化
    var init=function(){
        queryChartFu();
        active.statisticsDeviceCount();
    }
    //初始化数据
    init();
 });
