function changeTypeTo_OnePlus(){
    console.log('changeToOnePlus');
    $("#reward_info").empty();
    initOnePlusWrap();
}
function initReward_OnePlus(){
    console.log('initReward_OnePlus-reward_info',reward_info);
    changeTypeTo_OnePlus();

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

    var products = {1837: "商品1", 1838: "商品2"};
    console.log('1+1:reward_info',reward_infoNew);
    if(reward_infoNew){
        $.each(reward_infoNew, function (i, o) {
            console.log(i, o)
            $('#addOnePlusSpinsWrapBtn').click();
            $('input[name="onePlusConfig['+i+'][start]"]').val(o.start);
            $('input[name="onePlusConfig['+i+'][end]"]').val(o.end);
            $.each(o.data, function(i1, o1){
                $('.addOnePlusShushuWrapBtn:last').click();
                $('select[name="onePlusConfig['+i+'][data]['+i1+'][shushu][]"]').val(o1.shushu);
                $.each(o1.data, function(i2, o2){
                    $('select[name="onePlusConfig['+i+'][data]['+i1+'][data]['+i2+'][gprice]"]').val(o2.gprice);
                    $('input[name="onePlusConfig['+i+'][data]['+i1+'][data]['+i2+'][gsku]"]').val(o2.gsku);
                    $('input[name="onePlusConfig['+i+'][data]['+i1+'][data]['+i2+'][gdiscount]"]').val(1);
                    $('input[name="onePlusConfig['+i+'][data]['+i1+'][data]['+i2+'][gid]"]').val(o2.gid);
                    $('input[name="onePlusConfig['+i+'][data]['+i1+'][data]['+i2+'][prizes]"]').val(escape(JSON.stringify(o2.prizes)));
                    var prizeHtml = '';
                    $.each(o2.prizes, function (a,prizes){
                        prizeHtml += chestPropsHtml(prizes)
                    });
                    $('input[name="onePlusConfig['+i+'][data]['+i1+'][data]['+i2+'][gid]"]').next('.prizeShow').html(prizeHtml)
                });
            });
        })
    }
}

function onePlusWrapStageTable(){
    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="addOnePlusSpinsWrapBtn" onclick="addOnePlusSpinsWrap(this)" type="button">添加等级层级</button>';
    html+='<button class="btn btn-white"   id="delOnePlusSpinsWrapBtn" onclick="delOnePlusSpinsWrap()" type="button">删除等级层级</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    return html;
}

function addOnePlusSpinsWrap(obj){
    OnePlusSpinsCnt++
    onePlusShushuStages[OnePlusSpinsCnt] = 0
    var html = ''
        +       '<div class="form-group onePlusSpinsWrap " id="OnePlusSpinsWrap_'+OnePlusSpinsCnt+'" data-idx="'+OnePlusSpinsCnt+'" 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="onePlusConfig['+OnePlusSpinsCnt+'][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="onePlusConfig['+OnePlusSpinsCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton class="btn btn-primary  addOnePlusShushuWrapBtn" onclick="addOnePlusShushuWrap( this,1)">添加数数分组层级</botton>'
        +           '<botton class="btn btn-danger del-tr delOnePlusShushuWrapBtn" onclick="delOnePlusShushuWrap( this,1)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    $('.select2').select2({})
}
function delOnePlusSpinsWrap(){
    if(OnePlusSpinsCnt < 1){
        return;
    }
    $('#OnePlusSpinsWrap_'+OnePlusSpinsCnt).remove();
    OnePlusSpinsCnt--;
}

function addOnePlusShushuWrap(obj,c=0){
    var spinsIdx = $(obj).parents('.onePlusSpinsWrap').data('idx')
    onePlusShushuStages[spinsIdx]++
    console.log(spinsIdx)

    var rewardIdx = spinsIdx + '_' + onePlusShushuStages[spinsIdx]
    OnePlusRewardStages[rewardIdx] = 1836
    var html = '';
    html +='<table class="table table-bordered onePlusShushuWrap" id="OnePlusShushuWrap_'+rewardIdx+'" data-idx="'+onePlusShushuStages[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="onePlusConfig['+spinsIdx+'][data]['+onePlusShushuStages[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><th>价格</th>';
    html +=     '<th>SKU</th>';
    // html +=     '<th>商品折扣</th>';
    html +=     '<th>奖品</th>';
    html +=    '<th>奖励设置</th></tr></thead>' ;
    html +='<tbody id="OnePlusRewardBody_'+rewardIdx+'">';

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

function addOnePlusRewardTr(obj){
    var spinsIdx = $(obj).parents('.onePlusSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.onePlusShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    OnePlusRewardStages[rewardIdx]++
    var showHtml = '';
    if(OnePlusRewardStages[rewardIdx] > 1837)showHtml = 'style="display:none"';
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + OnePlusRewardStages[rewardIdx]
    var prizes = '[]';
    var prize_show_arr = JSON.parse(prizes);
    prizes = escape(prizes);
    var html ='';
    html+='<tr id="onePlusRewardInfo_'+rewardKey+'" data-idx="'+OnePlusRewardStages[rewardIdx]+'" class="commonStageRewardTr priceSkuGroup">';
    html +='<td><select '+showHtml+' onchange="priceChanged(this)"   class="form-control"  name="onePlusConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+OnePlusRewardStages[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="onePlusConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+OnePlusRewardStages[rewardIdx]+'][gsku]" value="" readonly></td>';
    html +='<input type="hidden" class="form-control"  name="onePlusConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+OnePlusRewardStages[rewardIdx]+'][gdiscount]" value="1">';
    html+= '<input type="hidden" class="form-control"  name="onePlusConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+OnePlusRewardStages[rewardIdx]+'][gid]" value="'+(OnePlusRewardStages[rewardIdx])+'">';
    html +='<td class="prizeShow"></td>';
    html +='<td><input type="hidden" class="prizeInput" name="onePlusConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+OnePlusRewardStages[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>';
    $('#OnePlusRewardBody_'+rewardIdx).append(html)
}
function delOnePlusRewardTr(obj){
    var spinsIdx = $(obj).parents('.onePlusSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.onePlusShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + OnePlusRewardStages[rewardIdx]
    if(OnePlusRewardStages[rewardIdx] < 1)return;
    $('#onePlusRewardInfo_'+rewardKey).remove()
    OnePlusRewardStages[rewardIdx]--
}





function initOnePlusWrap(){
    OnePlusSpinsCnt = 0;
    onePlusShushuStages = {}
    OnePlusRewardStages = {}
    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 +=onePlusWrapStageTable();

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