var isInitReward =0;
function changeTypeTo_SafeSurprise(){
    console.log('changeToTheme');
    $("#reward_info").empty();
    initSafeSurpriseWrap();
}
function initReward_SafeSurprise(){
    isInitReward =1
    console.log('initReward_SafeSurprise-reward_info',reward_info);
    changeTypeTo_SafeSurprise();
    var reward_infoNew =[];
    if(reward_info[0]['start']==undefined){//数据兼容
        var newData =[];
        $.each(reward_info, function(i,o){
            newData[i] = {"shushu":o.gid,"data":o.data};
        });
        reward_infoNew[0] = {"start":1,"end":300,"data":newData};
    }else{
        reward_infoNew = reward_info;
    }
    // 奖励回写
    console.log('惊喜礼包:reward_info',reward_infoNew);
    if(reward_infoNew){
        var i=0;
        $.each(reward_infoNew, function (i_, o) {

            $('#addSafeSurpriseSpinsWrapBtn').click();
            $('input[name="SafeSurpriseConfig['+i+'][start]"]').val(o.start);
            $('input[name="SafeSurpriseConfig['+i+'][end]"]').val(o.end);
            var i1 =0;
            $.each(o.data, function(i1_, o1){
                $('.addSafeSurpriseShushuWrapBtn:last').click();
                $('select[name="SafeSurpriseConfig['+i+'][data]['+i1+'][shushu][]"]').val(o1.shushu);
                $.each(o1.data, function(i2, o2){
                    i2 = i2>2?i2-1838:i2;
                    console.log("stage:"+i2+"-info:",o2);
                    console.log('SafeSurpriseConfig['+i+'][data]['+i1+'][data]['+i2+']');
                    $('.addSafeSurpriseRewardTrBtn:last').click();
                    $('select[name="SafeSurpriseConfig['+i+'][data]['+i1+'][data]['+i2+'][gprice]"]').val(o2.gprice);
                    $('input[name="SafeSurpriseConfig['+i+'][data]['+i1+'][data]['+i2+'][gsku]"]').val(o2.gsku);
                    $('input[name="SafeSurpriseConfig['+i+'][data]['+i1+'][data]['+i2+'][gdiscount]"]').val(o2.gdiscount);
                    $('input[name="SafeSurpriseConfig['+i+'][data]['+i1+'][data]['+i2+'][gid]"]').val(o2.gid);
                    $('input[name="SafeSurpriseConfig['+i+'][data]['+i1+'][data]['+i2+'][prizes]"]').val(escape(JSON.stringify(o2.prizes)));
                    var prizeHtml = '';
                    // console.log("prizes;",o2.prizes)
                    $.each(o2.prizes, function (a,prizes){
                        // console.log(prizes)
                        prizeHtml += chestPropsHtml(prizes)
                    });
                     // console.log("prizeHtml;",prizeHtml)
                    $('.prizeShow:last').html(prizeHtml)
                });
                i1++;
            });
            i++;
        })
    }
    isInitReward=0;
}

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

function addSafeSurpriseSpinsWrap(obj){

    SafeSurpriseShushuStages[SafeSurpriseSpinsCnt] = 0
    var html = ''
        +       '<div class="form-group SafeSurpriseSpinsWrap " id="SafeSurpriseSpinsWrap_'+SafeSurpriseSpinsCnt+'" data-idx="'+SafeSurpriseSpinsCnt+'" 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="SafeSurpriseConfig['+SafeSurpriseSpinsCnt+'][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="SafeSurpriseConfig['+SafeSurpriseSpinsCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton class="btn btn-primary  addSafeSurpriseShushuWrapBtn" onclick="addSafeSurpriseShushuWrap( this,1)">添加数数分组层级</botton>'
        +           '<botton class="btn btn-danger del-tr delSafeSurpriseShushuWrapBtn" onclick="delSafeSurpriseShushuWrap( this,1)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    SafeSurpriseSpinsCnt++
    $('.select2').select2({})
}
function delSafeSurpriseSpinsWrap(){
    if(SafeSurpriseSpinsCnt < 1){
        return;
    }
    $('#SafeSurpriseSpinsWrap_'+SafeSurpriseSpinsCnt).remove();
    SafeSurpriseSpinsCnt--;
}

function addSafeSurpriseShushuWrap(obj,c=0){
    var spinsIdx = $(obj).parents('.SafeSurpriseSpinsWrap').data('idx')
    console.log(spinsIdx)

    var rewardIdx = spinsIdx + '_' + SafeSurpriseShushuStages[spinsIdx]
    SafeSurpriseRewardStages[rewardIdx] = 0
    var html = '';
    html +='<table class="table table-bordered SafeSurpriseShushuWrap" id="SafeSurpriseShushuWrap_'+rewardIdx+'" data-idx="'+SafeSurpriseShushuStages[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="SafeSurpriseConfig['+spinsIdx+'][data]['+SafeSurpriseShushuStages[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="SafeSurpriseRewardBody_'+rewardIdx+'">';

    html +='</tbody>' +
        '<tr style="display: none">' +
        '<td colspan="5">' +
        '<div class="text-center">' +
        '<botton class="btn btn-primary addSafeSurpriseRewardTrBtn" onclick="addSafeSurpriseRewardTr(this)">添加</botton>' +
        '<botton class="btn btn-danger del-tr" onclick="delSafeSurpriseRewardTr(this)">删除</botton>' +
        '</div>' +
        '</td>' +
        '</tr>' +
        '</table>';
    $(obj).before(html)
    SafeSurpriseShushuStages[spinsIdx]++
    if(isInitReward!=1){
        $(obj).prev('#SafeSurpriseShushuWrap_'+rewardIdx).find('.addSafeSurpriseRewardTrBtn').click()
        $(obj).prev('#SafeSurpriseShushuWrap_'+rewardIdx).find('.addSafeSurpriseRewardTrBtn').click()
    }
    $('.select2').select2({})
}
function delSafeSurpriseShushuWrap(obj){
    var spinsIdx = $(obj).parents('.SafeSurpriseSpinsWrap').data('idx')
    // console.log(spinsIdx)
    if(SafeSurpriseShushuStages[spinsIdx] < 1)return
    $(obj).parents('.SafeSurpriseSpinsWrap').find('.SafeSurpriseShushuWrap:last').remove()
    SafeSurpriseShushuStages[spinsIdx]--
}

function addSafeSurpriseRewardTr(obj){
    var spinsIdx = $(obj).parents('.SafeSurpriseSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.SafeSurpriseShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    SafeSurpriseRewardStages[rewardIdx]++
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + SafeSurpriseRewardStages[rewardIdx]
    var prizes = '[]';
    var prize_show_arr = JSON.parse(prizes);
    prizes = escape(prizes);
    var html ='';
    html+='<tr id="SafeSurpriseRewardInfo_'+rewardKey+'" data-idx="'+SafeSurpriseRewardStages[rewardIdx]+'" class="commonStageRewardTr priceSkuGroup">';
    html +='<td><select onchange="priceChanged(this)"   class="form-control"  name="SafeSurpriseConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+SafeSurpriseRewardStages[rewardIdx]+'][gprice]" >' ;
    $.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="SafeSurpriseConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+SafeSurpriseRewardStages[rewardIdx]+'][gsku]" value="" readonly></td>';
    html +='<td><input type="text" class="form-control"  name="SafeSurpriseConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+SafeSurpriseRewardStages[rewardIdx]+'][gdiscount]" value="0"></td>';
    html+= '<input type="hidden" class="form-control"  name="SafeSurpriseConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+SafeSurpriseRewardStages[rewardIdx]+'][gid]" value="'+(SafeSurpriseRewardStages[rewardIdx]+1838)+'">';
    html +='<td class="prizeShow"></td>';
    html +='<td><input type="hidden" class="prizeInput" name="SafeSurpriseConfig['+spinsIdx+'][data]['+shushuIdx+'][data]['+SafeSurpriseRewardStages[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>';
    $('#SafeSurpriseRewardBody_'+rewardIdx).append(html)
}
function delSafeSurpriseRewardTr(obj){
    var spinsIdx = $(obj).parents('.SafeSurpriseSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.SafeSurpriseShushuWrap').data('idx')
    var rewardIdx = spinsIdx + '_' + shushuIdx
    var rewardKey = spinsIdx + '_' + shushuIdx+ '_' + SafeSurpriseRewardStages[rewardIdx]
    if(SafeSurpriseRewardStages[rewardIdx] < 1)return;
    $('#SafeSurpriseRewardInfo_'+rewardKey).remove()
    SafeSurpriseRewardStages[rewardIdx]--
}





function initSafeSurpriseWrap(){
    SafeSurpriseSpinsCnt = 0;
    SafeSurpriseShushuStages = {}
    SafeSurpriseRewardStages = {}
    var html = ''
    html +=SafeSurpriseWrapStageTable();

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