function changeTypeTo_CallbackScore(){
    console.log('changeToCallbackScore');
    $("#reward_info").empty();
    initCallbackScoreWrap();
}
function initReward_CallbackScore(){
    console.log('initReward_CallbackScore-reward_info',reward_info);
    changeTypeTo_CallbackScore();

    // 最大购买次数回写
    if(reward_conf && reward_conf.maxNum){
        $('input[name=maxNum]').val(reward_conf.maxNum)
    }
    var reward_infoNew = reward_info
    // 奖励回写

    console.log('1+1:reward_info',reward_infoNew);
    if(reward_infoNew){
        $.each(reward_infoNew, function (i, o) {
            console.log(i, o)
            $('#addCallbackScoreSpinsWrapBtn').click();
            $('input[name="callbackScore['+i+'][start]"]').val(o.start);
            $('input[name="callbackScore['+i+'][end]"]').val(o.end);
            $.each(o.data, function(i1, o1){
                $('.addCallbackScoreShushuWrapBtn:last').click();
                $('select[name="callbackScore['+i+'][data]['+i1+'][shushu][]"]').val(o1.shushu);
                $('input[name="callbackScore['+i+'][data]['+i1+'][data]"]').val(escape(JSON.stringify(o1.data)));
                var prizeHtml = '';
                console.log(o1)
                $.each(o1.data, function(i2, prizes){
                    // $('select[name="callbackScore['+i+'][data]['+i1+'][data]['+i2+'][gprice]"]').val(o2.gprice);
                    // $('input[name="callbackScore['+i+'][data]['+i1+'][data]['+i2+'][gsku]"]').val(o2.gsku);
                    // $('input[name="callbackScore['+i+'][data]['+i1+'][data]['+i2+'][gdiscount]"]').val(1);
                    // $('input[name="callbackScore['+i+'][data]['+i1+'][data]['+i2+'][gid]"]').val(o2.gid);
                    // $('input[name="callbackScore['+i+'][data]['+i1+'][data]['+i2+'][prizes]"]').val(escape(JSON.stringify(o2.prizes)));
                    // $.each(o2.data, function (a,prizes){
                        prizeHtml += chestPropsHtml(prizes)
                    // });
                });
                $('.prizeShow:last').html(prizeHtml)
            });
        })
    }
}

function callbackScoreWrapStageTable(){
    var html ='';
    html+='<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    html +='<div class="" style="top:10px">';
    var str ='分层级配置';
    html +='<div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">'+str+'</h5></div>';

    html+='</div>';
    html+='<div class="form-group"><div class=" ">';
    html+='<button class="btn btn-primary" id="addCallbackScoreSpinsWrapBtn" onclick="addCallbackScoreSpinsWrap(this)" type="button">添加等级层级</button>';
    html+='<button class="btn btn-white"   id="delCallbackScoreSpinsWrapBtn" onclick="delCallbackScoreSpinsWrap()" type="button">删除等级层级</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    return html;
}

function addCallbackScoreSpinsWrap(obj){
    CallbackScoreSpinsCnt++
    callbackScoreShushuStages[CallbackScoreSpinsCnt] = 0
    var html = ''
        +       '<div class="form-group callbackScoreSpinsWrap " id="CallbackScoreSpinsWrap_'+CallbackScoreSpinsCnt+'" data-idx="'+CallbackScoreSpinsCnt+'" style="margin:10px;padding:10px;border:solid 1px black;">'
        +           '<div class="row">'
        +               '<div class="col-sm-2"><lable class="form-control">等级区间：</lable></div>'
        +               '<div class="col-sm-3">'
        +                  '<input name="callbackScore['+CallbackScoreSpinsCnt+'][start]" value="0">'
        +               '</div>'
        +               '<div class="col-sm-2"><lable class="form-control" style="text-align: center">-</lable></div>'
        +               '<div class="col-sm-3">'
        +                  '<input name="callbackScore['+CallbackScoreSpinsCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton class="btn btn-primary  addCallbackScoreShushuWrapBtn" onclick="addCallbackScoreShushuWrap( this,1)">添加数数分组层级</botton>'
        +           '<botton class="btn btn-danger del-tr delCallbackScoreShushuWrapBtn" onclick="delCallbackScoreShushuWrap( this,1)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    $('.select2').select2({})
}
function delCallbackScoreSpinsWrap(){
    if(CallbackScoreSpinsCnt < 1){
        return;
    }
    $('#CallbackScoreSpinsWrap_'+CallbackScoreSpinsCnt).remove();
    CallbackScoreSpinsCnt--;
}

function addCallbackScoreShushuWrap(obj,c=0){
    var spinsIdx = $(obj).parents('.callbackScoreSpinsWrap').data('idx')
    callbackScoreShushuStages[spinsIdx]++
    console.log(spinsIdx)

    var rewardIdx = spinsIdx + '_' + callbackScoreShushuStages[spinsIdx]
    CallbackScoreRewardStages[rewardIdx] = 0
    var html = '';
    html +='<table class="table table-bordered callbackScoreShushuWrap" id="CallbackScoreShushuWrap_'+rewardIdx+'" data-idx="'+callbackScoreShushuStages[spinsIdx]+'">';
    html +='<thead><tr><td colspan="5">';
    html +='<div class="form-group"><label class="col-sm-1 control-label m-b">数数分组 </label><div class="col-sm-10 input-group">';
    html +='<select class=" select2" name="callbackScore['+spinsIdx+'][data]['+callbackScoreShushuStages[spinsIdx]+'][shushu][]"  multiple  style="min-width: 100%">';
    $.each(shushuGroups, function(i,o){
        html += '<option value="'+i+'">'+o+'</option>'
    });
    html +='</select>';
    html +='</div></div>';
    html +='</td></tr>';
    html +='<tr>';
    // html +=     '<th>价格</th>';
    // html +=     '<th>SKU</th>';
    // html +=     '<th>商品折扣</th>';
    html +=     '<th>奖品</th>';
    html +=    '<th>奖励设置</th></tr></thead>' ;
    html +='<tbody id="CallbackScoreRewardBody_'+rewardIdx+'">';

    html +='</tbody>' +
        '<tr style="display: none">' +
        '<td colspan="5">' +
        '<div class="text-center">' +
        '<botton class="btn btn-primary addCallbackScoreRewardTrBtn" onclick="addCallbackScoreRewardTr(this)">添加</botton>' +
        '<botton class="btn btn-danger del-tr" onclick="delCallbackScoreRewardTr(this)">删除</botton>' +
        '</div>' +
        '</td>' +
        '</tr>' +
        '</table>';
    $(obj).before(html)
    $(obj).prev('#CallbackScoreShushuWrap_'+rewardIdx).find('.addCallbackScoreRewardTrBtn').click()
    $('.select2').select2({})
}
function delCallbackScoreShushuWrap(obj){
    var spinsIdx = $(obj).parents('.callbackScoreSpinsWrap').data('idx')
    // console.log(spinsIdx)
    if(callbackScoreShushuStages[spinsIdx] < 1)return
    $(obj).parents('.callbackScoreSpinsWrap').find('.callbackScoreShushuWrap:last').remove()
    callbackScoreShushuStages[spinsIdx]--
}

function addCallbackScoreRewardTr(obj){
    var spinsIdx = $(obj).parents('.callbackScoreSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.callbackScoreShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    CallbackScoreRewardStages[rewardIdx]++
    var showHtml = '';
    if(CallbackScoreRewardStages[rewardIdx] > 1837)showHtml = 'style="display:none"';
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + CallbackScoreRewardStages[rewardIdx]
    var prizes = '[]';
    var prize_show_arr = JSON.parse(prizes);
    prizes = escape(prizes);
    var html ='';
    html+='<tr id="callbackScoreRewardInfo_'+rewardKey+'" data-idx="'+CallbackScoreRewardStages[rewardIdx]+'" class="commonStageRewardTr priceSkuGroup">';
    // html +='<td><select '+showHtml+' onchange="priceChanged(this)"   class="form-control"  name="callbackScore['+spinsIdx+'][data]['+shushuIdx+'][data]['+CallbackScoreRewardStages[rewardIdx]+'][gprice]" >' ;
    // $.each(goodSku, function (k, v) {
    //     html += '<option value="' + v["price"] + '" >' + v["price"] + '</option>';
    // });
    // html +='</select></td>';
    // html +='<td><input type="text"  '+showHtml+'  class="form-control skuInput"  name="callbackScore['+spinsIdx+'][data]['+shushuIdx+'][data]['+CallbackScoreRewardStages[rewardIdx]+'][gsku]" value="" readonly></td>';
    // html +='<input type="hidden" class="form-control"  name="callbackScore['+spinsIdx+'][data]['+shushuIdx+'][data]['+CallbackScoreRewardStages[rewardIdx]+'][gdiscount]" value="1">';
    // html+= '<input type="hidden" class="form-control"  name="callbackScore['+spinsIdx+'][data]['+shushuIdx+'][data]['+CallbackScoreRewardStages[rewardIdx]+'][gid]" value="'+(CallbackScoreRewardStages[rewardIdx])+'">';
    html +='<td class="prizeShow"></td>';
    html +='<td><input type="hidden" class="prizeInput" name="callbackScore['+spinsIdx+'][data]['+shushuIdx+'][data]" value="'+prizes+'"><button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,\'setCommonPrize\',1)">奖励配置</button></td>';// data-toggle="modal"
    // html +='<td><input type="hidden" class="prizeInput" name="callbackScore['+spinsIdx+'][data]['+shushuIdx+'][data]['+CallbackScoreRewardStages[rewardIdx]+'][prizes]" value="'+prizes+'"><button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,\'setCommonPrize\',1)">奖励配置</button></td>';// data-toggle="modal"
    html += '</td>';
    $('#CallbackScoreRewardBody_'+rewardIdx).append(html)
}
function delCallbackScoreRewardTr(obj){
    var spinsIdx = $(obj).parents('.callbackScoreSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.callbackScoreShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + CallbackScoreRewardStages[rewardIdx]
    if(CallbackScoreRewardStages[rewardIdx] < 1)return;
    $('#callbackScoreRewardInfo_'+rewardKey).remove()
    CallbackScoreRewardStages[rewardIdx]--
}





function initCallbackScoreWrap(){
    CallbackScoreSpinsCnt = 0;
    callbackScoreShushuStages = {}
    CallbackScoreRewardStages = {}
    var html = ''
    // html +='<div class="form-group">';
    // html +='<label class="col-xs-1 control-label">最大购买次数:</label><div class="col-sm-1"><input type="text"  name="maxNum" value="1" class="form-control"></div>';
    // html +='</div>';
    html +=callbackScoreWrapStageTable();

    $("#reward_info").append(html);
    initLayDate();
}