
function changeTypeTo_superGashapon(){
    getActBasicConfig(act_type).then(()=>{
        initSuperGashaponWrap()
    })
}
function initReward_superGashapon(){
    getActBasicConfig(act_type).then(()=>{

        initSuperGashaponWrap()
        // 关卡回写
        if(activity_extra_data && activity_extra_data!='null'){
            $.each(activity_extra_data, function(i, o){
                let lvIdx = i
                addSuperGashaponLvStageWrap()
                $(`input[name="superGashaponRewardConfig[${i}][start]"]`).val(o.start)
                $(`input[name="superGashaponRewardConfig[${i}][end]"]`).val(o.end)
                $.each(o.stages, function(i0, o0){
                    let spinIdx = i0
                    addSuperGashaponSpinStageWrap(lvIdx)

                    $(`input[name="superGashaponRewardConfig[${i}][stages][${i0}][start]"]`).val(o0.start)
                    $(`input[name="superGashaponRewardConfig[${i}][stages][${i0}][end]"]`).val(o0.end)

                    $.each(o0.stages, function(i1, o1){
                        let shushuIdx = i1
                        addSuperGashaponShushuStageWrap(lvIdx, spinIdx)
                        $(`select[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][shushu][]"]`).val(o1.shushu)

                        // 收集物配置
                        $.each([1,2], (collectIdx, collectType)=>{
                            o1[`collect${collectType}`] && $.each(o1[`collect${collectType}`], function(i2, o2){
                                let rewardIdx = i2
                                if (o2.prizes == undefined)o2.prizes = []
                                addSuperGashaponCollectRewardTr(lvIdx, spinIdx, shushuIdx, collectType)
                                let prizeInput = $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect${collectType}][${rewardIdx}][prizes]"]`)
                                $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect${collectType}][${rewardIdx}][stage]"]`).val(o2.stage)
                                $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect${collectType}][${rewardIdx}][target]"]`).val(o2.target)
                                prizeInput.val(escape(JSON.stringify(o2.prizes)))
                                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o2.prizes))

                            })
                        })

                        // 关卡配置
                        o1.layers && $.each(o1.layers, function(i2, o2){
                            let rewardIdx = i2
                            addSuperGashaponStageRewardTable(lvIdx, spinIdx, shushuIdx)
                            $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][layer]"]`).val(o2.layer)

                            $.each(o2.items, function(i2, o3){
                                if (o3.prizes == undefined)o3.prizes = []
                                addSuperGashaponRewardTr(lvIdx, spinIdx, shushuIdx, rewardIdx)
                                let prizeInput = $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${i2}][prizes]"]`)
                                $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${i2}][id]"]`).val(o3.id)
                                $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${i2}][spendLowest]"]`).val(o3.spendLowest)
                                $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${i2}][progressNum]"]`).val(o3.progressNum)
                                $(`select[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${i2}][type]"]`).val(o3.type)
                                $(`input[name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${i2}][weight]"]`).val(o3.weight)
                                prizeInput.val(escape(JSON.stringify(o3.prizes)))
                                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o3.prizes))
                            })

                        })

                    })
                })
            })

            $('.select2').select2({})
        }
    })
}
function initSuperGashaponWrap(){
    $("#reward_info").empty()
    // 关卡数据
    superGashaponMainConfig();
}



// 主配置
function superGashaponMainConfig(){
    var html ='';
    var str='关卡奖励'
    superGashaponLvCnt = 0;
    superGashaponSpinsCnt = {};
    superGashaponShushuCnt = {};
    superGashaponStageCnt = {}
    superGashaponStageRewardCnt = {}
    superGashaponCollectStageRewardCnt = {}
    html+='<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    html +='<div class="" id="spinStageTable" style="top:10px">';
    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="col-sm-12">';
    html+='<button class="btn btn-primary" id="addSuperGashaponLvWrapBtn" onclick="addSuperGashaponLvStageWrap()" type="button">添加等级区间</button>';
    html+='<button class="btn btn-white"   id="delSuperGashaponLvWrapBtn" onclick="delSuperGashaponLvStageWrap()" type="button">删除等级区间</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    $("#reward_info").append(html);
}

function delSuperGashaponLvStageWrap(){
    if(superGashaponLvCnt < 1) return
    $(`#superGashaponLvWrap_${superGashaponLvCnt}`).remove()
    superGashaponLvCnt--
}
function addSuperGashaponLvStageWrap(){
    superGashaponLvCnt++
    superGashaponSpinsCnt[superGashaponLvCnt] = 0;
    var html = `
         <div class="form-group superGashaponLvWrap " id="superGashaponLvWrap_${superGashaponLvCnt}" data-idx="${superGashaponLvCnt}" 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="superGashaponRewardConfig[${superGashaponLvCnt}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="superGashaponRewardConfig[${superGashaponLvCnt}][end]" value="0">
                </div>
            </div>
            <div class="btnGroups">
                <botton class="btn btn-primary  addSuperGashaponStageWrapBtn" onclick="addSuperGashaponSpinStageWrap(${superGashaponLvCnt})">添加体力层级</botton>
                <botton class="btn btn-danger del-tr delSuperGashaponStageWrapBtn" onclick="delSuperGashaponSpinStageWrap(${superGashaponLvCnt})">删除体力层级</botton>
            </div>
         </div>`

    $('#addSuperGashaponLvWrapBtn').before(html)
    $('.select2').select2({})
}

function delSuperGashaponSpinStageWrap(lvIdx){
    let spinIdx = superGashaponSpinsCnt[lvIdx]
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    if(spinIdx < 1) return
    $(`#superGashaponSpinWrap_${lvSpinIdx}`).remove()
    superGashaponSpinsCnt[lvIdx]--
}
function addSuperGashaponSpinStageWrap(lvIdx){
    let spinIdx = ++superGashaponSpinsCnt[lvIdx]
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    superGashaponShushuCnt[lvSpinIdx] = 0;
    var html = `
         <div class="form-group superGashaponSpinWrap " id="superGashaponSpinWrap_${lvSpinIdx}" data-idx="${lvSpinIdx}" 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="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][end]" value="0">
                </div>
            </div>
            <div class="btnGroups">
                <botton class="btn btn-primary  addSuperGashaponStageWrapBtn" onclick="addSuperGashaponShushuStageWrap(${lvIdx}, ${spinIdx})">添加数数分组层级</botton>
                <botton class="btn btn-danger del-tr delSuperGashaponStageWrapBtn" onclick="delSuperGashaponShushuStageWrap(${lvIdx}, ${spinIdx})">删除数数分组层级</botton>
            </div>
         </div>`

    $(`#superGashaponLvWrap_${lvIdx}`).find('.btnGroups:last').before(html)
    $('.select2').select2({})
}

// 数数分组
function delSuperGashaponShushuStageWrap(lvIdx, spinIdx){
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${superGashaponShushuCnt[lvSpinIdx]}`
    if(superGashaponShushuCnt[lvSpinIdx] < 1)return;
    $(`#superGashaponShushuWrap_${lvSpinShushuIdx}`).remove()
    superGashaponShushuCnt[lvSpinIdx]--
}
function addSuperGashaponShushuStageWrap(lvIdx, spinIdx){
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    let shushuIdx = ++superGashaponShushuCnt[lvSpinIdx]
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    superGashaponStageCnt[lvSpinShushuIdx] = 0;

    let ssOptions = ''
    $.each(shushuGroups, function(i,o){
        ssOptions += '<option value="'+i+'">'+o+'</option>'
    });
    var html = `
         <div class="form-group superGashaponShushuWrap superGashaponShushuWrap_${lvSpinShushuIdx}" id="superGashaponShushuWrap_${lvSpinShushuIdx}" data-idx="${lvSpinShushuIdx}" 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-5">
                    <select class=" select2" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][shushu][]"  multiple  style="min-width: 100%">
                        ${ssOptions}
                    </select>
                 </div>
             </div>
             <div class="btnGroups">
                 <botton class="btn btn-primary  addSuperGashaponStageWrapBtn" onclick="addSuperGashaponStageRewardTable(${lvIdx}, ${spinIdx}, ${shushuIdx})">添加关卡</botton>
                 <botton class="btn btn-danger del-tr delSuperGashaponStageWrapBtn" onclick="delSuperGashaponStageRewardTable(${lvIdx}, ${spinIdx}, ${shushuIdx})">删除关卡</botton>
             </div>
             
          </div>`

    $(`#superGashaponSpinWrap_${lvSpinIdx}`).find('.btnGroups:last').before(html)
    $('.select2').select2({})
    addSuperGashaponCollectStageRewardTable(lvIdx, spinIdx, shushuIdx, 1)
    addSuperGashaponCollectStageRewardTable(lvIdx, spinIdx, shushuIdx, 2)
    // addSuperGashaponStageRewardTable(lvIdx, spinIdx, shushuIdx)
}

//奖励配置表html
function delSuperGashaponStageRewardTable(lvIdx, spinIdx, shushuIdx){
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    let lvSpinShushuRewardIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${superGashaponStageCnt[lvSpinShushuIdx]}`
    if(superGashaponStageCnt[lvSpinShushuIdx]<1)return
    $(`#reward_table_${lvSpinShushuRewardIdx}`).remove();
    superGashaponStageCnt[lvSpinShushuIdx]--
}
function addSuperGashaponStageRewardTable(lvIdx, spinIdx, shushuIdx) {
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`

    let rewardIdx = superGashaponStageCnt[lvSpinShushuIdx]++
    var index = rewardIdx
    var data ={"stage":rewardIdx+1,"end":0};
    var stageIdx = lvSpinShushuIdx + '_' + index
    superGashaponStageRewardCnt[stageIdx] = -1;
    var cols = 7;
    var html = `
    <table data-idx="${index}" class="table table-bordered superGashaponStageTable" id="reward_table_${stageIdx}">
        <thead>
            <tr>
                <td colspan="${cols}">
                    <div class="form-group">
                        <label class="col-sm-2 control-label m-b">关卡</label>
                        <div class="col-sm-3 input-group">
                            <input readonly type="text" style="text-align: center" class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][layer]" value="${data['stage']}">
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <th>序号</th>
                <th>类型</th>
                <th>权重值</th>
                <th>出奖所需代币</th>
                <th>收集物数量</th>
                <th>奖品</th>
                <th>奖励设置</th>
            </tr>
        </thead>
        <tbody id="table_body_${stageIdx}">
    
        </tbody>
        <tr>
            <td colspan="${cols}">
                <div class="text-center">
                    <botton class="btn btn-primary addSuperGashaponRewardTrBtn" onclick="addSuperGashaponRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx}, ${rewardIdx})">添加</botton>
                    <botton class="btn btn-danger  delSuperGashaponRewardTrBtn" onclick="delSuperGashaponRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx}, ${rewardIdx})">删除</botton>
                </div>
            </td>
        </tr>
    </table>`
    $(`#superGashaponShushuWrap_${lvSpinShushuIdx}`).find('.btnGroups:last').before(html);
}


// 奖励行
function addSuperGashaponRewardTr(lvIdx, spinIdx, shushuIdx, rewardIdx) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${rewardIdx}`
    var stage = ++superGashaponStageRewardCnt[complexIdx]
    let rewardTypeHtml = ''
    actBasicConf[act_type] && actBasicConf[act_type]['rewardTypes'] && $.each(actBasicConf[act_type]['rewardTypes'], (tp, txt)=>{
        rewardTypeHtml += `<option value="${tp}">${txt}</option>`
    })
    var reward = '[]';
    reward = escape(reward);
    var html =`
            <tr class="superGashaponRewardTr commonStageRewardTr" data-idx="${stage}" id="commonStageRewardTr_${complexIdx}">
                <td>
                    <input type="text" class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${stage}][id]" value="${stage + 1}">
                </td>
                <td>
                    <select class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${stage}][type]"  >
                        ${rewardTypeHtml}
                    </select>
                </td>
                <td>
                    <input type="text" class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${stage}][weight]" value="0">
                </td>
                <td>
                    <input type="text" class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${stage}][spendLowest]" value="0">
                </td>
                <td>
                    <input type="text" class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${stage}][progressNum]" value="0">
                </td>
                <td class="prizeShow "></td>
                <td>
                    <input type="hidden" class="prizeInput" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][layers][${rewardIdx}][items][${stage}][prizes]" value="${reward}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td>
            </tr>`

    $('#table_body_'+complexIdx).append(html);
}
function delSuperGashaponRewardTr(lvIdx, spinIdx, shushuIdx, rewardIdx) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${rewardIdx}`
    if(superGashaponStageRewardCnt[complexIdx] < 0)return
    $('#table_body_'+complexIdx).find('tr:last').remove();
    superGashaponStageRewardCnt[complexIdx]--
}

//收集阶段奖励配置表html
function addSuperGashaponCollectStageRewardTable(lvIdx, spinIdx, shushuIdx, collectType) {
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    let complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${collectType}`
    superGashaponCollectStageRewardCnt[complexIdx] = 0;
    let cols = 4;
    let html = `
        <h5>收集物${collectType}</h5>
        <table data-idx="${lvSpinShushuIdx}" class="table table-bordered superGashaponCollectStageTable" id="collect_table_${complexIdx}">
            <thead>
                <tr>
                    <th>阶段</th>
                    <th>阶段值</th>
                    <th>奖品</th>
                    <th>奖励设置</th>
                </tr>
            </thead>
            <tbody id="collect_tbody_${lvSpinShushuIdx}_${collectType}">
        
            </tbody>
            <tr>
                <td colspan="${cols}">
                    <div class="text-center">
                        <botton class="btn btn-primary addSuperGashaponRewardTrBtn" onclick="addSuperGashaponCollectRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx}, ${collectType})">添加收集阶段</botton>
                        <botton class="btn btn-danger  delSuperGashaponRewardTrBtn" onclick="delSuperGashaponCollectRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx}, ${collectType})">删除收集阶段</botton>
                    </div>
                </td>
            </tr>
        </table>`
    $(`#superGashaponShushuWrap_${lvSpinShushuIdx}`).find('.btnGroups:last').before(html);
}
// 收集阶段奖励行
function addSuperGashaponCollectRewardTr(lvIdx, spinIdx, shushuIdx, collectType) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${collectType}`
    var stage = superGashaponCollectStageRewardCnt[complexIdx]++

    var reward = '[]';
    reward = escape(reward);
    var html =`
            <tr class=" commonStageRewardTr superGashaponRewardTr superGashaponCollectStageRewardTr" data-idx="${stage}" id="superGashaponCollectStageRewardTr_${complexIdx}_${superGashaponCollectStageRewardCnt[complexIdx]}">
                <td>
                    <input type="text" class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect${collectType}][${stage}][stage]" value="${stage + 1}">
                </td>
                <td>
                    <input type="text" class="form-control" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect${collectType}][${stage}][target]" value="0">
                </td>
                <td class="prizeShow "></td>
                <td>
                    <input type="hidden" class="prizeInput" name="superGashaponRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect${collectType}][${stage}][prizes]" value="${reward}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td>
            </tr>`

    $(`#collect_tbody_${complexIdx}`).append(html);
}
function delSuperGashaponCollectRewardTr(lvIdx, spinIdx, shushuIdx, collectType) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${collectType}`
    if(superGashaponCollectStageRewardCnt[complexIdx]<1)return
    $(`#superGashaponCollectStageRewardTr_${complexIdx}_${superGashaponCollectStageRewardCnt[complexIdx]}`).remove();
    superGashaponCollectStageRewardCnt[complexIdx]--
}