function changeTypeTo_shopPromotion(){
    console.log('changeToTheme')
    $("#reward_info").empty();
    initShopPromotionWrap();
}
function initReward_shopPromotion(){
    console.log('initTheme')
    changeTypeTo_shopPromotion()

    // 奖励回写
    console.log('reward_conf',reward_conf)
    if(reward_conf){
        $.each(reward_conf, function (i, o) {
            $('#addShopPromotionSpinsWrapBtn').click()
            $('input[name="shopPromotionConfig['+i+'][start]"]').val(o.start)
            $('input[name="shopPromotionConfig['+i+'][end]"]').val(o.end)
            $.each(o.stages, function(i1, o1){
                $('select[name="shopPromotionConfig['+i+'][stages]['+i1+'][shushu][]"]').val(o1.shushu)
                $('input[name="shopPromotionConfig['+i+'][stages]['+i1+'][data][buyNum]"]').val(o1.data.buyNum)
                $('input[name="shopPromotionConfig['+i+'][stages]['+i1+'][data][bet]"]').val(o1.data.bet)
                $.each(o1.data.list, function(i2, o2){
                    i2 = o2.id
                    if(!o2.gid)o2.gid=100+parseInt(i2)
                    $('input[name="shopPromotionConfig['+i+'][stages]['+i1+'][data][list]['+i2+'][id]"]').val(o2.id)
                    $('input[name="shopPromotionConfig['+i+'][stages]['+i1+'][data][list]['+i2+'][gid]"]').val(o2.gid)
                    $('input[name="shopPromotionConfig['+i+'][stages]['+i1+'][data][list]['+i2+'][name]"]').val(o2.name)
                    $('input[name="shopPromotionConfig['+i+'][stages]['+i1+'][data][list]['+i2+'][spins]"]').val(o2.spins)
                })
            })
        })
    }
}


function addShopPromotionSpinsWrap(obj){
    shopPromotionSpinsCnt++
    shopPromotionShushuStages[shopPromotionSpinsCnt] = 0
    var html = ''
        +       '<div class="form-group shopPromotionSpinsWrap " id="shopPromotionSpinsWrap_'+shopPromotionSpinsCnt+'" data-idx="'+shopPromotionSpinsCnt+'" 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="shopPromotionConfig['+shopPromotionSpinsCnt+'][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="shopPromotionConfig['+shopPromotionSpinsCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton  style="display: none" class="btn btn-primary  addShopPromotionShushuWrapBtn" onclick="addShopPromotionShushuWrap( this)">添加数数分组层级</botton>'
        +           '<botton  style="display: none" class="btn btn-danger del-tr delShopPromotionShushuWrapBtn" onclick="delShopPromotionShushuWrap( this)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    $('.addShopPromotionShushuWrapBtn:last').click()
    $('.select2').select2({})
}
function delShopPromotionSpinsWrap(){
    if(shopPromotionSpinsCnt < 1){
        return;
    }
    $('#shopPromotionSpinsWrap_'+shopPromotionSpinsCnt).remove();
    shopPromotionSpinsCnt--;
}

function addShopPromotionShushuWrap(obj){
    var spinsIdx = $(obj).parents('.shopPromotionSpinsWrap').data('idx')
    console.log(spinsIdx)
    shopPromotionShushuStages[spinsIdx]++
    var rewardIdx = spinsIdx + '_' + shopPromotionShushuStages[spinsIdx]
    shopPromotionRewardStages[rewardIdx] = 0
    var html = '';
    html +='<table class="table table-bordered shopPromotionShushuWrap" id="shopPromotionShushuWrap_'+rewardIdx+'" data-idx="'+shopPromotionShushuStages[spinsIdx]+'">';
    html +='<thead><tr><td colspan="5">';
    html +='<div class="form-group" style="display: none" ><label class="col-sm-1 control-label m-b">数数分组 </label><div class="col-sm-10 input-group">';
    html +='<select class=" select2" name="shopPromotionConfig['+spinsIdx+'][stages]['+shopPromotionShushuStages[spinsIdx]+'][shushu][]"  multiple  style="min-width: 100%">';
    $.each(shushuGroups, function(i,o){
        html += '<option value="'+i+'">'+o+'</option>'
    });
    html +='</select>';
    html +='</div></div>';
    html += ''
    +           '<div class="row">'
    +               '<div class="col-sm-2"><lable class="form-control">购买次数：</lable></div>'
    +               '<div class="col-sm-3">'
    +                  '<input name="shopPromotionConfig['+spinsIdx+'][stages]['+shopPromotionShushuStages[spinsIdx]+'][data][buyNum]" 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="shopPromotionConfig['+spinsIdx+'][stages]['+shopPromotionShushuStages[spinsIdx]+'][data][bet]" value="0">'
    +               '</div>'
    +           '</div>'

    html +='</td></tr>';
    html +=' <tr>' ;
    html +=     '<th>体力序号</th>';
    html +=     '<th>体力数</th>';
    html +=   '</tr>'
    html += '</thead>' ;
    html +='<tbody id="shopPromotionRewardBody_'+rewardIdx+'">';

    html +='</tbody>' +
        '<tr style="display: none">' +
        '<td colspan="5">' +
        '<div class="text-center">' +
        '<botton class="btn btn-primary addShopPromotionRewardTrBtn" onclick="addShopPromotionRewardTr(this)">添加</botton>' +
        '<botton class="btn btn-danger del-tr" onclick="delShopPromotionRewardTr(this)">删除</botton>' +
        '</div>' +
        '</td>' +
        '</tr>' +
        '</table>';
    $(obj).before(html)
    $('select[name="shopPromotionConfig['+spinsIdx+'][stages]['+shopPromotionShushuStages[spinsIdx]+'][shushu][]"]').val([0])
    while(shopPromotionRewardStages[rewardIdx] < 6){
        $('.addShopPromotionRewardTrBtn:last').click()
    }
    $('.select2').select2({})
}
function delShopPromotionShushuWrap(obj){
    var spinsIdx = $(obj).parents('.shopPromotionSpinsWrap').data('idx')
    console.log(spinsIdx)
    if(shopPromotionShushuStages[spinsIdx] < 1)return
    $(obj).parents('.shopPromotionSpinsWrap').find('.shopPromotionShushuWrap:last').remove()
    shopPromotionShushuStages[spinsIdx]--
}

function addShopPromotionRewardTr(obj){
    var spinsIdx = $(obj).parents('.shopPromotionSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.shopPromotionShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    shopPromotionRewardStages[rewardIdx]++
    var gid = 100 + parseInt(shopPromotionRewardStages[rewardIdx])
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + shopPromotionRewardStages[rewardIdx]
    var prize = '[]';
    var prize_show_arr = JSON.parse(prize);
    prize = escape(prize);
    var html ='';
    html+='<tr id="shopPromotionRewardInfo_'+rewardKey+'" data-idx="'+shopPromotionRewardStages[rewardIdx]+'" class="commonStageRewardTr ">';
    html +='<td>';
    html +=     '<input type="hidden" class="form-control "  name="shopPromotionConfig['+spinsIdx+'][stages]['+shushuIdx+'][data][list]['+shopPromotionRewardStages[rewardIdx]+'][id]" value="'+shopPromotionRewardStages[rewardIdx]+'" readonly>';
    html +=     '<input type="hidden" class="form-control "  name="shopPromotionConfig['+spinsIdx+'][stages]['+shushuIdx+'][data][list]['+shopPromotionRewardStages[rewardIdx]+'][gid]" value="'+gid+'" readonly>';
    html +=     '<input type="text"   class="form-control "  name="shopPromotionConfig['+spinsIdx+'][stages]['+shushuIdx+'][data][list]['+shopPromotionRewardStages[rewardIdx]+'][name]" value="体力'+shopPromotionRewardStages[rewardIdx]+'" readonly>';
    html +='</td>';
    html +='<td><input type="text" class="form-control"  name="shopPromotionConfig['+spinsIdx+'][stages]['+shushuIdx+'][data][list]['+shopPromotionRewardStages[rewardIdx]+'][spins]" value="0"></td>';
    html += '</tr>';
    $('#shopPromotionRewardBody_'+rewardIdx).append(html)
}
function delShopPromotionRewardTr(obj){
    var spinsIdx = $(obj).parents('.shopPromotionSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.shopPromotionShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + shopPromotionRewardStages[rewardIdx]
    if(shopPromotionRewardStages[rewardIdx] < 1)return;
    $('#shopPromotionRewardInfo_'+rewardKey).remove()
    shopPromotionRewardStages[rewardIdx]--
}





function initShopPromotionWrap(){
    shopPromotionSpinsCnt = 0;
    shopPromotionShushuStages = {}
    shopPromotionRewardStages = {}
    var html = ''
    html+='<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    html +='<div class="" id="spinStageTable" 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="addShopPromotionSpinsWrapBtn" onclick="addShopPromotionSpinsWrap(this)" type="button">添加等级层级</button>';
    html+='<button class="btn btn-white"   id="delShopPromotionSpinsWrapBtn" onclick="delShopPromotionSpinsWrap()" type="button">删除等级层级</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';


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