function changeTypeTo_superSerialPack(type){
    console.log('changeTo'+type)
    $("#reward_info").empty();
    initSerialWrap();
}
function initReward_superSerialPack(type){
    console.log('init'+type)
    changeTypeTo_superSerialPack(type)
    // 奖励回写
    console.log('reward_info',reward_info)
    if(extra_reward){
        $.each(extra_reward, function (i, o) {
            $('#addSerialLvWrapBtn').click()
            $('input[name="superSerialConfig['+i+'][start]"]').val(o.start)
            $('input[name="superSerialConfig['+i+'][end]"]').val(o.end)
            $.each(o.stages, function(i1, o1){
                $('.addSerialShushuWrapBtn:last').click()

                $('select[name="superSerialConfig['+i+'][stages]['+i1+'][shushu][]"]').val(o1.shushu)
                var stage = i+'_'+i1
                $.ajax({
                    type: "GET", // 数据提交类型
                    url: "activity/getActStageReward/"+$asyncId+'/'+stage, // 发送地址
                    data: {}, //发送数据
                    async: false, // 是否异步
                    success: function(data) { // 成功回调
                        console.log(stage,'finish')
                        data = JSON.parse(data);
                        o1.list = data.data
                    }
                });
                console.log(stage,'complete')
                $.each(o1.list, function(i2, o2){
                    console.log(i2, o2)
                    $('.addSerialRewardTrBtn:last').click()
                    $('input[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][stage]"]').val(o2.stage)
                    $('input[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][name]"]').val(o2.name)
                    $('input[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][gMax]"]').val(o2.gMax)
                    $('select[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][mtype]"]').val(o2.mtype)
                    $('.priceSkuGroup:last').html( getSerialRewardPayInfoByType(o2.mtype, i, i1, i2))
                    $('input[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][bet]"]').val(o2.bet)
                    $('input[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][sku]"]').val(o2.sku)
                    if(o2.mtype == 1){
                        $('select[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][coin]"]').val(o2.coin)
                    }else{
                        $('select[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][price]"]').val(o2.price)
                    }
                    $('input[name="superSerialConfig['+i+'][stages]['+i1+'][list]['+i2+'][prize]"]').val(escape(JSON.stringify(o2.prize)))
                    console.log(47)
                    var prizeHtml = ''
                    console.log(o2.prize)
                    $.each(o2.prize, function (i,prize){
                        console.log(prize)
                        prizeHtml += chestPropsHtml(prize)
                    })
                    $('.prizeShow:last').html(prizeHtml)
                })
            })
        })
    }
}

function serialWrapStageTable(){
    var html ='';
    html+='<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    html +='<div class="" id="spinStageTable" style="top:10px">';
    html +='<div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold"></h5></div>';

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

function addSerialLvWrap(obj){
    serialLvCnt++
    serialShushuStages[serialLvCnt] = 0
    var html = ''
        +       '<div class="form-group serialLvWrap " id="serialLvWrap_'+serialLvCnt+'" data-idx="'+serialLvCnt+'" 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-4">'
        +                  '<input name="superSerialConfig['+serialLvCnt+'][start]" value="0">'
        +               '</div>'
        +               '<div class="col-sm-2"><lable class="form-control" style="text-align: center">-</lable></div>'
        +               '<div class="col-sm-4">'
        +                  '<input name="superSerialConfig['+serialLvCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton class="btn btn-primary  addSerialShushuWrapBtn" onclick="addSerialShushuWrap( this)">添加数数分组层级</botton>'
        +           '<botton class="btn btn-danger del-tr delSerialShushuWrapBtn" onclick="delSerialShushuWrap( this)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    $('.select2').select2({})
}
function delSerialLvWrap(){
    if(serialLvCnt < 1){
        return;
    }
    $('#serialLvWrap_'+serialLvCnt).remove();
    serialLvCnt--;
}

function addSerialShushuWrap(obj){
    var lvIdx = $(obj).parents('.serialLvWrap').data('idx')
    console.log(lvIdx)
    serialShushuStages[lvIdx]++
    var rewardIdx = lvIdx + '_' + serialShushuStages[lvIdx]
    serialRewardStages[rewardIdx] = 0
    var html = '';
    html +='<table class="table table-bordered serialShushuWrap" id="serialShushuWrap_'+rewardIdx+'" data-idx="'+serialShushuStages[lvIdx]+'">';
    html +='<thead><tr><td colspan="7">';
    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="superSerialConfig['+lvIdx+'][stages]['+serialShushuStages[lvIdx]+'][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>名称</th>';
    html +=     '<th>限量</th>';
    html +=     '<th>购买货币</th>';
    html +=     '<th>价格</th>';
    html +=     '<th>奖励</th>';
    html +=    '<th>奖励设置</th></tr></thead>' ;
    html +='<tbody id="serialRewardBody_'+rewardIdx+'">';

    html +='</tbody><tr><td colspan="7"><div class="text-center"><botton class="btn btn-primary addSerialRewardTrBtn" onclick="addSerialRewardTr(this)">添加</botton><botton class="btn btn-danger del-tr" onclick="delSerialRewardTr(this)">删除</botton>';
    html +='</div></td></tr></table>';
    $(obj).before(html)
    $('.select2').select2({})
}
function delSerialShushuWrap(obj){
    var lvIdx = $(obj).parents('.serialLvWrap').data('idx')
    console.log(lvIdx)
    if(serialShushuStages[lvIdx] < 1)return
    $(obj).parents('.serialLvWrap').find('.serialShushuWrap:last').remove()
    serialShushuStages[lvIdx]--
}

function addSerialRewardTr(obj){
    var lvIdx = $(obj).parents('.serialLvWrap').data('idx')
    var shushuIdx = $(obj).parents('.serialShushuWrap').data('idx')
    var rewardIdx = lvIdx + '_' + shushuIdx
    serialRewardStages[rewardIdx]++
    var rewardKey = lvIdx + '_' + shushuIdx+ '_' + serialRewardStages[rewardIdx]
    var prize = '[]';
    var prize_show_arr = JSON.parse(prize);
    prize = escape(prize);
    var html ='';
    html+='<tr class="serialRewardInfoTr" id="serialRewardInfo_'+rewardKey+'" data-idx="'+serialRewardStages[rewardIdx]+'">';
    html +='<td><input  name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+serialRewardStages[rewardIdx]+'][stage]" type="text"   class="form-control"  value="'+serialRewardStages[rewardIdx]+'" readonly ></td>';
    html +='<td><input  name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+serialRewardStages[rewardIdx]+'][name]" type="text"   class="form-control" value="连环礼包-'+serialRewardStages[rewardIdx]+'" readonly></td>';
    html +='<td><input  name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+serialRewardStages[rewardIdx]+'][gMax]" type="text"   class="form-control" value="0"></td>';
    html +='<td><select name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+serialRewardStages[rewardIdx]+'][mtype]"  onchange="serialPayTypeChange(this)"  class="form-control"  >' +
            '<option value="1">金币</option>' +
            '<option value="2">美元</option>' +
        '</select></td>';

    html +='<td class=" priceSkuGroup">' +
        getSerialRewardPayInfoByType(1, lvIdx, shushuIdx, serialRewardStages[rewardIdx]) +
        '</td>';

    html +='<td class="prizeShow"></td>';
    html +='<td><input type="hidden" class="prizeInput" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+serialRewardStages[rewardIdx]+'][prize]" value="'+prize+'">' +
        '<button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,\'superSerial\',1)">奖励配置</button></td>';// data-toggle="modal"
    html += '</td>';
    $('#serialRewardBody_'+rewardIdx).append(html)
}
function delSerialRewardTr(obj){
    var lvIdx = $(obj).parents('.serialLvWrap').data('idx')
    var shushuIdx = $(obj).parents('.serialShushuWrap').data('idx')
    var rewardIdx = lvIdx + '_' + shushuIdx
    var rewardKey = lvIdx + '_' + shushuIdx+ '_' + serialRewardStages[rewardIdx]
    if(serialRewardStages[rewardIdx] < 1)return;
    $('#serialRewardInfo_'+rewardKey).remove()
    serialRewardStages[rewardIdx]--
}
function serialPayTypeChange(obj) {
    var payType = $(obj).val();
    var lvIdx = $(obj).parents('.serialLvWrap').data('idx')
    var shushuIdx = $(obj).parents('.serialShushuWrap').data('idx')
    var trIdx = $(obj).parents('.serialRewardInfoTr').data('idx')
    console.log(payType, lvIdx, shushuIdx);
    $(obj).parents('.serialRewardInfoTr').find('.priceSkuGroup').html(getSerialRewardPayInfoByType(payType, lvIdx, shushuIdx, trIdx))
}
function getSerialRewardPayInfoByType(payType, lvIdx, shushuIdx, trIdx){
    payType = parseInt(payType)
    var html = '';
    var sel = '';
    var rewardIdx = lvIdx + '_' + shushuIdx
    console.log(rewardIdx)
    switch (payType) {
        case 1://金币
            html += '价格:<select class="form-control m-b" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+trIdx+'][coin]">';
            for(var i=0;i<coins_list.length;i++){
                html += '<option value="' + coins_list[i] + '" ' + sel + '>' + coins_list[i] + '</option>';
            }
            html +='</select>倍数:<input class="form-control" type="text" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+trIdx+'][bet]" value="0">';
            html +='<input type="hidden" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+trIdx+'][sku]" value="0">';
            html +='<input type="hidden" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+trIdx+'][price]" value="0">';
            break;
        case 2://美元
            html += '价格:<select class="form-control m-b selPrice" onchange="priceChanged(this)" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+trIdx+'][price]">';
            $.each(goodSku, function (k, v) {
                html += '<option value="' + v["price"] + '" ' + sel + '>' + v["price"] + '</option>';
            });
            html +='</select>';
            html +='商品sku:<input class="form-control t_sku skuInput" readonly type="text" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+trIdx+'][sku]" value="">';
            html +='<input type="hidden" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+trIdx+'][coin]" value="0">';
            html +='<input type="hidden" name="superSerialConfig['+lvIdx+'][stages]['+shushuIdx+'][list]['+trIdx+'][bet]" value="0">';
            break;
    }
    return html;
}




function initSerialWrap(){
    serialLvCnt = 0;
    serialShushuStages = {}
    serialRewardStages = {}
    var html = ''
    html +=serialWrapStageTable();
    $("#reward_info").append(html);
}