function initRewardRunningRabbit(type) {
    console.log('init ' + type)
    changetypeToRunningRabbit()

    // 奖励回写
    console.log('reward_info ', reward_info)
    if (!reward_info) {
        return
    }

    $.each(reward_info, function (lvStage, lvItem) {
        $('#addRunningRabbitLvWrapBtn').click()
        $('input[name="runningRabbitConfig[' + lvStage + '][lvStart]"]').val(lvItem.lvStart)
        $('input[name="runningRabbitConfig[' + lvStage + '][lvEnd]"]').val(lvItem.lvEnd)


        $(`#RunningRabbitLvWrap_${lvStage}`).find('botton').remove()
        $(`#RunningRabbitLvWrap_${lvStage}`).append(`<button class="btn btn-primary asyncLoadConfigBtn" style="" type="button" onclick="asyncLoadRunningRabbitConfig(${lvStage})">加载配置</button>`)

    })
    $('#submitBtn').hide();
}

function changetypeToRunningRabbit() {
    $("#reward_info").empty();
    initRunningRabbitWrap();
}

function initRunningRabbitWrap() {
    RunningRabbitLvCnt = 0;
    RunningRabbitSpinsStages = {}
    RunningRabbitLevelsStages = {}
    RunningRabbitRewardStages = {}

    var html = RunningRabbitWrapStageTable();
    $("#reward_info").append(html);
}

function RunningRabbitWrapStageTable() {
    return `
         <div class="text-left" style="margin:5px;padding:5px;border:solid 1px lightgray;">
            <button style="margin-top: 20px" id="addRunningRabbitLvWrapBtn" class="btn btn-primary" onclick="addRunningRabbitLvWrap(this)" type="button">添加等级区间</button>
            <button style="margin-top: 20px" class="btn btn-danger" onclick="delRunningRabbitLvWrap()" type="button">删除等级区间</button>
         </div>`
}

// lvs 等级区间
function addRunningRabbitLvWrap(obj) {
    RunningRabbitLvCnt++
    RunningRabbitSpinsStages[RunningRabbitLvCnt] = 0
    var html = `
        <div class="form-group text-left RunningRabbitLvWrap" id="RunningRabbitLvWrap_${RunningRabbitLvCnt}" data-idx="${RunningRabbitLvCnt}" style="margin:10px;padding:5px;border:solid 1px black;">
        
            <div class="form-group">
                <label class="col-sm-1 control-label m-b">等级区间: </label>
                <div class="col-sm-3 input-group">
                    <input type="text" style="text-align: center" class="form-control" name="runningRabbitConfig[${RunningRabbitLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="runningRabbitConfig[${RunningRabbitLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton style="margin-top: 10px" class="btn btn-primary  addRunningRabbitSpinsWrapBtn" onclick="addRunningRabbitSpinsWrap( this)">添加体力分组层级</botton>
            <botton style="margin-top: 10px" class="btn btn-danger del-tr delRunningRabbitSpinsWrapBtn" onclick="delRunningRabbitSpinsWrap( this)">删除体力分组层级</botton>
        </div>`
    $(obj).before(html)
}

function delRunningRabbitLvWrap() {
    if (RunningRabbitLvCnt < 1) {
        return;
    }
    $('#RunningRabbitLvWrap_' + RunningRabbitLvCnt).remove();
    RunningRabbitLvCnt--;
}

// spins 体力区间
function addRunningRabbitSpinsWrap(obj) {
    let lvIdx = $(obj).parents('.RunningRabbitLvWrap').data('idx')
    RunningRabbitSpinsStages[lvIdx]++
    let rewardIdx = `${lvIdx}_${RunningRabbitSpinsStages[lvIdx]}`
    RunningRabbitLevelsStages[rewardIdx] = 0
    let html = `
        <div style="margin:10px;padding:5px;border:solid 1px goldenrod;" class="RunningRabbitSpinsWrap "  id="RunningRabbitSpinsWrap_${rewardIdx}" data-idx="${RunningRabbitSpinsStages[lvIdx]}">
            <table class="table table-bordered ">
                <thead>
                    <tr>
                        <td colspan="7">
                             <div class="form-group">
                                <label class="col-sm-2 control-label m-b">体力区间: </label>
                                <div class="col-sm-3 input-group">
                                    <input type="text" style="text-align: center" class="form-control" name="runningRabbitConfig[${lvIdx}][stages][${RunningRabbitSpinsStages[lvIdx]}][spinsStart]" value="0">
                                    <span class="input-group-addon">-</span>
                                    <input type="text" style="text-align: center" class="form-control" name="runningRabbitConfig[${lvIdx}][stages][${RunningRabbitSpinsStages[lvIdx]}][spinsEnd]" value="0">
                                </div>
                            </div>
                        </td>
                    </tr>
                    
                </thead>
                <tbody id="RunningRabbitRewardBody_${rewardIdx}"></tbody>
                <tr>
                    <td colspan="7">
                        <div class="text-center" style="margin-top: 20px">
                            <botton class="btn btn-primary addRunningRabbitLevelsBtn" onclick="addRunningRabbitLevelsWrap(this)">添加关卡</botton>
                            <botton class="btn btn-danger del-tr" onclick="delRunningRabbitLevelsWrap(this)">删除关卡</botton>
                        </div>
                    </td>
                </tr>
           </table>
        </div>`
    $(obj).before(html)


    $(`#RunningRabbitSpinsWrap_${rewardIdx}`).append(addRunningRabbitProgressWrap(lvIdx, RunningRabbitSpinsStages[lvIdx]))
    $(`#RunningRabbitSpinsWrap_${rewardIdx}`).append(noTokenAlert(rewardIdx, 144))
}

function delRunningRabbitSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.RunningRabbitLvWrap').data('idx')
    if (RunningRabbitSpinsStages[lvIdx] < 1) return
    $(obj).parents('.RunningRabbitLvWrap').find('.RunningRabbitSpinsWrap:last').remove()
    RunningRabbitSpinsStages[lvIdx]--
}

// levels 关卡
function addRunningRabbitLevelsWrap(obj) {
    let lvIdx = $(obj).parents('.RunningRabbitLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.RunningRabbitSpinsWrap').data('idx')
    let levelsKey = lvIdx + '_' + spinsIdx
    RunningRabbitLevelsStages[levelsKey]++

    let rewardKey = `${levelsKey}_${RunningRabbitLevelsStages[levelsKey]}`
    RunningRabbitRewardStages[rewardKey] = 0
    let html = `
        <div style="margin:10px;padding:5px;border:solid 1px goldenrod;" id="RunningRabbitLevelsWrap_${levelsKey}_${RunningRabbitLevelsStages[levelsKey]}" class="RunningRabbitLevelsWrap"  data-idx="${RunningRabbitLevelsStages[levelsKey]}">
            <table class="table table-bordered" >
                <thead>
                    <tr>
                        <td colspan="9">
                             <div class="form-group">
                                <label class="col-sm-2 control-label m-b">关卡: </label>
                                <div class="col-sm-3 input-group">
                                    <input type="text" style="text-align: center" class="form-control"  name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][levels][${RunningRabbitLevelsStages[levelsKey]}][level]" readonly value="${RunningRabbitLevelsStages[levelsKey]}">
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <th width="120">Index</th>
                        <th>格数</th>
                        <th>层级</th>
                        <th>权重</th>
                        <th width="100">是否大奖</th>
                        <th>出奖所需代币</th>
                        <th>奖励</th>
                        <th width="120">奖励设置</th>
                    </tr>
                </thead>
                <tbody id="RunningRabbitRewardBody_${rewardKey}"></tbody>
                <tr>
                    <td colspan="7">
                        <div class="text-center" style="margin-top: 20px">
                            <botton class="btn btn-primary addRunningRabbitRewardTrBtn" onclick="addRunningRabbitRewardTr(this)">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delRunningRabbitRewardTr(this)">删除</botton>
                        </div>
                    </td>
                </tr>
           </table>
        </div>`
    $(obj).before(html)
}

function delRunningRabbitLevelsWrap(obj) {
    var lvIdx = $(obj).parents('.RunningRabbitLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.RunningRabbitSpinsWrap').data('idx')
    let levelKey = `${lvIdx}_${spinsIdx}`
    if (RunningRabbitLevelsStages[levelKey] < 1) return
    let levelIdx = `${lvIdx}_${spinsIdx}_${RunningRabbitLevelsStages[levelKey]}`
    console.log($(`#RunningRabbitLevelsWrap_${levelIdx}`));
    $(`#RunningRabbitLevelsWrap_${levelIdx}`).remove()
    RunningRabbitLevelsStages[levelKey]--
}


// grids 格子
function addRunningRabbitRewardTr(obj) {
    let lvIdx = $(obj).parents('.RunningRabbitLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.RunningRabbitSpinsWrap').data('idx')
    let levelsIdx = $(obj).parents('.RunningRabbitLevelsWrap').data('idx')
    let levelKey = `${lvIdx}_${spinsIdx}`
    let rewardKey = `${lvIdx}_${spinsIdx}_${levelsIdx}`
    let rewardStage = ++RunningRabbitRewardStages[rewardKey]
    console.log(rewardStage);
    let prize = '[]';
    prize = escape(prize);
    let html = `
        <tr class="RunningRabbitRewardInfoTr commonStageRewardTr" id="RunningRabbitRewardInfo_${rewardKey}_${rewardStage}" data-idx=" ${rewardStage} ">
            <td width="120"><input type="text"  class="form-control" value="${rewardStage}" readonly ></td>
            <td><input name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][levels][${levelsIdx}][grids][${rewardStage-1}][grid]" type="text"  class="form-control" value="0"></td>
            <td><input name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][levels][${levelsIdx}][grids][${rewardStage-1}][layer]" type="text"  class="form-control" value="0"></td>
            <td><input name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][levels][${levelsIdx}][grids][${rewardStage-1}][weight]" type="text"  class="form-control" value="0"></td>
            <td><select class="form-control" name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][levels][${levelsIdx}][grids][${rewardStage-1}][bonus]">
                        <option value="1">是</option>
                        <option value="0">否</option>
                </select>
            </td>
            <td><input name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][levels][${levelsIdx}][grids][${rewardStage-1}][spendLowest]" type="text"  class="form-control" value="0"></td>
            <td class="prizeShow"></td>
            <td>
                <input type="hidden" class="prizeInput" name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][levels][${levelsIdx}][grids][${rewardStage-1}][prize]" value="${prize}">
                <input type="hidden" class="prizeInput" name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][levels][${levelsIdx}][grids][${rewardStage-1}][color]" value="1">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
            </td>
        </tr>`;

    $('#RunningRabbitRewardBody_' + rewardKey).append(html)
}

function delRunningRabbitRewardTr(obj) {
    var lvIdx = $(obj).parents('.RunningRabbitLvWrap').data('idx')
    var SpinsIdx = $(obj).parents('.RunningRabbitSpinsWrap').data('idx')
    var levelIdx = $(obj).parents('.RunningRabbitLevelsWrap').data('idx')
    var rewardKey = `${lvIdx}_${SpinsIdx}_${levelIdx}`
    var stageKey = `${lvIdx}_${SpinsIdx}_${levelIdx}_${RunningRabbitRewardStages[rewardKey]}`
    if (RunningRabbitRewardStages[rewardKey] < 1) return;
    console.log($('#RunningRabbitRewardInfo_' + stageKey));
    $('#RunningRabbitRewardInfo_' + stageKey).remove()
    RunningRabbitRewardStages[rewardKey]--
}


// 阶段将
addRunningRabbitProgressWrap = (lvIdx, spinsIdx, type = 0) => {
    let rewardKey = `${lvIdx}_${spinsIdx}_${type}`
    let html = `
     <table class="table table-bordered RunningRabbitProgress" id="RunningRabbitProgressWrap_${rewardKey}" >
                <thead>
                    <tr>
                        <td>阶段</td>
                        <td>阶段值</td>
                        <td>奖励</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tbody id="RunningRabbitProgressBody_${rewardKey}"></tbody>
                <tr>
                    <td colspan="7">
                        <div class="text-center" style="margin-top: 20px">
                        
                            <input type="hidden" class="prizeInput" name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][progress][${type}][color]" value="${type + 1}">
                            <botton class="btn btn-primary addRunningRabbitProgressBtn" onclick="addRunningRabbitProgressTr(${lvIdx}, ${spinsIdx}, ${type})">添加阶段</botton>
                            <botton class="btn btn-danger del-tr" onclick="delRunningRabbitProgressTr(${lvIdx}, ${spinsIdx}, ${type})">删除阶段</botton>
                        </div>
                    </td>
                </tr>
           </table>
`
    return html
}
addRunningRabbitProgressTr = (lvIdx, spinsIdx, type) => {

    let rewardKey = `${lvIdx}_${spinsIdx}_${type}`
    let length = $(`#RunningRabbitProgressBody_${rewardKey}`).find('.commonStageRewardTr').length
    let prize = '[]';
    prize = escape(prize);
    let html = `
        <tr class="RunningRabbitProgressInfoTr commonStageRewardTr" id="RunningRabbitProgressInfoTr_${rewardKey}_${length}" >
            <td><input name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][progress][${type}][config][${length}][stage]" type="text"  class="form-control" value="${length+1}" readonly ></td>
            <td><input name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][progress][${type}][config][${length}][target]" type="text"  class="form-control" value="0"></td>
            <td class="prizeShow"></td>
            <td>
                <input type="hidden" class="prizeInput" name="runningRabbitConfig[${lvIdx}][stages][${spinsIdx}][stages][progress][${type}][config][${length}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
            </td>
        </tr>`;

    $('#RunningRabbitProgressBody_' + rewardKey).append(html)
}
delRunningRabbitProgressTr = (lvIdx, spinsIdx, type) => {

    let rewardKey = `${lvIdx}_${spinsIdx}_${type}`
    $(`#RunningRabbitProgressBody_${rewardKey}`).find('tr:last').remove()
}


asyncLoadRunningRabbitConfig = (lvStage)=>{

    $(`#RunningRabbitLvWrap_${lvStage}`).find('.asyncLoadConfigBtn').remove()
    $(`#RunningRabbitLvWrap_${lvStage}`).append(`
            <botton style="margin-top: 10px" class="btn btn-primary  addRunningRabbitSpinsWrapBtn" onclick="addRunningRabbitSpinsWrap( this)">添加体力分组层级</botton>
            <botton style="margin-top: 10px" class="btn btn-danger del-tr delRunningRabbitSpinsWrapBtn" onclick="delRunningRabbitSpinsWrap( this)">删除体力分组层级</botton>
    `)

    let lvItem = reward_info[lvStage]
    $.each(lvItem.stages, function (spinsStage, spinsItem) {
        $(`#RunningRabbitLvWrap_${lvStage}`).find('.addRunningRabbitSpinsWrapBtn').click()

        $('input[name="runningRabbitConfig[' + lvStage + '][stages][' + spinsStage + '][spinsStart]"]').val(spinsItem.spinsStart)
        $('input[name="runningRabbitConfig[' + lvStage + '][stages][' + spinsStage + '][spinsEnd]"]').val(spinsItem.spinsEnd)

        spinsItem.stages && spinsItem.stages.levels && $.each(spinsItem.stages.levels, function (levelIdx, grids) {
            $(`#RunningRabbitSpinsWrap_${lvStage}_${spinsStage}`).find('.addRunningRabbitLevelsBtn').click()
            let rewardIndex = levelIdx + 1
            console.log(grids);
            grids && $.each(grids, function (gidx, layer) {
                if (!layer.prize)layer.prize = [];
                $(`#RunningRabbitSpinsWrap_${lvStage}_${spinsStage}`).find('.addRunningRabbitRewardTrBtn:last').click()
                console.log($(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][levels][${rewardIndex}][grids][${gidx}][prize]"]`));
                let prizeInput = $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][levels][${rewardIndex}][grids][${gidx}][prize]"]`)
                $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][levels][${rewardIndex}][grids][${gidx}][grid]"]`).val(layer.grid+1)
                $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][levels][${rewardIndex}][grids][${gidx}][layer]"]`).val(layer.layer+1)
                $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][levels][${rewardIndex}][grids][${gidx}][weight]"]`).val(layer.weight)
                $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][levels][${rewardIndex}][grids][${gidx}][spendLowest]"]`).val(layer.spendLowest)
                $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][levels][${rewardIndex}][grids][${gidx}][color]"]`).val(layer.color)
                $(`select[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][levels][${rewardIndex}][grids][${gidx}][bonus]"]`).val(layer.bonus)
                prizeInput.val(escape(JSON.stringify(layer.prize)))
                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(layer.prize))
            })
        })

        spinsItem.stages && spinsItem.stages.progress &&  $.each(spinsItem.stages.progress, function (progressType, progressList) {
            addRunningRabbitProgressWrap(lvStage, spinsStage, progressType)
            progressList.config && $.each(progressList.config, function (progressIdx, progressItem) {
                console.log(progressItem);
                addRunningRabbitProgressTr(lvStage, spinsStage, progressType)
                let prizeInput = $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][progress][${progressType}][config][${progressIdx}][prize]"]`)
                $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][progress][${progressType}][config][${progressIdx}][stage]"]`).val(progressItem.stage)
                $(`input[name="runningRabbitConfig[${lvStage}][stages][${spinsStage}][stages][progress][${progressType}][config][${progressIdx}][target]"]`).val(progressItem.target)
                prizeInput.val(escape(JSON.stringify(progressItem.prize)))
                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(progressItem.prize))
            })
        })

        renderNotokenAlert(`${lvStage}_${spinsStage}`, activity_extra_data)
    })
    updateSubmitBtnStatus()
}