(function(){
    X.pageLogic['userlabel_dy'] = {
        init : function(parms){
            var me = this;
            var _id = X.DATA.userlabelid;

            var data;
            if(_id && _id != ""){
                X.api("user_label/detail","post",{label_id: _id},function(d){
                    data = d;
                    $(".lablename").val(d.cluster_name);
                    $(".labledesc").val(d.display_name);
                    $(".labeldes").val(d.remarks);
                    updatalable();
                })
            }else {
                data = {
                    "project_id": X.DATA.projectid,
                    "cluster_name": "",
                    "display_name": "",
                    "cluster_type":'user_custom',
                    "qp": {
                        "tagRemark": "",
                        "tagValue": "",
                        "user_cluster_def": {
                            'event_relation':'and',
                            "event_user_relation":"and",
                            "events": [
                                
                            ],
                            "filts": [],
                            "relation": "and"
                        },
                    },
                    "remarks": ""
                };
            }


            // 标签值定义
            var choicelabellist=[
                {'title':'做过（没做过事件）',id:'0'},
                {'title':'用户属性满足',id:'1'}
            ];
            X.laydropdown('.choicelabel',choicelabellist,function(e){
                console.log(e);
                var arr = {};
                if(e.id == 0){
                    arr= {
                        endTime: "",
                        eventDesc: dataArr[0]['category'][0].event_desc,
                        event_name: dataArr[0]['category'][0].event_name,
                        eventType: "event",
                        filts: [],
                        num: "",
                        e_days:0,
                        s_days:7,
                        relation: "and",
                        startTime: "",
                        uce_calcu_symbol: "==",
                        uce_calcu_symbolDesc: "等于",
                        prop_quota: {analysis: '', analysisDesc: '', quota: retdata[0]['id'], quotaDesc: retdata[0]["title"],quota_type: null},
                        is_touch: true,
                        is_time: false
                    }
                    data['qp']['user_cluster_def']['events'].push(arr);
                }else {
                    arr = {
                        "columnDesc":defaultgroupdata[0]['category'][0]['title'],//事件中文显示 columnDesc
                        "columnName":defaultgroupdata[0]['category'][0]['id'],//事件id
                        'data_type':defaultgroupdata[0]['category'][0]['data_type'],//类型
                        'comparator_name':'等于',//默认等于
                        "comparator":'==',//符号id
                        "ftv":[],//条件
                        "strftv":'',//显示条件
                        "section":[-1,1],//区间
                        "tableType":defaultgroupdata[0]['id'],
                        "filts": []
                    }
                    data['qp']['user_cluster_def']['filts'].push(arr);
                }
                
                updatalable();
            });

            $(document).off('click','.userlabel-dy-head-userlabel').on('click','.userlabel-dy-head-userlabel',function(){
                X.gourl('userlabel','conetnt');
            });

            $(document).off("click",'.ta-isDone').on("click",'.ta-isDone',function(){
                var index = $(this).attr("data-index");
                data['qp']['user_cluster_def']['events'][index]['is_touch'] = !data['qp']['user_cluster_def']['events'][index]['is_touch'];
                updatalable();
            });

            var dataArr = [],retdata = [],props=[],defaultgroupdata=[],filter_map=[],quotas_map=[];

            X.api("data_auth/my_event","get",{},function(d){
            
                dataArr = d;
                X.api("data_auth/load_prop_quotas","post",{event_name:dataArr[0]['category'][0]['event_name']},function(d){
                    retdata = d.staid_quots.concat(d.props);
                    props = d.props;
         
                    X.api("data_auth/load_filter_props","post",{event_name:dataArr[0]['category'][0]['event_name']},function(val){
                        defaultgroupdata = val;

                        X.api("data_auth/filter_map","get",{},function(d){
                            filter_map = d;

                            X.api('data_auth/quotas_map',"get",{},function(d){
                                quotas_map = d;
                                
                            })

                        })

                    })
                })
            });

            $(document).off("click",'.my-event-data').on('click','.my-event-data',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr('data-index');
                X.querycriteriapop(dataArr,'setgrouping',offset.left,offset.top+obj.height(),function(val){
                    
                    data['qp']['user_cluster_def']['events'][index]['eventDesc'] = val.id;
                    data['qp']['user_cluster_def']['events'][index]['event_name'] = val.name;
                    updatalable();
                })
            });

            var label_time_index; //点击修改日期的index;
            $(document).off('click','.time-days-box').on("click",'.time-days-box',function(){
                var index = $(this).attr("data-index"); 
                label_time_index = index;
            });

            function updatalable(){
                X.laytpldata('#userlabel-json-box-dot',data['qp']['user_cluster_def'],'.userlabel-json-box');

                X.daterender(".time-days-box",function(start, end, label){
                    var startTime = start.format('YYYY-MM-DD')+ " "+ "00:00:00";
                    var endTime = end.format('YYYY-MM-DD')+ " "+ "23:59:59";
                    data['qp']['user_cluster_def']['events'][label_time_index]['startTime'] = startTime;
                    data['qp']['user_cluster_def']['events'][label_time_index]['endTime'] = 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;
                    data['qp']['user_cluster_def']['events'][label_time_index]['e_days'] = parseInt((sameday - endmonth)/86400);
                    data['qp']['user_cluster_def']['events'][label_time_index]['s_days'] = parseInt((sameday - startmonth)/86400);
                    updatalable();
                })
            };
            
            $(document).off("click",'.load_prop_quotas_data').on('click','.load_prop_quotas_data',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr('data-index')
                X.parametersopen(retdata,'dropdownlist',[offset.top+obj.height(),offset.left],function(val){
                    
                    data['qp']['user_cluster_def']['events'][index]['prop_quota']['quotaDesc'] = val.title;
                    data['qp']['user_cluster_def']['events'][index]['prop_quota']['quota_type'] = val.data_type;
                    if(val.data_type == null){
                        data['qp']['user_cluster_def']['events'][index]['prop_quota']['quota'] = val.analysis;
                    }else {
                        data['qp']['user_cluster_def']['events'][index]['prop_quota']['quota'] = val.id;
                    }
                    updatalable();
                });
            });

            $(document).off("click",'.analysis-data-box').on('click','.analysis-data-box',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr('data-index');
                var type = obj.attr('data-type');
                X.parametersopen(quotas_map[type],'dropdownlist',[offset.top+obj.height(),offset.left],function(val){
                    
                    data['qp']['user_cluster_def']['events'][index]['prop_quota']['analysisDesc'] = val.title;
                    data['qp']['user_cluster_def']['events'][index]['prop_quota']['analysis'] = val.id;
                    updatalable();
                })
            });

            $(document).off('click','.uce_calcu_symbol_box').on('click','.uce_calcu_symbol_box',function(){
                var obj = $(this);
                var index = obj.attr("data-index");
                var offset = obj.offset();
                X.parametersopen(X['DATA']['search']['condition2'],'dropdownlist',[offset.top+obj.height(),offset.left],function(val){
                    
                    data['qp']['user_cluster_def']['events'][index]['uce_calcu_symbolDesc'] = val.title;
                    data['qp']['user_cluster_def']['events'][index]['uce_calcu_symbol'] = val.id;
                    updatalable();
                })
            });

            $(document).off("change",'.lable-input-box input').on('change','.lable-input-box input',function(){
                var obj = $(this);
                var index = obj.attr("data-index");
                var text = obj.val();
                data['qp']['user_cluster_def']['events'][index]['num'] = text;
            });

            $(document).off('click','.action-icon-events-del').on('click','.action-icon-events-del',function(){
                var obj = $(this);
                var index = obj.attr("data-index");
                data['qp']['user_cluster_def']['events'].splice(index,1);
                updatalable();
            });

            $(document).off("click",".action-icon-time").on('click','.action-icon-time',function(){
                var index = $(this).attr("data-index");
                data['qp']['user_cluster_def']['events'][index]['is_time'] = !data['qp']['user_cluster_def']['events'][index]['is_time'];
                updatalable();
            });

            $(document).off("click",".ta-property-select").on("click",".ta-property-select",function(){
                var obj = $(this);
                


            });

            $(document).off("click",".user-attribute").on("click",".user-attribute",function(){
                var obj =$(this);
                var offset = obj.offset();
                var index = obj.attr("data-index");
                X.querycriteriapop(defaultgroupdata,'category',offset.left,offset.top+obj.height(),function(val){
                    console.log(val);
                    $(this).html(val.title);
                    data['qp']['user_cluster_def']['filts'][index]['columnName'] = val.id;
                    data['qp']['user_cluster_def']['filts'][index]['columnDesc'] = val.title;
                    data['qp']['user_cluster_def']['filts'][index]['data_type'] = val.type;
                    data['qp']['user_cluster_def']['filts'][index]['tableType'] = val.table_type;
                    updatalable();
                })
            });

            $(document).off("click",".user-attribute-filter").on("click",".user-attribute-filter",function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr("data-index");
                var type = data['qp']['user_cluster_def']['filts'][index]['data_type'];
                console.log()
                X.parametersopen(filter_map[type],'dropdownlist',[offset.top+obj.height(),offset.left],function(val){
                    $(this).html(val.title);
                    data['qp']['user_cluster_def']['filts'][index]['comparator_name'] = val.title;
                    data['qp']['user_cluster_def']['filts'][index]['comparator'] = val.id;
                    if(val.id == "range"){
                        data['qp']['user_cluster_def']['filts'][index]['ftv'] = [-1,1];
                    }
                    updatalable();
                })
            });

            $(document).off('click','.userattrfilts').on('click','.userattrfilts',function(){
                var obj = $(this);
                var index = obj.attr("data-index");
                var type = obj.attr("data-type");
                var arr = {
                    "columnDesc":defaultgroupdata[0]['category'][0]['title'],//事件中文显示 columnDesc
                    "columnName":defaultgroupdata[0]['category'][0]['id'],//事件id
                    'data_type':defaultgroupdata[0]['category'][0]['data_type'],//类型
                    'comparator_name':'等于',//默认等于
                    "comparator":'==',//符号id
                    "ftv":[],//条件
                    "strftv":'',//显示条件
                    "section":[-1,1],//区间
                    "tableType":defaultgroupdata[0]['id']
                }
                if(type == 'userattr'){
                    data['qp']['user_cluster_def']['filts'][index]['filts'].push(arr);
                }else {
                    data['qp']['user_cluster_def']['events'][index]['filts'].push(arr);
                }
                console.log(data);
                X.laytpldata('#userlabel-json-box-dot',data['qp']['user_cluster_def'],'.userlabel-json-box');
            });

            $(document).off("click",".taDragDisable").on("click",".taDragDisable",function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr("data-index");
                var columnName = data['qp']['user_cluster_def']['filts'][index]['columnName']

                X.api("data_mana/select_attr","post",{attr_name:columnName},function(val){
                    if(val != "查无数据"){
                        
                        X.parametersopen(val['map_'],'dropdownlist',[offset.top+obj.height(),offset.left],function(val){
                        
                            data['qp']['user_cluster_def']['filts'][index]['strftv'] = val.title;
                            var arr = [];
                            arr.push(val.id);
                            data['qp']['user_cluster_def']['filts'][index]['ftv'] = arr; 
                            updatalable();
                            
                        })
                    }
                })
            });

            $(document).off("click",'.userlabel-dy-baocun').on("click",".userlabel-dy-baocun",function(){
                var lablename = $(".lablename").val();
                var labledesc = $(".labledesc").val();
                var labeldes = $(".labeldes").val();
                if(lablename == ""){
                    layer.msg("请填写标签名");
                    return;
                }
                if(labledesc == ""){
                    layer.msg("请填写显示名");
                    return;
                }
                data['cluster_name'] = lablename;
                data['display_name'] = labledesc;
                data['remarks'] = labeldes;

                X.api('user_label/save','post',data,function(d){
                    layer.msg("添加成功");
                })
            });
        }
    };
})();