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

            X.api("project/","get",{},function(d){
                X.laytpldata('#select-datacheck-list-box-dot',d,'.select-datacheck-list-box',function(){
                    layui.form.render();
                });
            })
            
            // 事件属性添加
            var eventarr = [];
            $(document).off('click','.eventattr-add').on('click','.eventattr-add',function(){
                var arr = {
                    'attrname':'',
                    'v':''
                }
                eventarr.push(arr);
                X.laytpldata("#eventattr-box-dot",eventarr,"#datacheck-eventattr-box");
                layui.form.render();
            });

            // 事件属性删除
            $(document).off('click','.attrname-del').on('click','.attrname-del',function(){
                var index = $(this).attr('data-index');
                eventarr.splice(index,1);
                X.laytpldata("#eventattr-box-dot",eventarr,"#datacheck-eventattr-box");
                layui.form.render();
            });
            
            $(document).off('click','.eventattr-input').on('blur','.eventattr-input',function(){
                var index = $(this).attr('data-index');
                if(eventarr[index] == undefined){
                    var arr = {
                        'attrname':'',
                        'v':''
                    }
                    eventarr.push(arr);
                }
                eventarr[index]['attrname'] = $(this).val();
            });

            layui.form.on('select(eventattr_filter)',function(data){
                var value = data.value;
                var valarr = value.split('_');
                var index = valarr[1];
                var val = valarr[0];
                eventarr[index]['v'] = val;
            });

            $(document).off('click','.eventattr-copy').on('click','.eventattr-copy',function(){
                var txt = $(".datacheck-copy-txt-box").val();
                var valarr = txt.split(/[(\r\n)\r\n]+/);
                // console.log(valarr);
                var dataDarr = [];
                for(let i in valarr){
                    var arr = valarr[i].split("\t");
                    // console.log(arr);
                    dataDarr.push(arr);
                }
                var eventdata = [];
                for(let i in dataDarr){
                    if(dataDarr[i][1]){
                        var arr = {
                            'attrname': dataDarr[i][1],
                            'v': fieidmapping(dataDarr[i][2])
                        }
                        eventdata.push(arr);
                    }
                }
                eventarr = eventdata;
                layui.form.val('datacheckexample',{
                    'eventname':dataDarr[0][0]
                });
                // console.log(eventarr);
                // return;
                X.laytpldata("#eventattr-box-dot",eventarr,"#datacheck-eventattr-box");
                layui.form.render();
            });

            // 字段映射
            function fieidmapping(d){
                if(d == 'int'){
                   return 'integer';
                }else if(d == 'str'){
                    return 'string';
                }else if(d == 'date'){
                    return 'date';
                }else if(d == 'json'){
                    return 'json'
                }else {
                    return 'array'
                }
            }

            // 其他条件添加
            var conditionarr = [];
            $(document).off('click','.condition-add').on('click','.condition-add',function(){
                var arr = {
                    'conditionname':'',
                    'v':''
                }
                conditionarr.push(arr);
                X.laytpldata("#condition-box-dot",conditionarr,"#datacheck-condition-box");
                layui.form.render();
            });
            // 其他条件删除
            $(document).off('click','.condition-del').on("click",'.condition-del',function(){
                var index = $(this).attr('data-index');
                conditionarr.splice(index,1);
                X.laytpldata("#condition-box-dot",conditionarr,"#datacheck-condition-box");
                layui.form.render();
            });

            $(document).off('click','.condition-input').on('blur','.condition-input',function(){
                var index = $(this).attr('data-index');
                if(conditionarr[index] == undefined){
                    var arr = {
                        'conditionname':'',
                        'v':''
                    }
                    conditionarr.push(arr);
                }
                conditionarr[index]['conditionname'] = $(this).val();
            });

            $(document).off('click','.condition-v-input').on('blur','.condition-v-input',function(){
                var index = $(this).attr('data-index');
                if(conditionarr[index] == undefined){
                    var arr = {
                        'conditionname':'',
                        'v':''
                    }
                    conditionarr.push(arr);
                }
                conditionarr[index]['v'] = $(this).val();
            });

            // 默认属性
            var baseeventdata = {};
            $(document).off('click','.baseevent').on('click','.baseevent',function(){
                X.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    offset: 'auto',
                    anim: 5,
                    skin: 'yourclass',
                    url: 'datacheck_moren',
                    data:baseeventdata,
                    callback : function(d){
                        baseeventdata = d;
                    }
                })
            });

            //默认全选默认属性
            X.api('check_data/default_field','get',{},function(d){
                baseeventdata = d;
            });
            
            $(document).off('click','.datacheck-but-bc').on('click','.datacheck-but-bc',function(){
                var fromdata = layui.form.val('datacheckexample');
                var is_unique;
                // console.log(fromdata);
                if(fromdata.only == "true"){
                    is_unique = true
                }else {
                    is_unique = false
                }
        
                var props={},where={};
                for(let i in eventarr){
                    props[eventarr[i]['attrname']] = eventarr[i]['v']
                }
                for(let i in conditionarr){
                    where[conditionarr[i]['conditionname']] = conditionarr[i]['v']
                }
        
                var data={
                    'game': fromdata.game,
                    'db_name':fromdata.db_name,
                    'event_name':fromdata.eventname,
                    'is_unique':is_unique,
                    'props':props,
                    'where':where,
                    'default_field':baseeventdata,
                    'modeltitle': modeltitle
                };
                X.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    offset: 'auto',
                    anim: 5,
                    skin: 'yourclass',
                    url: 'datacheck_preser',
                    data:data
                })
            });

            var modeltitle
            $(document).off('click','.datacheck-but-ck').on('click','.datacheck-but-ck',function(){
                X.parametersopen({},'datacheck_list','rt',function(d){
                    var data = d;
                    var props = data.check.props;
                    var where =  data.check.where;
                    baseeventdata = data.check.default_field;
                    var eventData=[];
                    var conditionData=[];
                    for(let i in props){
                        var arr = {
                            'attrname':i,
                            'v':props[i]
                        }
                        eventData.push(arr);
                    }
                    eventarr = eventData;
                    for(let i in where){
                        var arr = {
                            'conditionname':i,
                            'v':where[i]
                        }
                        conditionData.push(arr);
                    }
                    conditionarr = conditionData;
                    modeltitle = data.title;
                    layui.form.val('datacheckexample',{
                        'game':data['game'],
                        'db_name':data['check']['db_name'],
                        'eventname':data['check']['event_name'],
                        'only':data['check']['is_unique'],
                    });
                    
                    X.laytpldata("#eventattr-box-dot",eventarr,"#datacheck-eventattr-box");
                    X.laytpldata("#condition-box-dot",conditionarr,"#datacheck-condition-box");
                    layui.form.render();
                })
            });
            

            // 查询按钮
            $(document).off('click','.datacheck-but-cx').on('click','.datacheck-but-cx',function(){
                var fromdata = layui.form.val('datacheckexample');
                var is_unique;
                // console.log(fromdata);
                if(fromdata.only == "true"){
                    is_unique = true
                }else {
                    is_unique = false
                }
        
                var props={},where={};
                for(let i in eventarr){
                    props[eventarr[i]['attrname']] = eventarr[i]['v']
                }
                for(let i in conditionarr){
                    where[conditionarr[i]['conditionname']] = conditionarr[i]['v']
                }
        
                var data={
                    'game': fromdata.game,
                    'db_name':fromdata.db_name,
                    'event_name':fromdata.eventname,
                    'is_unique':is_unique,
                    'props':props,
                    'where':where,
                    'default_field':baseeventdata
                };
                //
                X.api('check_data/check','post',data,function(d){
                    $(".datacheck-result-info-tongguo").html("");
                    $(".datacheck-result-info-cuowu").html("");

                    // X.laytpldata("#datacheck-table-title-box",d.title,".table-fenxi-th");
                    // X.laytpldata("#datacheck-tbody-title-box",d.data,".table-fenxi-td");

                    var data=d;
                    var pass_list = data.pass_list;
                    var fail_list = data.fail_list;
                    for(let i in pass_list){
                        $(".datacheck-result-info-tongguo").append(pass_list[i]);
                    }
                    for(let i in fail_list){
                        $(".datacheck-result-info-cuowu").append(fail_list[i]);
                    }

                    var headdata=[];
                    for(let i in data.title){
                        var arr =  {
                            width: "150px",
                            field: data.title[i],
                            fixed: false
                        };
                        if(i == 0){
                            arr['fixed'] = true;
                        }
                        headdata.push(arr);
                    }
                    // X.fixedTable("datacheck-table-box",'row-col-fixed','',headdata,data.data);
                   
                    
                    X.laytpldata("#datacheck-table-title-box",data.title,".table-fenxi-th");
                    X.laytpldata("#datacheck-tbody-title-box",data.data ,".table-fenxi-td");

                })
            });

        }
    };
})();