(function(){
    X.pageLogic['setupbaobiao'] = {
        init : function(parms){
            var me = this;
            this.parms = parms;
            this.callback = parms.callback;
            var data = parms.extData;
            // console.log('luo',data);
            var report_id = [];
            report_id.push(data.id);
            var postdata;//上传数据
            X.api('report/read_report','post',{project_id:X.DATA.projectid,report_id:report_id},function(d){
                postdata = d;
                $('.setupbaobiao-title').val(d[0]['name']);
                $('.setupbaobiao-textarea').val(d[0]['desc']);
                // 初始化时间
                if(endTime && startTime){
                    var endTime = d[0]['query']['eventView']['endTime'];
                    var startTime = d[0]['query']['eventView']['startTime'];
                }else {
                    $(".setupbaobiao-date-box").hide();
                }
                
                $(".setupbaobiao-time").html(startTime +' - '+ endTime);
                if(data.modeltype == "table"){ //初始化图表类型
                    $("#setupbaobiao-table").addClass("icon___XxbtJ");
                }else {
                    $("#setupbaobiao-echarts").addClass("icon___XxbtJ");
                }
                //初始化窗体大小
                if(data.modelsize == 'in'){ //中图
                    $("#setupbaobiao-in").prop("checked",true);
                }else if( data.modelsize == 'small' ){//小图
                    $("#setupbaobiao-small").prop("checked",true);
                }else{//大图
                    $("#setupbaobiao-large").prop("checked",true);
                }
                //初始化xy轴切换
                if(data.modelswitch && data.modelswitch != "undefined" && data.modelswitch == "true"){
                    $("#kxyswitch").prop("checked",true);
                }else {
                    $("#gxyswitch").prop("checked",true);
                }
                //初始化倒序
                if(data.ascending && data.ascending != undefined && data.ascending == "true"){
                    $("#kqreverseorder").prop("checked",true);
                }else {
                    $("#gbreverseorder").prop("checked",true);
                }

                if(data.daydata && data.daydata != undefined && data.daydata == "false"){
                    $('#daydatafalse').prop('checked',true);
                }else {
                    $('#daydatatrue').prop('checked',true);
                }
                if(data.avesumdata && data.avesumdata != undefined && data.avesumdata == "false"){
                    $('#avesumdatafalse').prop('checked',true);
                }else {
                    $('#avesumdatatrue').prop('checked',true);
                }
 
                X.laytpldata("#setupbaobiao-select-dot",X['DATA']['screenlist'][d[0]['cat']],".setupbaobiao-select");
            });

            X.daterender(".setupbaobiao-rli-box2",function(start, end, label){
                var startTime = start.format('YYYY-MM-DD')+ " "+ "00:00:00";
                var endTime = end.format('YYYY-MM-DD')+ " "+ "23:59:59";
                postdata[0]['query']['eventView']['startTime'] = startTime;
                postdata[0]['query']['eventView']['endTime'] = endTime;
                $(".setupbaobiao-time").html(startTime +' - '+ endTime);

                var sameday = Date.parse(new Date()) / 1000; //当天时间戳
                
                var enddate = new Date(end.format('YYYY-MM-DD'));
                var endmonth = Date.parse(enddate)/1000;

                var startdate = new Date(start.format('YYYY-MM-DD'));
                var startmonth = Date.parse(startdate)/1000;
                postdata[0]['query']['e_days'] = parseInt((sameday - endmonth)/86400);
                postdata[0]['query']['s_days'] = parseInt((sameday - startmonth)/86400);
            });
            
            // 分析时间粒度
            $(document).off('click','.setupbaobiao-select').on('change','.setupbaobiao-select',function(){
                postdata[0]['query']['eventView']['timeParticleSize'] = $(this).val();
            });

            $(document).off('click','.shape___2nUUi').on('click','.shape___2nUUi',function(){
                $('.shape___2nUUi').removeClass('icon___XxbtJ');
                $(this).addClass('icon___XxbtJ');
            });

            $(document).off('click','.setupbaobiao-xq').on('click','.setupbaobiao-xq',function(){
                layer.closeAll();
            });

            $(document).off('click','.setupbaobiao-yy').on('click','.setupbaobiao-yy',function(){
                var title = $('.setupbaobiao-title').val();
                var desc = $(".setupbaobiao-textarea").val();
                var modeltype = $(".icon___XxbtJ").attr('data-type');
                var modelsize = $('input:radio[name="modelsize"]:checked').val();
                var modelswitch = $('input:radio[name="xyswitch"]:checked').val();
                var ascending = $('input:radio[name="reverseorder"]:checked').val();
                var avesumdata = $('input:radio[name="avesumdata"]:checked').val();
                var daydata = $('input:radio[name="daydata"]:checked').val();
                if(modelswitch == "true"){ modelswitch = true;}else {modelswitch = false;};
                if(ascending == 'true'){ ascending = true }else { ascending = false };
                if(avesumdata == "true"){ avesumdata = true;}else {avesumdata = false;};
                if(daydata == 'true'){ daydata = true }else { daydata = false };
                var arr = {
                    _id:X.DATA.dashboard_id,
                    report:{
                        name: title,
                        report_id:postdata[0]['_id'],
                        desc:desc,
                        model:postdata[0]['cat'],
                        graph_type:modeltype,
                        graph_size:modelsize,
                        modelswitch: modelswitch,
                        ascending:ascending,
                        avesumdata:avesumdata,
                        daydata:daydata
                    }
                }
                console.log(arr);
                X.api('dashboard/edit_report','post',arr,function(d){
                    console.log(d);
                    var reportedit = {
                        name: title,
                        desc:desc,
                        query:postdata[0]['query'],
                        report_id:postdata[0]['_id']
                    }
                    X.api('report/edit','post',reportedit,function(d){
                        me.callback && me.callback(d);
                        layer.closeAll();
                    })    
                })
                
            });

        }
    };
})();