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 = {
        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)',
        },
        calculable: !0,
        grid: {
            left: '0',
            right: '0',
            top:"30",
            containLabel: true
        },
        legend: {
            data: ["观看次数", "住户观看次数","陌生人观看次数"],
            icon: "rect",
            itemWidth: 15,
            itemHeight: 4,
            x: 'center', // 'center' | 'left' | {number},

            y: '93%', // 'center' | 'bottom' | {number}
        },
        xAxis: [{
            type: "category",
            axisLine: {
                lineStyle: {
                    color: '#5a5a5a'
                }
            },
            axisPointer: {//指示线

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

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

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

                },
                show:true //隐藏或显示
            }
        }],
        series: [{
            name: "观看次数",
            type: "line",
            data:[],
            itemStyle: {
                normal: { color: "#1f93ff" },
            }
        }, {
            name: "住户观看次数",
            type: "line",
            data:[],
            itemStyle: {
                normal: { color: "#FA6332" },
            }
        }, {
            name: "陌生人观看次数",
            type: "line",
            data:[],
            itemStyle: {
                normal: { color: "#2fc25b" },
            }
        }]
    };
    // 窗口大小改变事件
    window.onresize = function () {
        myCharts3.resize();
    };
    myCharts3.setOption(option3);
    //查询
    var queryChartFu=function(){
        //地区
        var province=$('input[name="regionProvince"]').val();
        var city=$('input[name="regionCity"]').val();
        var area=$('input[name="regionArea"]').val();
        //广告类别
        var selectedAdTypeId = $('#selectedAdTypeId').val();
        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 adWatchCountSeries=new Array();
        //住户观看次数
        var adWatchUserCountSeries=new Array();
        //陌生人观看次数
        var adWatchVisitorCountSeries=new Array();
        var watchDate=year+"-"+month+"-1 00:00:00";
        //参数
        var dataParam={
            dateType:dateType,
            watchDate:watchDate,
            regionProvince:province,
            regionCity:city,
            regionArea:area,
            adTypeId:selectedAdTypeId
        }
        var layerIndex = layer.load(1);
         common.sendAsyncRequest("/statistics/statisticsAdWatch/statisticsAdWatchForChart","POST",dataParam,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 adWatchCount=Number(map['adWatchCount']);
                     var adWatchUserCount=Number(map['adWatchUserCount']);
                     if(dateType==1){
                         var day=map.day;
                         //图形x轴日期
                         var xDay=a+1;
                         if(xDay==day){
                             adWatchCountSeries[a]=adWatchCount;
                             adWatchUserCountSeries[a]=adWatchUserCount;
                             adWatchVisitorCountSeries[a]=adWatchCount-adWatchUserCount;
                             break;
                         }else{
                             adWatchCountSeries[a]=0;
                             adWatchUserCountSeries[a]=0;
                             adWatchVisitorCountSeries[a]=0;
                         }
                     }else{
                         var month=map.month;
                         //图形x轴日期
                         var xMonth=a+1;
                         if(xMonth==month){
                             adWatchCountSeries[a]=adWatchCount;
                             adWatchUserCountSeries[a]=adWatchUserCount;
                             adWatchVisitorCountSeries[a]=adWatchCount-adWatchUserCount;
                             break;
                         }else{
                             adWatchCountSeries[a]=0;
                             adWatchUserCountSeries[a]=0;
                             adWatchVisitorCountSeries[a]=0;
                         }
                     }

                 }
             }
             //重新加载统计图形
             myCharts3.setOption({
                 xAxis: {
                     data:xAxisArra
                 },
                 series: [{
                     name: "观看次数",
                     type: "line",
                     data: adWatchCountSeries
                 }, {
                     name: "住户观看次数",
                     type: "line",
                     data: adWatchUserCountSeries
                 }, {
                     name: "陌生人观看次数",
                     type: "line",
                     data: adWatchVisitorCountSeries
                 }]
             });

         });


    }
    //日期年份选择
    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());
            $("#select_month").val("");
            $('#selectedAdTypeId').val("");
            $('input[name="regionProvince"]').val("");
            $('input[name="regionCity"]').val("");
            $('input[name="regionArea"]').val("");
            $("#region").val("");
            // 刷新select标签
            form.render('select');
            queryChartFu();
        },
        statisticsAdWatchTotal:function () {
            //统计 总观看次数 住户观看次数 访客观看次数
            common.sendAsyncRequest("/statistics/statisticsAdWatch/statisticsAdWatchTotal","POST",null,function(result) {
                var resultMap=result.data;
                if(resultMap){
                    //总观看次数
                    var adWatchTotalCount=resultMap['adWatchTotalCount'];
                    if(adWatchTotalCount){
                        $("#adWatchTotalCount").text(adWatchTotalCount);
                    }
                    //总住户观看次数
                    var adWatchTotalUserCount=resultMap['adWatchTotalUserCount'];
                    if(adWatchTotalUserCount){
                        $("#adWatchTotalUserCount").text(adWatchTotalUserCount);
                    }
                    // 总陌生人观看次数
                    var adWatchTotalVisitorCount=resultMap['adWatchTotalVisitorCount'];
                    if(adWatchTotalVisitorCount){
                        $("#adWatchTotalVisitorCount").text(adWatchTotalVisitorCount);
                    }
                }
            });
        }
    };
    $(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('');
        }
    });
    //获取根机构下面所有广告类型Adtype
    common.sendAsyncRequest("/ad/type/getAdTypeList","GET",{},function(result) {
        var $select = $('#selectedAdTypeId');
        result.list.forEach(function(item){
            $select.append('<option value="' + item.id + '">' + item.name+ '</option>')
        });
        // 刷新select标签
        form.render('select');
    });
    //初始化
    var init=function(){
        queryChartFu();
        active.statisticsAdWatchTotal();
    }
    //初始化数据
    init();
});

