var FoxHammerLevelCnt =0;
var FoxHammerShushuStages=[];
var foxHammerShushuCnt =0;
var foxHammerShushuArr =[];
var foxHammerRewardArr=[];
function initFoxHammerWrap(){
    foxHammerShushuCnt = 0
    foxHammerShushuArr = []
    foxHammerRewardArr = []

    $('#reward_info').empty()
    var html = foxHammerStageTable();
    $('#reward_info').html(html)
}

function foxHammerStageTable() {
    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="addFoxHammerLevelWrapGroup">';
    html+='<button class="btn btn-primary " id="addFoxHammerLevelWrapBtn"  onclick="addFoxHammerLevelWrap(this)" type="button">添加等级层级</button>';
    html+='<button class="btn btn-white " id="delFoxHammerLevelWrapBtn"  onclick="delFoxHammerLevelWrap()" type="button">删除等级层级</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    return html;
}
//添加等级分层
function addFoxHammerLevelWrap(obj) {
    console.log("===addFoxHammerLevelWrap===");
    FoxHammerShushuStages[FoxHammerLevelCnt] = 0
    var html = ''
        +       '<div class="form-group FoxHammerLevelWrap " id="FoxHammerLevelWrap_'+FoxHammerLevelCnt+'" data-idx="'+FoxHammerLevelCnt+'" 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="foxHammerShuhuConfig['+FoxHammerLevelCnt+'][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="foxHammerShuhuConfig['+FoxHammerLevelCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton class="btn btn-primary  addFoxHammerShushuWrapBtn_'+FoxHammerLevelCnt+'" onclick="addFoxHammerShushuWrap( this)">添加数数分组层级</botton>'
        +           '<botton class="btn btn-danger del-tr delFoxHammerShushuWrapBtn_'+FoxHammerLevelCnt+'" onclick="delFoxHammerShushuWrap( this)">删除数数分组层级</botton>'
        +       ' </div>';
    $(obj).before(html)
    FoxHammerLevelCnt++
    $('.select2').select2({})
}
//删除等级分层
function delFoxHammerLevelWrap() {
    if(FoxHammerLevelCnt < 1)return
    FoxHammerLevelCnt--
    $("#FoxHammerLevelWrap_"+FoxHammerLevelCnt).remove();
}

function addFoxHammerShushuWrap(obj){
    // console.log("==addFoxHammerShushuWrap==")
    var lvIdx = $(obj).parents('.FoxHammerLevelWrap').data('idx');
    var rewardKey = lvIdx + '_' + FoxHammerShushuStages[lvIdx];
    console.log("addFoxHammerShushuWrap--rewardKey:"+rewardKey);
    foxHammerShushuCnt = FoxHammerShushuStages[lvIdx];
    // foxHammerShushuArr[rewardKey] = 0
    var html = ''
        +       '<div class="form-group foxHammerSecondWrap" data-idx="'+foxHammerShushuCnt+'" 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-11">'
        +           '<select class="select2 form-control foxHammerShushuSelect2" name="foxHammerShuhuConfig['+lvIdx+'][data]['+foxHammerShushuCnt+'][shushu][]" multiple style="min-width:100px;">';
        $.each(shushuGroups, function(i,o){
            html += '<option value="'+i+'">'+o+'</option>'
        });
        html += '</select>'
            +'</div>'
            +'</div>'
        +       '<botton class="btn btn-primary  addFoxHammerTimes" onclick="addFoxHammerTimes( this)">添加锤子次数</botton>'
        +       '<botton class="btn btn-danger del-tr delFoxHammerTimes" onclick="delFoxHammerTimes( this)">删除锤子次数</botton>'
        +' </div>';
    $(obj).before(html)
    $('.select2').select2({})
    FoxHammerShushuStages[lvIdx]++;
}
function delFoxHammerShushuWrap(obj){
    var lvIdx = $(obj).parents('.FoxHammerLevelWrap').data('idx')
    console.log("delFoxHammerShushuWrap-lvIdx:"+lvIdx+"-shushustage:"+FoxHammerShushuStages[lvIdx] );
    if(FoxHammerShushuStages[lvIdx] < 2)return;
    $(obj).parents('.FoxHammerLevelWrap').find('.foxHammerSecondWrap:last').remove()
    FoxHammerShushuStages[lvIdx]--;

}
function initReward_foxHammer(){
    initFoxHammerWrap();
    var reward_infoNew=[];
    if(reward_info[0]['start']==undefined){//数据兼容
        var newData =[];
        $.each(reward_info, function(i,o){
            newData[i] = {"shushu":o.shushu,"data":o.data};
        });
        reward_infoNew[0] = {"start":1,"end":300,"data":newData};
    }else{
        reward_infoNew = reward_info;
    }
console.log("reward_infoNew:",reward_infoNew);
    $.each(reward_infoNew, function(lvIdx, data1){
        console.log("lvIdx:"+lvIdx+"-data1:",data1);
        //添加等级分层
        $('#addFoxHammerLevelWrapBtn').click();
        $('input[name="foxHammerShuhuConfig['+lvIdx+'][start]"]').val(data1.start);
        $('input[name="foxHammerShuhuConfig['+lvIdx+'][end]"]').val(data1.end);
        $.each(data1.data, function(shushuIdx, o){
            //添加数数分层
            console.log("lvIdx:"+lvIdx+"-shushuIdx:"+shushuIdx)
            $('.addFoxHammerShushuWrapBtn_'+lvIdx).click();
            $('select[name="foxHammerShuhuConfig['+lvIdx+'][data]['+shushuIdx+'][shushu][]"]').val(o.shushu)

            $.each(o.data, function(i1, o1){
                i1 = parseInt(i1+1)
                $('.addFoxHammerTimes:last').click()
                console.log('"foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+i1+'][ton]"');
                $('input[name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+i1+'][ton]"]').val(o1.ton)
                $('input[name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+i1+'][discount]"]').val(o1.discount)
                $('select[name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+i1+'][price]"]').val(o1.price)
                $('input[name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+i1+'][sku]"]').val(o1.sku)
                $.each(o1.prizes, function(i2, o2){
                    i2 = parseInt(i2+1)
                    $('.addFoxHammerReward:last').click()
                    $('input[name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+i1+'][prize]['+i2+'][weight]"]').val(o2.weight)
                    $('input[name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+i1+'][prize]['+i2+'][spins]"]').val(o2.reward.num)
                })
            })
        });
    });
    $('.select2').select2({})
}

function addFoxHammerTimes(obj){
    var lvIdx = $(obj).parents('.FoxHammerLevelWrap').data('idx')
    var shushuIdx = $(obj).parents('.foxHammerSecondWrap').data('idx')

    var  lvShushu= lvIdx +"_"+shushuIdx;

    if(foxHammerShushuArr[lvShushu] == undefined)foxHammerShushuArr[lvShushu] = 0;
    foxHammerShushuArr[lvShushu]++
    var secondIdx = foxHammerShushuArr[lvShushu]
    var thirdIdx = lvShushu + '_' + secondIdx
    console.log("addFoxHammerTimes-shushuIdx:"+thirdIdx);
    var html = ''
    +       '<div class=" foxHammerThirdWrap priceSkuGroup" data-idx="'+secondIdx+'"  style="border: 1px solid black;margin: 5px;padding: 5px">'
    +           '<div class="form-group" data-idx="'+secondIdx+'" >'
    +           '<lable class="col-sm-1" style="text-align: center">锤子吨位:</lable>'
    +           '<input class="col-sm-1" name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+secondIdx+'][ton]">'
    +           '<lable class="col-sm-1" style="text-align: right;">价格：</lable>'
    +           '<select  class="col-sm-2" name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+secondIdx+'][price]"  onchange="priceChanged(this)" >';
    html += '<option hidden value="">选择价格</option>'
    $.each(priceSkuMap, function(i,o){
        html += '<option value="'+i+'">'+i+'</option>'
    })
    html += '</select>'
    +           '<lable class="col-sm-1" style="text-align: right;">sku：</lable>'
    +           '<input readonly class="col-sm-2 skuInput"  name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+secondIdx+'][sku]">'
    +           '<lable class="col-sm-1" style="text-align: right;display: none">折扣：</lable>'
    +           '<input type="number" style="display: none" value="0" class="col-sm-1"  name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+secondIdx+'][discount]">'
    +           '</div>'
    +           '<table class="table table-bordered foxHammerConfig_'+thirdIdx+'">'
    +               '<thead>'
    +                   '<tr>'
    +                       '<th>体力数</th>'
    +                       '<th>权重</th>'
    +                   '</tr>'
    +               '</thead>'
    +               '<tbody class="foxHammerConfigTbody_'+thirdIdx+'">'
    +               '</tbody>'
    +               '<tr>'
    +                   '<td colspan="2">'
    +                       '<div class="text-center">'
    +                          '<botton class="btn btn-primary addFoxHammerReward" onclick="addFoxHammerReward(this)">添加</botton>'
    +                          '<botton class="btn btn-danger delFoxHammerReward" onclick="delFoxHammerReward(this)">删除</botton>'
    +                       '</div>'
    +                   '</td>'
    +               '</tr>'
    +           '</table>'
    +'      </div>'
    $(obj).before(html)

}

function delFoxHammerTimes(obj){
    var lvIdx = $(obj).parents('.FoxHammerLevelWrap').data('idx')
    var shushuIdx = $(obj).parents('.foxHammerSecondWrap').data('idx')

    var currentCnt = lvIdx+'_'+shushuIdx
    if(foxHammerShushuArr[currentCnt]<1)return;
    foxHammerShushuArr[currentCnt]--
    $(obj).parents('.foxHammerSecondWrap').find('.foxHammerThirdWrap:last').remove()
}

function addFoxHammerReward(obj){
    var lvIdx = $(obj).parents('.FoxHammerLevelWrap').data('idx')
    var shushuIdx = $(obj).parents('.foxHammerSecondWrap').data('idx')
    var currentCnt = $(obj).parents('.foxHammerThirdWrap').data('idx')
    var thirdIdx = lvIdx+'_'+shushuIdx + '_' + currentCnt
    console.log('addFoxHammerReward-thirdIdx:'+thirdIdx)
    if(foxHammerRewardArr[thirdIdx] == undefined)foxHammerRewardArr[thirdIdx] = 0;
    foxHammerRewardArr[thirdIdx]++
    var html = ''
    +                   '<tr class="foxHammerRewardTr">'
    +                       '<td><input name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+currentCnt+'][prize]['+foxHammerRewardArr[thirdIdx]+'][spins]"></td>'
    +                       '<td><input name="foxHammerConfig['+lvIdx+'][data]['+shushuIdx+'][data]['+currentCnt+'][prize]['+foxHammerRewardArr[thirdIdx]+'][weight]"></td>'
    +                   '</tr>'
    $(obj).parents('.foxHammerThirdWrap').find('.foxHammerConfigTbody_'+thirdIdx).append(html)
}
function delFoxHammerReward(obj){
    var currentCnt = $(obj).parents('.foxHammerThirdWrap').data('idx')
    var shushuIdx = $(obj).parents('.foxHammerSecondWrap').data('idx')
    var thirdIdx = shushuIdx + '_' + currentCnt
    if(foxHammerRewardArr[thirdIdx]<1)return;
    foxHammerRewardArr[thirdIdx]--
    $(obj).parents('.foxHammerThirdWrap').find('.foxHammerConfigTbody_'+thirdIdx).find('.foxHammerRewardTr:last').remove()
}
function changeTypeTo_foxHammer(){
    initFoxHammerWrap();
}