function initRewardTokenRank(type) {
    console.log('init ' + type)
    getActBasicConfig(type).then(()=>{
        initTokenRankWrap()

        // 基础配置
        if (reward_conf){
            reward_conf.minScore && $(`input[name="tokenRankBasicConfig[minScore]"]`).val(reward_conf.minScore)

            let tokenTypeIdx = 0
            reward_conf.tokens && $.each(reward_conf.tokens, function(key, val){
                addTokenRankScoreSetting()
                $(`select[name="tokenRankBasicConfig[tokens][${tokenTypeIdx}][tokenType]"]`).val(key)
                $(`input[name="tokenRankBasicConfig[tokens][${tokenTypeIdx}][score]"]`).val(val)
                tokenTypeIdx++
            })
        }

        // 奖励配置
        reward_pro && $.each(reward_pro, function (i, o) {
            addTokenRankStages()
            $(`input[name="tokenRankConfig[${i}][lv_start]"]`).val(o['lv_start'])
            $(`input[name="tokenRankConfig[${i}][lv_end]"]`).val(o['lv_end'])
            $(`input[name="tokenRankConfig[${i}][spins_start]"]`).val(o['spins_start'])
            $(`input[name="tokenRankConfig[${i}][spins_end]"]`).val(o['spins_end'])
            $(`select[name="tokenRankConfig[${i}][shushu][]"]`).val(o['shushu'])
            o.stages && $.each(o.stages, function (i1, o1) {
                addTokenRankStageTr(i)
                $(`input[name="tokenRankConfig[${i}][stages][${i1}][stepnum]"]`).val(o1['stepnum'])
                let prizeInput = $(`input[name="tokenRankConfig[${i}][stages][${i1}][prize]"]`)
                prizeInput.val(escape(JSON.stringify(o1['prize'])))
                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o1['prize']))
            })
            o.ranks && $.each(o.ranks, function (i1, o1) {
                addTokenRankRankTr(i)
                $(`input[name="tokenRankConfig[${i}][ranks][${i1}][rankPos]"]`).val(o1['rankPos'])
                let prizeInput = $(`input[name="tokenRankConfig[${i}][ranks][${i1}][prize]"]`)
                prizeInput.val(escape(JSON.stringify(o1['prize'])))
                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o1['prize']))
            })
        })

        // 分房配置
        activity_extra_data && activity_extra_data.roomAllotConf && $.each(activity_extra_data.roomAllotConf, (i, o)=>{
            addTokenRankRoomStages()
            $(`input[name="tokenRankRoomConfig[${i}][lv_start]"]`).val(o.lvStart)
            $(`input[name="tokenRankRoomConfig[${i}][lv_end]"]`).val(o.lvEnd)
            o.stages && $.each(o.stages, (i1, o1)=>{
                addTokenRankRoomStageTr(i)
                $(`input[name="tokenRankRoomConfig[${i}][stages][${i1}][spinsStart]"]`).val(o1.spinsStart)
                $(`input[name="tokenRankRoomConfig[${i}][stages][${i1}][spinsEnd]"]`).val(o1.spinsEnd)
                $(`input[name="tokenRankRoomConfig[${i}][stages][${i1}][numLimit]"]`).val(o1.numLimit)

            })
        })
        $('.select2').select2({})
    })
}

function changeTypeToTokenRank(type) {
    getActBasicConfig(type).then(()=>{
        initTokenRankWrap()
    })

}

function initTokenRankWrap() {
    $('#reward_info').empty()
    let html = `
        <table class="table table-bordered " style="margin-bottom: 0">
            <thead>
                <th>上榜门槛积分</th>
                <th><input name="tokenRankBasicConfig[minScore]"></th>
            </thead>
            
        </table>
            
        <table class="table table-bordered ">
            <thead>
                <th>代币类型</th>
                <th>获取积分</th>
            </thead>
            <tbody id="tokenRankScoreTbl">
            </tbody>
            <tr>
                <td colspan="3" class="text-center">
                    <button type="button" class="btn btn-outline btn-success addTokenRankScoreSetting" onclick="addTokenRankScoreSetting()">添加</button>
                    <button type="button" class="btn btn-outline btn-danger  delTokenRankScoreSetting" onclick="delTokenRankScoreSetting()">删除</button>
                </td>
            </tr>
        </table>
        
    <h3>奖励配置</h3>
        <div class="form-group text-center " id="act-stage-button">
            <div class=" ">
                <button class="btn btn-primary" id="addTokenRankStageWrapBtn" onclick="addTokenRankStages()" type="button">添加分层区间</button>
                <button class="btn btn-white"   id="delTokenRankStageWrapBtn" onclick="delTokenRankStages()" type="button">删除分层区间</button>
            </div>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
        
    <h3>分房间配置</h3>
        <div class="form-group text-center " id="act-room-stage-button">
            <div class="operationWrap">
                <button class="btn btn-primary" id="addTokenRankStageWrapBtn" onclick="addTokenRankRoomStages()" type="button">添加分层区间</button>
                <button class="btn btn-white"   id="delTokenRankStageWrapBtn" onclick="delTokenRankRoomStages()" type="button">删除分层区间</button>
            </div>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
    $("#reward_info").append(html);
}

function addTokenRankScoreSetting(){
    console.log(actBasicConf);
    let optionsHtml = getOptionsHtml(actBasicConf[act_type]['tokenMap'])
    let i = $('#tokenRankScoreTbl').find('tr').length
    $('#tokenRankScoreTbl').append(`
        <tr>
            <td>
                <select class="form-control" name="tokenRankBasicConfig[tokens][${i}][tokenType]">
                    ${optionsHtml}
                </select>
            </td>
            <td>
                <input class="form-control" name="tokenRankBasicConfig[tokens][${i}][score]">
            </td>
        </tr>
    `)

}
function delTokenRankScoreSetting(){
    $('#tokenRankScoreTbl').find('tr:last').remove()
}

// 分层配置
function addTokenRankStages() {
    let stageIdx = $('.tokenRankStageWrap').length
    let stageConditionTbl = getLvSpinsShushuStageWrapTable('tokenRankConfig', stageIdx)
    let html = `
        <div class="form-group tokenRankStageWrap " id="tokenRankLvWrap_${stageIdx}" data-idx="${stageIdx}" style="margin:10px;padding:10px;border:solid 1px lightgray;">
            ${stageConditionTbl}
               
            <hr style="border:solid 1px black;">
            <h3>阶段配置</h3>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>阶段</td>
                        <td>阶段值</td>
                        <td>奖励</td>
                        <td>操作</td>
                    </tr>   
                </thead>
                <tbody id="tokenRankStageWrap${stageIdx}"> </tbody>
                <tr>
                    <td colspan="13">
                        <div class="text-center">
                            <botton class="btn btn-primary " onclick="addTokenRankStageTr(${stageIdx})">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delTokenRankStageTr(${stageIdx})">删除</botton> 
                        </div>
                    </td>
                </tr>
            
            </table>
            
            <hr style="border:solid 1px black;">
            <h3>排行配置</h3>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>序号</td>
                        <td>排名</td>
                        <td>奖励</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tbody id="tokenRankRankWrap${stageIdx}"> </tbody>
                
                <tr>
                    <td colspan="5">
                        <div class="text-center">
                            <botton class="btn btn-primary addSurpriseCollectRewardTrBtn" onclick="addTokenRankRankTr(${stageIdx})">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delTokenRankRankTr(${stageIdx})">删除</botton> 
                        </div>
                    </td>
                </tr>
            
            </table>
        </div>
`

    $('#act-stage-button').before(html)
    $('.select2').select2({})
}
function delTokenRankStages(){
    $('.tokenRankStageWrap:last').remove()
}


// 阶段配置
addTokenRankStageTr = (stageIdx)=>{
    let rewardDft = encodeURI( '[]');
    let rewardIdx = $(`#tokenRankStageWrap${stageIdx}`).find('tr').length
    let trHtml = `
            <tr class="commonStageRewardTr">
                <td><input class="form-control" name="tokenRankConfig[${stageIdx}][stages][${rewardIdx}][stage]" value="${rewardIdx+1}" readonly></td>
                <td><input class="form-control" name="tokenRankConfig[${stageIdx}][stages][${rewardIdx}][stepnum]"></td>
                <td class="prizeShow"></td>
                <td>
                    <input type="hidden" class="prizeInput" name="tokenRankConfig[${stageIdx}][stages][${rewardIdx}][prize]" value="${rewardDft}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td>
            </tr>
`
    $(`#tokenRankStageWrap${stageIdx}`).append(trHtml)
}
delTokenRankStageTr = (stageIdx)=>{
    $(`#tokenRankStageWrap${stageIdx}`).find('tr:last').remove()
}


// 排行配置
addTokenRankRankTr = (stageIdx) => {
    let rewardIdx = $(`#tokenRankRankWrap${stageIdx}`).find('tr').length
    let rewardDft = encodeURI( '[]');
    let trHtml = `
            <tr class="commonStageRewardTr">
                <td><input class="form-control" name="tokenRankConfig[${stageIdx}][ranks][${rewardIdx}][stage]" value="${rewardIdx+1}" readonly></td>
                <td><input class="form-control" name="tokenRankConfig[${stageIdx}][ranks][${rewardIdx}][rankPos]"></td>
                <td class="prizeShow"></td>
                <td>
                    <input type="hidden" class="prizeInput" name="tokenRankConfig[${stageIdx}][ranks][${rewardIdx}][prize]" value="${rewardDft}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td>
            </tr>
        `
    $(`#tokenRankRankWrap${stageIdx}`).append(trHtml)
}
delTokenRankRankTr = (stageIdx) => {
    $(`#tokenRankRankWrap${stageIdx}`).find('tr:last').remove()
}


// 分房间配置
function addTokenRankRoomStages() {
    let stageIdx = $('.tokenRankRoomStageWrap').length
    let stageConditionTbl = getLvStageWrapTable('tokenRankRoomConfig', stageIdx)
    let html = `
        <div class="form-group tokenRankRoomStageWrap " id="tokenRankRoomLvWrap_${stageIdx}" data-idx="${stageIdx}" style="margin:10px;padding:10px;border:solid 1px lightgray;">
            ${stageConditionTbl}
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>体力下限</td>
                        <td>体力上限</td>
                        <td>房间人数</td>
                    </tr>   
                </thead>
                <tbody id="tokenRankRoomStageWrap${stageIdx}"> </tbody>
                <tr>
                    <td colspan="13">
                        <div class="text-center">
                            <botton class="btn btn-primary " onclick="addTokenRankRoomStageTr(${stageIdx})">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delTokenRankRoomStageTr(${stageIdx})">删除</botton> 
                        </div>
                    </td>
                </tr>
            </table>
            
        </div>
`

    $('#act-room-stage-button').find('.operationWrap').before(html)
    $('.select2').select2({})
}
function delTokenRankRoomStages(){
    $('.tokenRankRoomStageWrap:last').remove()
}

// 分房间阶段配置
addTokenRankRoomStageTr = (stageIdx)=>{
    let rewardIdx = $(`#tokenRankRoomStageWrap${stageIdx}`).find('tr').length
    let trHtml = `
            <tr>
                <td><input class="form-control" name="tokenRankRoomConfig[${stageIdx}][stages][${rewardIdx}][spinsStart]"></td>
                <td><input class="form-control" name="tokenRankRoomConfig[${stageIdx}][stages][${rewardIdx}][spinsEnd]"></td>
                <td><input class="form-control" name="tokenRankRoomConfig[${stageIdx}][stages][${rewardIdx}][numLimit]"></td>
            </tr>
`
    $(`#tokenRankRoomStageWrap${stageIdx}`).append(trHtml)
}
delTokenRankRoomStageTr = (stageIdx)=>{
    $(`#tokenRankRoomStageWrap${stageIdx}`).find('tr:last').remove()
}
