(function(){
    X.pageLogic['userinfo'] = {
        init : function(parms){
            var me = this;

            var event_list,start_time,end_time,userdata;
           
            

            // 日期渲染
            X.daterender("#analtsis-condition-date",function(start, end, label){
                var startTime = start.format('YYYY-MM-DD')+ " "+ "00:00:00";
                var endTime = end.format('YYYY-MM-DD')+ " "+ "23:59:59";
                start_time = startTime;
                end_time = endTime;
                updatasolouser();
            });

            var time = $("#analtsis-condition-date").val();
            var timearr = time.split(" ");
            start_time = timearr[0] + " "+"00:00:00";
            end_time = timearr[2]+" "+"23:59:59";

            $(document).off('click','.userinfo-event-list-title-box').on('click','.userinfo-event-list-title-box',function(){

                if($(this).next().css('display') == 'block'){
                    $(this).next().hide();
                }else {
                    $(this).next().show(); 
                }
               
            });

            $(document).off('click','.touserinfo').on('click','.touserinfo',function(){

                X.gourl('userinfolist','conetnt');

            });
            
            X.api("ck/event_list","get",{},function(d){
                event_list = d;
                X.laytpldata("#userinfo-event-select-dot",d,".userinfo-event-select",function(){
                    $('.userinfo-event-select').ySelect(
                        {
                          placeholder: '全部事件',
                          searchText: '搜索',
                          showSearch: true,
                          numDisplayed: 2,
                          overflowText: '{n}项',
                          isCheck: false
                        }
                      );
                });
                if(X['DATA']['search-user-id']){
                    updatasolouser();
                }
            });

            $(document).off('change','.userinfo-event-select').on('change','.userinfo-event-select',function(){
                var Values = $('.userinfo-event-select').ySelectedValues(",");
                var Texts = $('.userinfo-event-select').ySelectedTexts(",");
                if(Values &&  Texts){
                    var Valuesarr = Values.split(',');
                    var Textsarr = Texts.split(',');
                    const eventarr = [];
                    for(let i in Valuesarr){
                        var arr = {
                            'event': Valuesarr[i],
                            'event_name': Textsarr[i]
                        }
                        eventarr.push(arr);
                    }
                    event_list = eventarr;
                    updatasolouser();
                }
                
                
            });
            
            $(document).off('click','.analtsis-condition').on('click','.analtsis-condition',function(){
                var type = $(".userinfo-pie-echarts").css("display");
                if(type == 'none'){
                    $(".analysis-echarts").css("width","calc( 100% - 401px)");
                    $(".userinfo-pie-echarts").show();
                }else {
                    $(".analysis-echarts").css("width","100%");
                    $(".userinfo-pie-echarts").hide();
                }

                if(userdata.event_count.date.length > 0){
                    userinfoechert(userdata.event_count);
                }
                if(userdata.proportion.length > 0){
                    userinfopieechart(userdata.proportion);
                }
            });

            function updatasolouser(){
                // 520_624fc7b902e7a090f2225612
                // X.DATA['search-user-id']
                X.api("ck/solo_user","post",{account_id: X.DATA['search-user-id'] ,event_list:event_list,start_time:start_time,end_time:end_time},function(val){
                    userdata = val;
                    if(val.event_count.date.length > 0){
                        userinfoechert(val.event_count);
                    }
                    if(val.proportion.length > 0){
                        userinfopieechart(val.proportion);
                    }
                    X.laytpldata("#user-left-content-box-dot",val['details_data'],".user-left-content-box",function(){});
                    if(JSON.stringify(val['details_user']) == "{}"){
                        $(".userinfo-event-box").html('<p style="text-align: center; color: #42546d;">已显示全部数据</p>');
                    }else {
                        X.laytpldata("#userinfo-event-box-dot",val['details_user'],".userinfo-event-box",function(){});
                    }
                    
                })
            };

            function userinfopieechart(data){

                var myChart = echarts.init(document.getElementById('userinfo-pie'));

                var option = {
                    color: ['#4daef5','#5dd7d1','#bae5f6','#f8e7bb','#ad91d9','#7be0b8','#f78499','#fa9c6d','#fecde2','#677a9b','#4295da'],
                    title: {
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    label:{
                        formatter: '{b}\n{d}%',
                    },
                    series: [
                        {
                            name: '姓名',
                            type: 'pie',
                            radius: '50%',
                            center: ['50%', '50%'],
                            data: data,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                myChart.setOption(option,true);
            };

            function userinfoechert(data){
                var xAxisData = data.date;
                var seriesData = [];
                var arr = {
                    name:'总量',
                    type: 'bar',
                    barWidth : 30,
                    data: data.event_values,
                }
                seriesData.push(arr);

                var myChart = echarts.init(document.getElementById('userinfo-echarts'));
               
                var option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    color: X.DATA.echartscolor,
                    grid: {
                        left: '2%',
                        right: '2%',
                        bottom: '2%',
                        top: '10%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            axisTick: {show: false},
                            data: xAxisData
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: seriesData
                };
                myChart.resize();
                myChart.setOption(option,true);
            }
            

        }
    };
})();
