(function(){
    X.pageLogic['groupeditemspop'] = {
        init : function(parms){
            var me = this;
            this.parms = parms;
            this.callback = parms.callback;
            var data = parms.extData;

            var arr = [
                {title:'默认区间',id:'def',checked:true},
                {title:'离散数字',id:'discrete',checked:false},
                {title:'自定义区间',id:'user_defined',checked:false},
            ]
            console.log(data);
            if(data!= ''){
                for(let i in arr){
                    if(arr[i]['id'] == data['intervalType']){
                        arr[i]['checked'] = true
                    }else {
                        arr[i]['checked'] = false
                    }
                }
                // 添加
                var qvjian = [
                    {'rise':'-∞','tail':1},
                    {'rise':1,'tail':2},
                    {'rise':2,'tail':'+∞'},
                ];
                if(data['intervalType'] == 'user_defined'){
                    $(".groupeditemspop-custom-box").show();
                    console.log(data['quotaIntervalArr']);
                    if(data['quotaIntervalArr'].length >= qvjian.length){
                        var num = data['quotaIntervalArr'].length - qvjian.length;
                        for(var i = 0; i < num; i ++){
                            var qrr = {'rise': qvjian[qvjian.length - 2]['tail'] ,'tail': qvjian[qvjian.length - 2]['tail'] + 1 };
                            qvjian.splice( qvjian.length - 1 , 0, qrr);
                        }
                    }
                    for(let i in data['quotaIntervalArr']){
                        if(i == 0){
                            qvjian[i]['tail'] = data['quotaIntervalArr'][i];
                        }else {
                            qvjian[i]['tail'] = data['quotaIntervalArr'][i];
                            qvjian[i]['rise'] = data['quotaIntervalArr'][i - 1];
                        }

                    }
                    var drr = {'rise': data['quotaIntervalArr'][data['quotaIntervalArr'].length - 1] ,'tail':'+∞'}
                    qvjian[qvjian.length - 1 ] = drr;
                }
                

            }
            
           
            X.laytpldata("#groupeditemspop-list-box-dot",arr,".groupeditemspop-list-box");
            layui.form.render();

            layui.form.on('radio(groupeditemspopexample)',function(data){
                if(data.value == "user_defined"){
                    $(".groupeditemspop-custom-box").show();
                }else {
                    $(".groupeditemspop-custom-box").hide();
                }
            })
            
            X.laytpldata("#qvjian-box-dot",qvjian,".qvjian-box");
            $(document).off('click','.qvjian-but').on('click','.qvjian-but',function(){
                var arr = {'rise': qvjian[qvjian.length - 2]['tail'] ,'tail': qvjian[qvjian.length - 2]['tail'] + 1 };
                qvjian.splice( qvjian.length - 1 , 0, arr);
                qvjian[qvjian.length - 1]['rise'] = Number(qvjian[qvjian.length - 2]['tail'])  + 1;
                X.laytpldata("#qvjian-box-dot",qvjian,".qvjian-box");
            })

            $(document).off('click','.qvjian input').on('blur','.qvjian input',function(){
                var index = $(this).attr("data-index");
                var tit = $(this).val();
                for(let i in qvjian){
                    if(i > index){
                        qvjian[i]['tail'] = Number(tit) + 1;
                        qvjian[i]['rise'] = qvjian[ i - 1 ]['tail'];
                    }else if(i == index){
                        qvjian[i]['tail'] = Number(tit);
                    }
                }
                X.laytpldata("#qvjian-box-dot",qvjian,".qvjian-box");
            })

            $(document).off('click','.groupeditemspop-qd').on('click','.groupeditemspop-qd',function(){
                var data = layui.form.val('groupeditemsexample');
                if(data.type == "user_defined"){
                    var quotaIntervalArr=[];
                    for(let i in qvjian){
                        if(i < qvjian.length - 1){
                            quotaIntervalArr.push(qvjian[i]['tail']);
                        }
                    }
                    data['quotaIntervalArr'] = quotaIntervalArr;
                }
                layer.closeAll();
                me.callback && me.callback(data);
            })

        }
    };
})();
