(function(){
    X.pageLogic['screen'] = {
        init : function(parms){
            var width = window.innerWidth - 270;
            $(".screen-box").css("width",width+"px");
            var me = this;
            this.parms = parms;
            this.callback = parms.callback; //选择后执行的回调
            var reportid = parms.extData;//获取到上层弹窗传的数据

            //默认开始时有一个数据
            var data = {
                'filts':[],
                'relation':'and'
            };
            if(reportid){
                data = reportid;
                if(reportid.relation == 'and'){
                    $('.screen-left-qei').html("且");
                }else {
                    $('.screen-left-qei').html("或");
                }
                if(reportid['filts'].length > 1){
                    $('.screen-left-qei').show();
                }
                X.laytpldata("#screen-con-box-dot",reportid['filts'],".screen-con-box",function(){
                    renderdate();
                });
            }
            
            var my_event,load_filter_props,filter_map;
            X.api("data_auth/my_event","get",{},function(d){
                my_event = d;
                X.api("data_auth/load_filter_props","post",{event_name:my_event[0]['category'][0]['event_name']},function(val){
                    load_filter_props = val;
                    X.api("data_auth/filter_map","get",{},function(d){
                        filter_map = d;
                        if(!reportid){
                            updatascreencon();
                        }
                    })
                })
            });

            // 显示隐藏添加条件按钮
            $(document).off('click','.screen-right-box .img').on('click','.screen-right-box .img',function(){
                if($(".screen-buttom-box").css('display') == 'block'){
                    $(".screen-buttom-box").hide();
                    $(".screen-left-qei").css('bottom','0px');
                    $(".screen-right-box .img").css('transform','rotate(180deg)');
                }else {
                    $(".screen-buttom-box").show();
                    $(".screen-left-qei").css('bottom','24px');
                    $(".screen-right-box .img").css('transform','rotate(360deg)');
                }
            });

            //清空按钮
            $(document).off('click','.screen-right-box .qingkong').on('click','.screen-right-box .qingkong',function(){
                layer.closeAll();
            });

            $(document).off('click','.screen-left-box .screen-left-qei').on('click','.screen-left-box .screen-left-qei',function(){
                if($(this).html() == "且"){
                    $(this).html("或");
                    data['relation'] = 'or';
                }else {
                    $(this).html("且");
                    data['relation'] = 'and';
                }
            });
           
            function updatascreencon(){
                var drr={
                    "columnName":load_filter_props[0]['category'][0]['id'],//事件id
                    "columnDesc":load_filter_props[0]['category'][0]['title'],//事件中文显示
                    'data_type':load_filter_props[0]['category'][0]['data_type'],//类型
                    'comparator_name':'等于',//默认等于
                    "comparator":'==',//符号id
                    "ftv":[],//条件
                    "strftv":'',//显示条件
                    "section":[-1,1],//区间
                    "tableType":load_filter_props[0]['id']
                };

                data['filts'].push(drr);

                X.laytpldata("#screen-con-box-dot",data['filts'],".screen-con-box",function(){
                    renderdate();
                });
                if(data['filts'].length >= 2){
                    $(".screen-left-qei").show();
                }else {
                    $(".screen-left-qei").hide();
                }
            };
            
            //条件添加按钮
            $(document).off('click','.screen-buttom-box .screen-buttom').on('click','.screen-buttom-box .screen-buttom',function(){
                updatascreencon();
            });

            X.daterender("#time-box-timechoice1");

            var render_date_index;//且用户符合时间选择框的index
            $(document).off('click','.right-date-box').on("click",'.right-date-box',function(){
                var index = $(this).attr("data-index");
                render_date_index = index;
            });
            
            function renderdate(){
                lay('.right-date-box').each(function(){
                    layui.laydate.render({
                      elem: this
                      ,type:"datetime"
                      ,trigger: 'click'
                      ,done: function(value, date, endDate){
                        data['filts'][render_date_index]['strftv'] = value;
                        var datearr = [];
                        datearr.push(value)
                        data['filts'][render_date_index]['ftv'] = datearr;
                      }
                    });
                });
            }

            $(document).off('click','.screen-con-bg .left-type').on('click','.screen-con-bg .left-type',function(){
                var obj = $(this); 
                var offset = obj.offset();
                var index = obj.attr('data-index');
                //点击按钮，数据，向左偏移，向右偏移
                X.querycriteriapop(load_filter_props,'category',offset.left,offset.top+obj.height(),function(val){
                   obj.html(val.title);
                   data['filts'][index]['columnDesc'] = val.title;
                   data['filts'][index]['columnName'] = val.id;
                   data['filts'][index]['data_type'] = val.type;     
                   data['filts'][index]['tableType'] = val.table_type; 
                   X.laytpldata("#screen-con-box-dot",data['filts'],".screen-con-box",function(){
                       renderdate();
                   });
                });
            });

            //下拉选项触发事件
            $(document).off('click','.screen-content-sel').on('click','.screen-content-sel',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr('data-index');
                var type =obj.attr('data-type');

                X.parametersopen(filter_map[type],'dropdownlist',[offset.top+obj.height(),offset.left],function(val){
                    // console.log(val);
                    obj.html(val.title);
                    data['filts'][index]['comparator_name'] = val.title;
                    data['filts'][index]['comparator'] = val.id;
                    if(val.id == "range"){
                        data['filts'][index]['ftv'] = [-1,1];
                    }
                    X.laytpldata("#screen-con-box-dot",data['filts'],".screen-con-box",function(){
                        renderdate();
                    });
                })
            });

            $(document).off("click",'.right-box').on("click",'.right-box',function(){
                var obj = $(this);
                var index = obj.attr("data-index");
                var comparator = obj.attr("data-comparator");//选择多选时才能多选
                var offset = obj.offset();
                var columnName = data['filts'][index]['columnName'];
                X.api("data_mana/select_attr","post",{attr_name:columnName},function(val){
                    var darr = {
                        comparator:comparator,
                        data: val['map_']
                    }
                    if(val != "查无数据"){
                        X.parametersopen(darr,'dropdownlist3',[offset.top+obj.height(),offset.left],function(val){
                            if(comparator == "in"){
                                if(data['filts'][index]['strftv'] == "" ){
                                    var strftv = val.title;
                                    var ftv = val.id;
                                }else {
                                    var strftv =  val.title;
                                    var ftv = val.id;
                                }
                                data['filts'][index]['strftv'] = strftv;
                                console.log(ftv);
                                var valarr = ftv.split(",");
                                data['filts'][index]['ftv']=valarr;
                               
                            }else {
                                data['filts'][index]['strftv'] = val.title;
                                var arr = [];
                                arr.push(val.id);
                                data['filts'][index]['ftv'] = arr; 
                            }

                            X.laytpldata("#screen-con-box-dot",data['filts'],".screen-con-box",function(){
                                renderdate();
                            });
                        })
                    }
                })
                // console.log(data['filts'][index]['columnName'])
            });

            //文本框条件输入触发事件
            $(document).off('click','.screen-right-txt').on('change','.screen-right-txt',function(){
                var index = $(this).attr('data-index');
                var val = $(this).val();
                var valarr = val.split(",");
                data['filts'][index]['ftv']=valarr;
                data['filts'][index]['strftv']=val;
            });

            //删除按钮
            $(document).off('click','.screen-con-bg .screen-del').on('click','.screen-con-bg .screen-del',function(){
                var index = $(this).attr('data-index');
                if(data['filts'].length > 1){
                    data['filts'].splice(index, 1);
                    X.laytpldata("#screen-con-box-dot",data['filts'],".screen-con-box",function(){
                        renderdate();
                    });
                    if(data['filts'].length < 2){
                        $(".screen-left-qei").hide();
                    }
                }else {
                    layer.closeAll();
                    $(".screen-left-qei").hide();
                }
            });

            // 计算按钮
            $(document).off('click','.screen-right-box .jisuan').on('click','.screen-right-box .jisuan',function(){
               
                // 刷新看板里的数据暂时没有
                me.callback && me.callback(data);
            });

        }
    };
})();
