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 pushTaskType=$("#pushTaskType").val();
        // 渲染销售额图表
        var myCharts3 = echarts.init(document.getElementById('xse'), myEchartsTheme);
        var option3 = {
            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)',
            },
            grid: {
                left: '0',
                right: '0',
                top:"10",
                containLabel: true
            },
            calculable: !0,
            legend: {
                icon: "rect ",
                itemWidth: 15,
                itemHeight: 4,
                data: ["广告投放", "广告条数"],
                x: 'center', // 'center' | 'left' | {number},

                y: '90%', // 'center' | 'bottom' | {number}
            },
            xAxis: [{
                type: "category",
                axisPointer: {//指示线
                    snap: true,
                    lineStyle: {
                        show:false,
                        color: '#666',
                        opacity: 0.6,
                        width: .8
                    },

                },
                axisTick:{       //y轴刻度线
                    show:false
                },
                axisLine: {
                    lineStyle: {
                        color: '#5a5a5a'
                    }
                },
                data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
                //data: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"]
            }],
            yAxis: [{
                type: "value",
                name: "",
                splitArea:{show:false},
                axisLabel: {
                    formatter: "{value}"
                },
                axisLine: {
                    show: false,
                    lineStyle: {
                        color: '#5a5a5a'
                    }
                },
                splitLine :{    //网格线
                    lineStyle:{
                        type:'dashed',    //设置网格线类型 dotted：虚线   solid:实线
                        width:'0.8',

                    },
                    show:true //隐藏或显示
                }
            }],
            series: [{
                name: "广告投放",
                type: "line",
                //data: [900, 850, 950, 1e3, 1100, 1050, 1e3, 1150, 1250, 1370, 1250, 1100]
                data:[],
                itemStyle: {
                    normal: { color: "#1f93ff" },
                }

            }, {
                name: "广告条数",
                type: "line",
                //data: [870, 850, 850, 950, 1050, 1e3, 980, 1150, 1e3, 1300, 1150, 1e3]
                data:[],
                itemStyle: {
                    normal: { color: "#2fc25b" },
                }
            }]
        };
        // 窗口大小改变事件
        window.onresize = function () {
            myCharts3.resize();
        };
         myCharts3.setOption(option3);
       //查询
        var queryChartFu=function(){
            var  xAxisArra= new Array();
            var currentDate=new Date();
            var year=$("#laydate-range-year").val();
            var month=$("#select_month :selected").val();
            if(year==""){
                year=currentDate.getFullYear();
                $("#laydate-range-year").val(year);
            }
            var dateType=0;
            //月份统计
            if(!month){
                month=currentDate.getMonth()+1;
                xAxisArra=["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
            }else{
                //天数统计
                dateType=1;
                var day=laydate.getEndDate(month, year);
                xAxisArra== new Array();
                //初始化日期坐标
                for(var i=1;i<=day;i++){
                    xAxisArra[i-1]=i+"";
                }
            }
            var adExposureSeries=new Array();
            var adPushSumSeries=new Array();
            var deviceRegionProvince=$('input[name="regionProvince"]').val();
            var deviceRegionCity=$('input[name="regionCity"]').val();
            var deviceRegionArea=$('input[name="regionArea"]').val();
            var param={
                dateType:dateType,
                statisticsDate:year+"-"+month,
                deviceRegionProvince:deviceRegionProvince,
                deviceRegionCity:deviceRegionCity,
                deviceRegionArea:deviceRegionArea,
                taskType:pushTaskType
            }
            var layerIndex = layer.load(1);
            //获取统计数据
            common.sendAsyncRequest("/statistics/statisticsAdPush/statisticsAdPushAndExposure","POST",param,function(result){
                layer.close(layerIndex);
                var resultList=result.data;
                //格式化数据成图形需要的
                for(var a=0;a<=xAxisArra.length;a++){
                    for(var index in resultList){
                        var map=resultList[index];
                        var adExposureCount=map.adExposureCount;
                        var adPushCount=map.adPushCount;
                        if(dateType==1){
                            var day=map.day;
                            //图形x轴日期
                            var xDay=a+1;
                            if(xDay==day){
                                adExposureSeries[a]=adExposureCount;
                                adPushSumSeries[a]=adPushCount;
                                break;
                            }else{
                                adExposureSeries[a]=0;
                                adPushSumSeries[a]=0;
                            }
                        }else{
                            var month=map.month;
                            //图形x轴日期
                            var xMonth=a+1;
                            if(xMonth==month){
                                adExposureSeries[a]=adExposureCount;
                                adPushSumSeries[a]=adPushCount;
                                break;
                            }else{
                                adExposureSeries[a]=0;
                                adPushSumSeries[a]=0;
                            }
                        }
                    }
                }
                //重新加载统计图形
                myCharts3.setOption({
                    xAxis: {
                        data:xAxisArra
                    },
                    series: [{
                        name: "广告投放",
                        type: "line",
                        data: adExposureSeries
                    }, {
                        name: "广告条数",
                        type: "line",
                        data: adPushSumSeries
                    }]
                });
            });

        }
        //日期年份选择
        laydate.render({
            elem: '#laydate-range-year'
            ,type: 'year',
            value: new Date().getFullYear()
        });
          var  active = {
            query: function(){
                queryChartFu();
            },
            reset: function(){
                //清空条件
                var currentDate=new Date();
                $("#laydate-range-year").val(currentDate.getFullYear());
                $("#select_month").val("");
                $('input[name="regionProvince"]').val("");
                $('input[name="regionCity"]').val("");
                $('input[name="regionArea"]').val("");
                $("#region").val("");
                // 刷新select标签
                form.render('select');
                queryChartFu();
            },
            statisticsAdPushSum: function(){
              //统计页面头部数据
              common.sendAsyncRequest("/statistics/statisticsAdPush/statisticsAdPushTotal","POST",{"taskType":pushTaskType},function(result){
                  var resultMap=result.data;
                  if(resultMap){
                      //广告投放
                      var adExposureCount=resultMap['adExposureCount'];
                      if(adExposureCount){
                          $("#adExposureCount").text(adExposureCount);
                      }
                      //累计发布广告条数
                      var adPushCount=resultMap['adPushCount'];
                      if(adPushCount){
                          $("#adPushCount").text(adPushCount);
                      }
                      //当月广告投放
                      var adExposureCurrentMonthCount=resultMap['adExposureCurrentMonthCount'];
                      if(adExposureCurrentMonthCount){
                          $("#adExposureCurrentMonthCount").text(adExposureCurrentMonthCount);
                      }
                      //当月已发布广告条数
                      var adPushCurrentMonthCount=resultMap['adPushCurrentMonthCount'];
                      if(adPushCurrentMonthCount){
                          $("#adPushCurrentMonthCount").text(adPushCurrentMonthCount);
                      }
                  }
               });
              }
        };
        $(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('');
        }
    });
    //初始化
    var init=function(){
        queryChartFu();
        active.statisticsAdPushSum();
    }
    //初始化数据
    init();
 });
