function changeTypeTo_themePack(){
    console.log('changeToTheme')
    $("#reward_info").empty();
    initThemePackWrap();
}
function initReward_themePack(){
    console.log('initTheme')
    changeTypeTo_themePack()

    // 奖励回写
    console.log('reward_conf',reward_conf)
    if(reward_conf){
        $.each(reward_conf, function (i, o) {
            $('#addThemePackSpinsWrapBtn').click()
            $('input[name="themePackConfig['+i+'][start]"]').val(o.start)
            $('input[name="themePackConfig['+i+'][end]"]').val(o.end)
            $.each(o.stages, function(i1, o1){
                $('.addThemePackShushuWrapBtn:last').click()

                $('select[name="themePackConfig['+i+'][stages]['+i1+'][shushu][]"]').val(o1.shushu)
                $.each(o1.list, function(i2, o2){
                    console.log(i2, o2)
                    i2 = o2.stage
                    // $('.addThemePackRewardTrBtn:last').click()
                    $('select[name="themePackConfig['+i+'][stages]['+i1+'][list]['+i2+'][price]"]').val(o2.price)
                    $('input[name="themePackConfig['+i+'][stages]['+i1+'][list]['+i2+'][sku]"]').val(o2.sku)
                    $('input[name="themePackConfig['+i+'][stages]['+i1+'][list]['+i2+'][times]"]').val(o2.times)
                    $('input[name="themePackConfig['+i+'][stages]['+i1+'][list]['+i2+'][prize]"]').val(escape(JSON.stringify(o2.prize)))
                    var prizeHtml = ''
                    console.log(o2.prize)
                    $.each(o2.prize, function (i,prize){
                        console.log(prize)
                        prizeHtml += chestPropsHtml(prize)
                    })
                    $('.prizeShow:last').html(prizeHtml)
                })
            })
        })
    }
}

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

function addThemePackSpinsWrap(obj){
    themePackSpinsCnt++
    themePackShushuStages[themePackSpinsCnt] = 0
    var html = ''
        +       '<div class="form-group themePackSpinsWrap " id="themePackSpinsWrap_'+themePackSpinsCnt+'" data-idx="'+themePackSpinsCnt+'" 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="themePackConfig['+themePackSpinsCnt+'][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="themePackConfig['+themePackSpinsCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton class="btn btn-primary  addThemePackShushuWrapBtn" onclick="addThemePackShushuWrap( this)">添加数数分组层级</botton>'
        +           '<botton class="btn btn-danger del-tr delThemePackShushuWrapBtn" onclick="delThemePackShushuWrap( this)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    $('.select2').select2({})
}
function delThemePackSpinsWrap(){
    if(themePackSpinsCnt < 1){
        return;
    }
    $('#themePackSpinsWrap_'+themePackSpinsCnt).remove();
    themePackSpinsCnt--;
}

function addThemePackShushuWrap(obj){
    var spinsIdx = $(obj).parents('.themePackSpinsWrap').data('idx')
    console.log(spinsIdx)
    themePackShushuStages[spinsIdx]++
    var rewardIdx = spinsIdx + '_' + themePackShushuStages[spinsIdx]
    themePackRewardStages[rewardIdx] = 0
    var html = '';
    html +='<table class="table table-bordered themePackShushuWrap" id="themePackShushuWrap_'+rewardIdx+'" data-idx="'+themePackShushuStages[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="themePackConfig['+spinsIdx+'][stages]['+themePackShushuStages[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="themePackRewardBody_'+rewardIdx+'">';

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

function addThemePackRewardTr(obj){
    var spinsIdx = $(obj).parents('.themePackSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.themePackShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    themePackRewardStages[rewardIdx]++
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + themePackRewardStages[rewardIdx]
    var prize = '[]';
    var prize_show_arr = JSON.parse(prize);
    prize = escape(prize);
    var html ='';
    html+='<tr id="themePackRewardInfo_'+rewardKey+'" data-idx="'+themePackRewardStages[rewardIdx]+'" class="commonStageRewardTr priceSkuGroup">';
    html +='<td><select onchange="priceChanged(this)"   class="form-control"  name="themePackConfig['+spinsIdx+'][stages]['+shushuIdx+'][list]['+themePackRewardStages[rewardIdx]+'][price]" >' ;
    $.each(goodSku, function (k, v) {
        html += '<option value="' + v["price"] + '" >' + v["price"] + '</option>';
    });
    html +='</select></td>';
    html +='<td><input type="text" class="form-control skuInput"  name="themePackConfig['+spinsIdx+'][stages]['+shushuIdx+'][list]['+themePackRewardStages[rewardIdx]+'][sku]" value="" readonly></td>';
    html +='<td><input type="text" class="form-control"  name="themePackConfig['+spinsIdx+'][stages]['+shushuIdx+'][list]['+themePackRewardStages[rewardIdx]+'][times]" value="0"></td>';
    html +='<td class="prizeShow"></td>';
    html +='<td><input type="hidden" class="prizeInput" name="themePackConfig['+spinsIdx+'][stages]['+shushuIdx+'][list]['+themePackRewardStages[rewardIdx]+'][prize]" value="'+prize+'"><button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,\'setCommonPrize\',1)">奖励配置</button></td>';// data-toggle="modal"
    html += '</td>';
    $('#themePackRewardBody_'+rewardIdx).append(html)
}
function delThemePackRewardTr(obj){
    var spinsIdx = $(obj).parents('.themePackSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.themePackShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + themePackRewardStages[rewardIdx]
    if(themePackRewardStages[rewardIdx] < 1)return;
    $('#themePackRewardInfo_'+rewardKey).remove()
    themePackRewardStages[rewardIdx]--
}





function initThemePackWrap(){
    themePackSpinsCnt = 0;
    themePackShushuStages = {}
    themePackRewardStages = {}
    var html = ''
    html +=themePackWrapStageTable();

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