let crazyTokenLvCnt = 0;
let crazyTokenSpinsStages = {}
let crazyTokenShushuStages = {}
let crazyTokenRewardStages = {}
async function initCrazyTokenConfig(){
    if(typeof crazyTokenTypes == 'undefined'){
        showLoading()
        await $.get('/activity/getActBasicConfig/crazy_token',function(response){
            if (typeof response === 'string')response = JSON.parse(response)
            hideLoading()
            crazyTokenTypes = response.data.tokens
        })
    }
    console.log(crazyTokenTypes)
}
function renderCrazyTokenConfig() {
    initCrazyTokenConfig().then(()=>{
        initCrazyTokenWrap()
        reward_conf && $.each(reward_conf, function (lvIdx, o) {
            addCrazyTokenLvWrap()
            $(`input[name="crazyTokenConfig[${lvIdx}][lvStart]"]`).val(o.lvStart)
            $(`input[name="crazyTokenConfig[${lvIdx}][lvEnd]"]`).val(o.lvEnd)
            o.stages && $.each(o.stages, function (spinsIdx, o1) {
                addCrazyTokenSpinsWrap(lvIdx)
                $(`input[name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][spinsStart]"]`).val(o1.spinsStart)
                $(`input[name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][spinsEnd]"]`).val(o1.spinsEnd)
                o1.stages && $.each(o1.stages, function (ssIdx, o2) {
                    addCrazyTokenShushuWrap(lvIdx, spinsIdx)
                    $(`select[name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][shushu][]"]`).val(o2.shushu)
                    o2.layers && $.each(o2.layers, function (stageIdx, o3) {
                        addCrazyTokenStagesWrap(lvIdx, spinsIdx, ssIdx)
                        addCrazyTokenReward(lvIdx, spinsIdx, ssIdx, stageIdx, o3.stages.length)
                        $(`select[name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][layers][${stageIdx}][stageNum]"]`).val(o3.stages.length)
                        o3.stages && $.each(o3.stages, function (ri, ro) {
                            $(`input[name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][layers][${stageIdx}][stages][${ri}][stepnum]"]`).val(ro.stepnum)
                            $(`input[name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][layers][${stageIdx}][stages][${ri}][prize]"]`).val(escape(JSON.stringify(ro.prize)))
                            $(`input[name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][layers][${stageIdx}][stages][${ri}][prize]"]`).parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(ro.prize))
                        })

                    })
                })
            })
        })
        activity_extra_data && $('select[name="crazyTokenExtra[icons][]"]').val(activity_extra_data.icons)
        activity_extra_data && $('select[name="crazyTokenExtra[noCircle]"]').val(activity_extra_data.noCircle)
        $('.select2').select2({})
    })


}

function changeTypeToCrazyToken() {
    initCrazyTokenConfig().then(()=>{
        initCrazyTokenWrap()
        console.log(123)
    })

}
function initCrazyTokenWrap(){
    $("#reward_info").empty();
    let html = crazyTokenWrap();
    $("#reward_info").append(html);
    $('.select2').select2({})
}
function crazyTokenWrap() {
    let optionsHtml = '';
    $.each(crazyTokenTypes, (i,o)=>{
        optionsHtml += `<option value="${i}">${o}</option>`
    })

    let tokenText = '生效代币类型'
    return `
        <div class="form-group">
            <label class="col-xs-1 control-label">${tokenText}:</label>
            <div class="col-sm-3">
                <select name="crazyTokenExtra[icons][]"  class="form-control select2" multiple>
                    ${optionsHtml}
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-xs-1 control-label">无限升级开关:</label>
            <div class="col-sm-3">
                <select name="crazyTokenExtra[noCircle]"  class="form-control select2" >
                    <option value="0">开</option>
                    <option value="1">关</option>
                </select>
            </div>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
        <div class="" id="crazyTokenWrap" style="top:10px">
            <div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">分层级配置</h5></div>
        </div>
        <div class="form-group addCrazyTokenLvWrapBtnGroup">
            <div class=" ">
                <button class="btn btn-primary" id="addCrazyTokenLvWrapBtn" onclick="addCrazyTokenLvWrap()" type="button">添加等级层级</button>
                <button class="btn btn-white"   id="delCrazyTokenLvWrapBtn" onclick="delCrazyTokenLvWrap()" type="button">删除等级层级</button>
            </div>
      </div>
      <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

function addCrazyTokenLvWrap() {
    crazyTokenLvCnt++
    crazyTokenSpinsStages[crazyTokenLvCnt] = 0

    let html = `
        <div class="form-group crazyTokenLvWrap " id="crazyTokenLvWrap_${crazyTokenLvCnt}" data-idx="${crazyTokenLvCnt}" 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="crazyTokenConfig[${crazyTokenLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="crazyTokenConfig[${crazyTokenLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <button type="button" class="btn btn-primary addCrazyTokenSpinsWrapBtn" onclick="addCrazyTokenSpinsWrap(${crazyTokenLvCnt})">添加体力层级</button>
            <button type="button" class="btn btn-danger del-tr delCrazyTokenSpinsWrapBtn" onclick="delCrazyTokenSpinsWrap(${crazyTokenLvCnt})">删除体力层级</button>
        </div>
`

    $(`.addCrazyTokenLvWrapBtnGroup`).before(html)
    $('.select2').select2({})
}

function addCrazyTokenSpinsWrap(lvIdx) {
    let spinsIdx = ++crazyTokenSpinsStages[lvIdx]
    let ssKey = `${lvIdx}_${spinsIdx}`
    crazyTokenShushuStages[ssKey] = 0

    let html = `
        <div class="form-group crazyTokenSpinsWrap " id="crazyTokenSpinsWrap_${ssKey}" data-idx="${spinsIdx}" 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="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][spinsStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][spinsEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <button type="button" class="btn btn-primary addCrazyTokenShushuWrapBtn" onclick="addCrazyTokenShushuWrap(${lvIdx}, ${spinsIdx})">添加数数分组层级</button>
            <button type="button" class="btn btn-danger del-tr delCrazyTokenShushuWrapBtn" onclick="delCrazyTokenShushuWrap(${lvIdx}, ${spinsIdx})">删除数数分组层级</button>
        </div>
`

    $(`#crazyTokenLvWrap_${lvIdx}`).find('.addCrazyTokenSpinsWrapBtn').before(html)
    $('.select2').select2({})
}

function addCrazyTokenShushuWrap(lvIdx, spinsIdx) {
    let ssKey = `${lvIdx}_${spinsIdx}`
    let ssIdx = ++crazyTokenShushuStages[ssKey]
    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}`
    crazyTokenRewardStages[rewardKey] = -1

    let ssOptionsHtml = ''
    $.each(shushuGroups, function (i, o) {
        ssOptionsHtml += `<option value="${i}">${o}</option>`
    });

    let html = `
        <div class="crazyTokenShushuWrap" id="crazyTokenShushuWrap_${rewardKey}" data-idx="${ssIdx}" style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            <div class="form-group">
                <label class="col-sm-1 control-label m-b">数数分组: </label>
                <div class="col-sm-3 input-group">
                    <select class="form-control select2" name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][shushu][]"  >
                        ${ssOptionsHtml}
                    </select>
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <button type="button" class="btn btn-primary addCrazyTokenStagesWrapBtn" onclick="addCrazyTokenStagesWrap(${lvIdx}, ${spinsIdx}, ${ssIdx})">添加奖励层级</button>
            <button type="button" class="btn btn-danger del-tr delCrazyTokenStagesWrapBtn" onclick="delCrazyTokenStagesWrap(${lvIdx}, ${spinsIdx}, ${ssIdx}})">删除奖励层级</button>
        </div>`;

    $(`#crazyTokenSpinsWrap_${ssKey}`).find('.addCrazyTokenShushuWrapBtn').before(html)
    $('.select2').select2({})
}

function addCrazyTokenStagesWrap(lvIdx, spinsIdx, ssIdx) {
    let stageKey = `${lvIdx}_${spinsIdx}_${ssIdx}`
    crazyTokenRewardStages[stageKey]++
    let stageIdx = crazyTokenRewardStages[stageKey]

    let stageNumOptionsHtml = ''
    let stageNumMap = {
        3:3,
        5:5,
        7:7
    };
    let defaultNum = 3;
    if(act_type == 'crazy_token2'){
        stageNumMap = {
            10:10
        }
        defaultNum = 10
    }
    $.each(stageNumMap, function (i, o) {
        stageNumOptionsHtml += `<option value="${i}">${o}</option>`
    });

    let html = `
        <div class="crazyTokenStageWrap" id="crazyTokenStageWrap_${stageKey}" data-idx="${ssIdx}" style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            <table class="table table-bordered" > 
            <thead>
                <tr style="display: none">
                    <td colspan="4"> 
                        <div class="form-group">
                            <label class="col-sm-1 control-label m-b">奖励阶段数 </label>
                            <div class="col-sm-3 input-group"> 
                                <select class=" form-control" 
                                onchange="addCrazyTokenReward(${lvIdx}, ${spinsIdx}, ${ssIdx}, ${stageIdx}, $(this).val())" 
                                name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][layers][${stageIdx}][stageNum]"  style="min-width: 100%"> 
                                    ${stageNumOptionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th>阶段值</th> 
                    <th>奖品</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="crazyTokenRewardBody_${stageKey}_${stageIdx}"> </tbody>
            <tr>
                <td colspan="4" class="text-left"> 
                   
                        <button type="button" class="btn btn-primary" onclick="addCrazyTokenRewardTr(${lvIdx}, ${spinsIdx}, ${ssIdx}, ${stageIdx});">增加阶段</button>
                        <button type="button" class="btn btn-danger" onclick="delCrazyTokenRewardTr(${lvIdx}, ${spinsIdx}, ${ssIdx}, ${stageIdx});">删除阶段</button>
                </td>
            </tr>
        </table>
        </div>`;

    $(`#crazyTokenShushuWrap_${stageKey}`).find('.addCrazyTokenStagesWrapBtn').before(html)
    // addCrazyTokenReward(lvIdx, spinsIdx, ssIdx, stageIdx, defaultNum)
    $('.select2').select2({})
}

function addCrazyTokenReward(lvIdx, spinsIdx, ssIdx, stageIdx, stageNum){
    let i = 0;
    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}_${stageIdx}`
    $(`#crazyTokenRewardBody_${rewardKey}`).empty()
    while(i < stageNum){
        addCrazyTokenRewardTr(lvIdx, spinsIdx, ssIdx, stageIdx);
        i++;
    }
}
function addCrazyTokenRewardTr(lvIdx, spinsIdx, ssIdx, stageIdx) {
    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}_${stageIdx}`
    let rIdx = $(`#crazyTokenRewardBody_${rewardKey}`).find('tr').length
    let prize = '[]';
    prize = escape(prize);
    let html = `
        <tr id=" " data-idx="${rIdx}" class="commonStageRewardTr"> 
            <td>
                <input type="text" class="form-control" readonly name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][layers][${stageIdx}][stages][${rIdx}][stage]" value="${rIdx+1}">
            </td> 
            <td>
                <input type="text" class="form-control"  name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][layers][${stageIdx}][stages][${rIdx}][stepnum]" value="0">
            </td> 
            <td class="prizeShow"></td> 
            <td>
                <input type="hidden" class="prizeInput" name="crazyTokenConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][layers][${stageIdx}][stages][${rIdx}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
            </td> 
        </tr> 
    `;

    $(`#crazyTokenRewardBody_${rewardKey}`).append(html)
}
function delCrazyTokenRewardTr(lvIdx, spinsIdx, ssIdx, stageIdx) {
    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}_${stageIdx}`
    $(`#crazyTokenRewardBody_${rewardKey}`).find('tr:last').remove()
}

function delCrazyTokenLvWrap() {
    if (crazyTokenLvCnt < 1) {
        return;
    }
    $('#crazyTokenLvWrap_' + crazyTokenLvCnt).remove();
    crazyTokenLvCnt--;
}

function delCrazyTokenSpinsWrap(lvIdx) {
    if (crazyTokenSpinsStages[lvIdx] < 1) return
    $(`#crazyTokenLvWrap_${lvIdx}`).find('.crazyTokenSpinsWrap:last').remove()
    crazyTokenSpinsStages[lvIdx]--
}

function delCrazyTokenShushuWrap(lvIdx, spinsIdx) {
    let ssKey = `${lvIdx}_${spinsIdx}`
    if (crazyTokenShushuStages[ssKey] < 1) return
    $(`#crazyTokenSpinsWrap_${ssKey}`).find('.crazyTokenShushuWrap:last').remove()
    crazyTokenShushuStages[ssKey]--
}


function delCrazyTokenStagesWrap(lvIdx, spinsIdx, ssIdx) {
    let stagesKey = `${lvIdx}_${spinsIdx}_${ssIdx}`
    if (crazyTokenRewardStages[stagesKey] < 0) return
    $(`#crazyTokenShushuWrap_${stagesKey}`).find('.crazyTokenStageWrap:last').remove()
    crazyTokenRewardStages[stagesKey]--
}


