let coinPushManiaLvStagesCnt = 0;
let coinPushManiaSpinStagesCnt = {};
let coinPushManiaCollectCnt = {};   // 收集
let coinPushManiaStagesCnt = {};    // 关卡
let coinPushManiaProgressCnt = {};  // 阶段
let coinIdx = 0;    // 全局代币id

function renderRewardCoinPushMania() {
    initCoinPushManiaWrap()
    reward_conf && $(`input[name="coinPushManiaExtra[collect_base_num]"]`).val(reward_conf.collect_base_num)
    activity_extra_data && $.each(activity_extra_data, function (i, o) {
        addCoinPushManiaLvStageWrap()

        $(`input[name="coinPushManiaReward[${i}][lvStart]"]`).val(o.lvStart)
        $(`input[name="coinPushManiaReward[${i}][lvEnd]"]`).val(o.lvEnd)
        o.stages && $.each(o.stages, function (i1, o1) {
            addCoinPushManiaSpinStageWrap(i)
            $(`input[name="coinPushManiaReward[${i}][stages][${i1}][spinsStart]"]`).val(o1.spinsStart)
            $(`input[name="coinPushManiaReward[${i}][stages][${i1}][spinsEnd]"]`).val(o1.spinsEnd)

            o1.tokenCollectConfig && $.each(o1.tokenCollectConfig, function (i2, o2) {
                addCoinPushManiaCollectStage(i, i1)
                $(`input[name="coinPushManiaReward[${i}][stages][${i1}][tokenCollectConfig][${i2}][target]"]`).val(o2.target)
                $(`input[name="coinPushManiaReward[${i}][stages][${i1}][tokenCollectConfig][${i2}][value]"]`).val(o2.value)
            })
            o1.rewardConfig && $.each(o1.rewardConfig, function (i2, o2) {
                addCoinPushManiaStage(i, i1)

                o2.coins && $.each(o2.coins, function (i3, o3) {
                    addCoinPushManiaStageRewardTr(i, i1, i2)
                    let bonus = o3.bonus ? 1 : 0
                    $(`select[name="coinPushManiaReward[${i}][stages][${i1}][rewardConfig][${i2}][coins][${i3}][coinSize]"]`).val(o3.coinSize)
                    $(`select[name="coinPushManiaReward[${i}][stages][${i1}][rewardConfig][${i2}][coins][${i3}][bonus]"]`).val(bonus)
                    $(`input[name="coinPushManiaReward[${i}][stages][${i1}][rewardConfig][${i2}][coins][${i3}][coinUsedLimit]"]`).val(o3.coinUsedLimit)
                    $(`input[name="coinPushManiaReward[${i}][stages][${i1}][rewardConfig][${i2}][coins][${i3}][prizes]"]`).val(escape(JSON.stringify(o3.prizes)))
                    $(`input[name="coinPushManiaReward[${i}][stages][${i1}][rewardConfig][${i2}][coins][${i3}][prizes]"]`).parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o3.prizes))
                })
            })

            o1.tokenStageConfig && $.each(o1.tokenStageConfig, function (i2, o2) {
                addCoinPushManiaProgress(i, i1)
                console.log($(`input[name="coinPushManiaReward[${i}][stages][${i1}][tokenStageConfig][${i2}][target]"]`), o2);
                $(`input[name="coinPushManiaReward[${i}][stages][${i1}][tokenStageConfig][${i2}][target]"]`).val(o2.target)
                $(`input[name="coinPushManiaReward[${i}][stages][${i1}][tokenStageConfig][${i2}][prizes]"]`).val(escape(JSON.stringify(o2.prizes)))
                $(`input[name="coinPushManiaReward[${i}][stages][${i1}][tokenStageConfig][${i2}][prizes]"]`).parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o2.prizes))
            })
        })
    })
}

function changeTypeToCoinPushMania() {
    initCoinPushManiaWrap()
}

function initCoinPushManiaWrap() {
    $('#reward_info').empty();
    coinPushManiaBasicConfig();
}

// 基础配置
function coinPushManiaBasicConfig() {
    coinPushManiaLvStagesCnt = 0;
    coinPushManiaSpinStagesCnt = {};
    coinPushManiaCollectCnt = {};
    coinPushManiaStagesCnt = {};
    coinPushManiaProgressCnt = {};

    let html = `
            <div class="form-inline">
                <label>收集物基础值：</label>
                <input class="form-control" name="coinPushManiaExtra[collect_base_num]" value="0" >
            </div>
            <div class="coinPushManiaWrap" data-idx="${coinPushManiaStagesCnt}" style="top:10px">
               
                <div style="top:10px;margin: 10px;">
                    <div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">层级配置</h5></div>
                </div>
                <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
            </div>
            <div class="form-group text-center">
                <div class=" ">
                    <button class="btn btn-primary" id="addCoinPushManiaLvStageWrapBtn" onclick="addCoinPushManiaLvStageWrap()" type="button">添加等级层级</button>
                    <button class="btn btn-white"   id="delCoinPushManiaLvStageWrapBtn" onclick="delCoinPushManiaLvStageWrap()" type="button">删除等级层级</button>
                </div>
            </div>
    `;

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


// 等级层
function addCoinPushManiaLvStageWrap() {
    coinPushManiaLvStagesCnt++;
    coinPushManiaSpinStagesCnt[coinPushManiaLvStagesCnt] = 0

    let html = `
        <div class="form-group coinPushManiaLvStageWrap " id="coinPushManiaLvStageWrap_${coinPushManiaLvStagesCnt}" data-idx="${coinPushManiaLvStagesCnt}" style="margin:10px;padding:10px;border:solid 1px lightgray;">
            <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="coinPushManiaReward[${coinPushManiaLvStagesCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="coinPushManiaReward[${coinPushManiaLvStagesCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;" class="op-hr">
            <botton class="btn btn-primary addCoinPushManiaSpinStageWrapBtn" onclick="addCoinPushManiaSpinStageWrap(${coinPushManiaLvStagesCnt})">添加体力层级</botton>
            <botton class="btn btn-danger del-tr delCoinPushManiaSpinStageWrapBtn" onclick="delCoinPushManiaSpinStageWrap(${coinPushManiaLvStagesCnt})">删除体力层级</botton>
        </div>
`
    $(`.coinPushManiaWrap`).append(html)
}
function delCoinPushManiaLvStageWrap() {
    if (coinPushManiaLvStagesCnt > 0) {
        $('#coinPushManiaLvStageWrap_' + coinPushManiaLvStagesCnt).remove()
        coinPushManiaLvStagesCnt--
    }
}

// 体力层
function addCoinPushManiaSpinStageWrap(lvIdx) {
    let spinsIdx = ++coinPushManiaSpinStagesCnt[lvIdx]
    let key3 = `${lvIdx}_${spinsIdx}`
    coinPushManiaCollectCnt[key3] = 0;   // 收集
    coinPushManiaStagesCnt[key3] = 0;    // 关卡
    coinPushManiaProgressCnt[key3] = 0;  // 阶段

    let html = `
        <div class="form-group coinPushManiaSpinStageWrap " id="coinPushManiaSpinStageWrap_${key3}" data-idx="${key3}" style="margin:10px;padding:10px;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="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][spinsStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][spinsEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <label class="col-sm-1 control-label m-b">代币收集配置: </label>
            <table class="coinPushManiaRewardConfigTable table table-bordered"  id=""  style="margin:10px;padding:5px;border:solid 1px goldenrod;">
                <thead>
                    <tr>
                        <td >阶段</td>
                        <td >目标值</td>
                        <td >代币数量</td>
                    </tr>
                </thead>
                <tbody class="coinPushManiaCollectWrap">
                </tbody>
                <tr>
                    <td colspan="3">
                        <botton class="btn btn-primary addCoinPushManiaCollectStageBtn" onclick="addCoinPushManiaCollectStage(${lvIdx}, ${spinsIdx})">添加收集层</botton>
                        <botton class="btn btn-danger del-tr delCoinPushManiaCollectStageBtn" onclick="delCoinPushManiaCollectStage(${lvIdx}, ${spinsIdx})">删除收集层</botton>
                    </td>
                </tr>
            </table>
            <hr style="border:solid 1px black;">
            <label class="col-sm-1 control-label m-b">关卡配置: </label>
            <table class=" table table-bordered"  id=""  style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            </table>
            <div class="coinPushManiaStageBtnTr">
                <div >
                    <botton class="btn btn-primary addCoinPushManiaStageBtn" onclick="addCoinPushManiaStage(${lvIdx}, ${spinsIdx})">添加关卡配置</botton>
                    <botton class="btn btn-danger del-tr delCoinPushManiaStageBtn" onclick="delCoinPushManiaStage(${lvIdx}, ${spinsIdx})">删除关卡配置</botton>
                </div>
            </div>
            
            <hr style="border:solid 1px black;">
            <label class="col-sm-1 control-label m-b">阶段奖励配置: </label>
            <table class="coinPushManiaRewardConfigTable table table-bordered"  id=""  style="margin:10px;padding:5px;border:solid 1px goldenrod;">
                <thead>
                    <tr>
                        <td >阶段</td>
                        <td >目标值</td>
                        <td >奖励</td>
                        <td >操作</td>
                    </tr>
                </thead>
                <tbody class="coinPushManiaProgressWrap">
                </tbody>
                <tr>
                    <td colspan="4">
                        <botton class="btn btn-primary addCoinPushManiaProgressBtn" onclick="addCoinPushManiaProgress(${lvIdx}, ${spinsIdx})">添加阶段配置</botton>
                        <botton class="btn btn-danger del-tr delCoinPushManiaProgressBtn" onclick="delCoinPushManiaProgress(${lvIdx}, ${spinsIdx})">删除阶段配置</botton>
                    </td>
                </tr>
            </table>
            
        </div>
    `;

    $(`#coinPushManiaLvStageWrap_${coinPushManiaLvStagesCnt}`).find('.op-hr').before(html)
}
function delCoinPushManiaSpinStageWrap(lvIdx) {
    console.log(lvIdx)
    console.log(coinPushManiaSpinStagesCnt[lvIdx])
    if($(`#coinPushManiaLvStageWrap_${coinPushManiaLvStagesCnt}`).find('.coinPushManiaSpinStageWrap').length >0){
        $(`#coinPushManiaLvStageWrap_${coinPushManiaLvStagesCnt}`).find('.coinPushManiaSpinStageWrap:last').remove()
        coinPushManiaSpinStagesCnt[lvIdx]--
    }
}

// 收集配置
function addCoinPushManiaCollectStage(lvIdx, spinsIdx) {
    let key3 = `${lvIdx}_${spinsIdx}`
    let collectIdx = coinPushManiaCollectCnt[key3]++

    let html = `
            <tr class="commonStageRewardTr">
                <td ><input  class="form-control" name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][tokenCollectConfig][${collectIdx}][id]" value="${collectIdx+1}" readonly></td>
                <td ><input  class="form-control" name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][tokenCollectConfig][${collectIdx}][target]" value="0"></td>
                <td ><input  class="form-control" name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][tokenCollectConfig][${collectIdx}][value]" value="0"></td>
            </tr>`

    $(`#coinPushManiaSpinStageWrap_${key3}`).find('.coinPushManiaCollectWrap').append(html)
}
function delCoinPushManiaCollectStage(lvIdx, spinsIdx) {
    let key3 = `${lvIdx}_${spinsIdx}`
    let collectWrap = $(`#coinPushManiaSpinStageWrap_${key3}`).find('.coinPushManiaCollectWrap')
    if(collectWrap.find('tr').length > 0){
        collectWrap.find('tr:last').remove()
        coinPushManiaCollectCnt[key3]--
    }
}

// 关卡配置
function addCoinPushManiaStage(lvIdx, spinsIdx){
    let key3 = `${lvIdx}_${spinsIdx}`
    let stageIdx = coinPushManiaStagesCnt[key3]++
    let rewardIdx = `${key3}_${stageIdx}`
    let html = `
                <table class="coinPushManiaStageWrap table table-bordered"  id=""  style="margin:10px;padding:5px;border:solid 1px goldenrod;" >
                    <tr>
                        <td colspan="6">
                            <div class="form-inline">
                                <label>关卡：</label>
                                <input class="form-control" value="${stageIdx+1}" readonly>
                            </div>
                        </td>
                    </tr>
                     <tr>
                        <td >奖励币唯一id</td>
                        <td >奖励币大小</td>
                        <td >是否大奖</td>
                        <td >代币消耗预设值</td>
                        <td >奖励</td>
                        <td >操作</td>
                    </tr>
                    <tbody id="coinPushManiaStageWrap_${rewardIdx}">
                    </tbody>
                    <tr>
                        <td colspan="6">
                            <botton class="btn btn-primary addCoinPushManiaStageRewardTrBtn" onclick="addCoinPushManiaStageRewardTr(${lvIdx}, ${spinsIdx}, ${stageIdx})">添加奖励币配置</botton>
                            <botton class="btn btn-danger del-tr delCoinPushManiaStageRewardTrBtn" onclick="delCoinPushManiaStageRewardTr(${lvIdx}, ${spinsIdx}, ${stageIdx})">删除奖励币配置</botton>
                        </td>
                    </tr>
                </table>
`

    $(`#coinPushManiaSpinStageWrap_${key3}`).find('.coinPushManiaStageBtnTr').before(html)
}
function delCoinPushManiaStage(lvIdx, spinsIdx){
    let key3 = `${lvIdx}_${spinsIdx}`
    console.log($(`#coinPushManiaSpinStageWrap_${key3}`).find('.coinPushManiaStageWrap'));
    if($(`#coinPushManiaSpinStageWrap_${key3}`).find('.coinPushManiaStageWrap').length >0){
        $(`#coinPushManiaSpinStageWrap_${key3}`).find('.coinPushManiaStageWrap:last').remove()
        coinPushManiaStagesCnt[key3]--
    }
}

// 奖励币配置
function addCoinPushManiaStageRewardTr(lvIdx, spinsIdx, stageIdx){
    let trKey = `${lvIdx}_${spinsIdx}_${stageIdx}`
    let trIdx = $(`#coinPushManiaStageWrap_${trKey}`).find('tr').length
    coinIdx++
    let prizePlaceholder = escape('[]')
    let html = `
            <tr class="commonStageRewardTr">
                <td ><input  class="form-control"  name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][rewardConfig][${stageIdx}][coins][${trIdx}][id]" value="${coinIdx}" readonly></td>
                <td >
                    <select  class="form-control"  name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][rewardConfig][${stageIdx}][coins][${trIdx}][coinSize]" >
                        <option value="1">小</option>
                        <option value="2">中</option>
                        <option value="3">大</option>
                    </select>
                </td>
                <td >
                     <select  class="form-control"  name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][rewardConfig][${stageIdx}][coins][${trIdx}][bonus]" >
                        <option value="0">否</option>
                        <option value="1">是</option>
                    </select>
                </td>
                <td ><input  class="form-control"  name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][rewardConfig][${stageIdx}][coins][${trIdx}][coinUsedLimit]" value="0"></td>
                <td class="prizeShow"></td>
                <td >
                    <input type="hidden" class="prizeInput" name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][rewardConfig][${stageIdx}][coins][${trIdx}][prizes]" value="${prizePlaceholder}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this, 'setCommonPrize', 1)">奖励配置</button>
                </td>
            </tr>`

    $(`#coinPushManiaStageWrap_${trKey}`).append(html)
}
function delCoinPushManiaStageRewardTr(lvIdx, spinsIdx, stageIdx){
    let trKey = `${lvIdx}_${spinsIdx}_${stageIdx}`
    if($(`#coinPushManiaStageWrap_${trKey}`).find('tr').length >0){
        $(`#coinPushManiaStageWrap_${trKey}`).find('tr:last').remove()
    }

}

// 进度配置
function addCoinPushManiaProgress(lvIdx, spinsIdx) {
    let progressKey = `${lvIdx}_${spinsIdx}`
    let progressIdx = coinPushManiaProgressCnt[progressKey]++
    let prizePlaceholder = escape('[]')
    let html = `
            <tr class="commonStageRewardTr">
                <td >
                    <input class="form-control" name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][tokenStageConfig][${progressIdx}][id]" value="${progressIdx+1}" readonly>
                </td>
                <td ><input class="form-control" name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][tokenStageConfig][${progressIdx}][target]" value="0"></td>
                <td class="prizeShow"></td>
                <td>
                    <input type="hidden" class="prizeInput" name="coinPushManiaReward[${lvIdx}][stages][${spinsIdx}][tokenStageConfig][${progressIdx}][prizes]" value="${prizePlaceholder}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this, 'setCommonPrize', 1)">奖励配置</button>
                </td>
            </tr>`

    $(`#coinPushManiaSpinStageWrap_${progressKey}`).find('.coinPushManiaProgressWrap').append(html)
}
function delCoinPushManiaProgress(lvIdx, spinsIdx) {
    let progressKey = `${lvIdx}_${spinsIdx}`
    let progressWrap = $(`#coinPushManiaSpinStageWrap_${progressKey}`).find('.coinPushManiaProgressWrap')
    if(progressWrap && progressWrap.find('tr').length >0 ){
        progressWrap && progressWrap.find('tr:last').remove()
        coinPushManiaProgressCnt[progressKey]--
    }
}