function initReward_superGashapon(type) {
    console.log('init ' + type)
    changeTypeTo_superGashapon()

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

    $.each(reward_info, function (i, o) {
        $('#addSuperGashaponLvWrapBtn').click()
        $(`input[name="superGashaponConfig[${i}][lvStart]"]`).val(o.lvStart)
        $(`input[name="superGashaponConfig[${i}][lvEnd]"]`).val(o.lvEnd)
        $.each(o.stages, function (i1, o1) {
            $('.addSuperGashaponSpinsWrapBtn:last').click()
            $(`input[name="superGashaponConfig[${i}][stages][${i1}][spinsStart]"]`).val(o1.spinsStart)
            $(`input[name="superGashaponConfig[${i}][stages][${i1}][spinsEnd]"]`).val(o1.spinsEnd)

            var stage = `${i}_${i1}`

            $.ajax({
                type: "GET", // 数据提交类型
                url: "activity/getActStageReward/" + $asyncId + '/' + stage, // 发送地址
                data: {}, //发送数据
                async: false, // 是否异步
                success: function (data) { // 成功回调
                    console.log(stage, 'finish')
                    data = JSON.parse(data);
                    o1.reward = data.data
                }
            });
            console.log(stage, 'complete')

            $.each(o1.reward, function (i2, o2) {
                $('.addSuperGashaponRewardTrBtn:last').click()
                $(`input[name="superGashaponConfig[${i}][stages][${i1}][reward][${i2}][layer]"]`).val(o2.layer)
                $(`select[name="superGashaponConfig[${i}][stages][${i1}][reward][${i2}][gashType]"]`).val(o2.gashType)
                $(`input[name="superGashaponConfig[${i}][stages][${i1}][reward][${i2}][weight]"]`).val(o2.weight)
                $(`input[name="superGashaponConfig[${i}][stages][${i1}][reward][${i2}][needCoins]"]`).val(o2.needCoins)
                $(`input[name="superGashaponConfig[${i}][stages][${i1}][reward][${i2}][prize]"]`).val(escape(JSON.stringify(o2.prize)))

                var prizeHtml = ''
                $.each(o2.prize, function (i, prize) {
                    prizeHtml += chestPropsHtml(prize)
                })
                $('.prizeShow:last').html(prizeHtml)
            })
        })
    })
}

function changeTypeTo_superGashapon() {
    superGashaponLvCnt = 0
    superGashaponSpinsStages = {}
    superGashaponShushuStages = {}
    superGashaponRewardStages = {}

    $('#reward_info').empty()
    var html = superGashaponStageTable();
    $("#reward_info").append(html);
}

function superGashaponStageTable() {
    return `
        <div class="form-group text-center">
            <div class=" ">
                <button class="btn btn-primary" id="addSuperGashaponLvWrapBtn" onclick="addSuperGashaponLvWrap(this)" type="button">添加等级层级</button>
                <button class="btn btn-white"   id="delSuperGashaponLvWrapBtn" onclick="delSuperGashaponLvWrap()" type="button">删除等级层级</button>
            </div>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

function addSuperGashaponLvWrap(obj) {
    superGashaponLvCnt++
    superGashaponSpinsStages[superGashaponLvCnt] = 0

    var html = `
        <div class="form-group superGashaponLvWrap " id="superGashaponLvWrap_${superGashaponLvCnt}" data-idx="${superGashaponLvCnt}" 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="superGashaponConfig[${superGashaponLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="superGashaponConfig[${superGashaponLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addSuperGashaponSpinsWrapBtn" onclick="addSuperGashaponSpinsWrap(this)">添加体力层级</botton>
            <botton class="btn btn-danger del-tr delSuperGashaponSpinsWrapBtn" onclick="delSuperGashaponSpinsWrap(this)">删除体力层级</botton>
        </div>
`

    $(obj).before(html)
    $('.select2').select2({})
}

function addSuperGashaponSpinsWrap(obj) {

    var lvIdx = $(obj).parents('.superGashaponLvWrap').data('idx')
    var superGashaponSpinsCnt = ++superGashaponSpinsStages[lvIdx]
    var rewardIdx = `${lvIdx}_${superGashaponSpinsCnt}`
    superGashaponRewardStages[rewardIdx] = 0

    var html = `
        <div class="form-group superGashaponSpinsWrap " id="superGashaponSpinsWrap_${rewardIdx}" data-idx="${superGashaponSpinsCnt}" style="margin:10px;padding:10px;border:solid 1px black;">
            <table class="table table-bordered" > 
            <thead>
                <tr>
                    <td colspan="7"> 
                        <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="superGashaponConfig[${lvIdx}][stages][${superGashaponSpinsCnt}][spinsStart]" value="0">
                                <span class="input-group-addon">-</span>
                                <input type="text" style="text-align: center" class="form-control" name="superGashaponConfig[${lvIdx}][stages][${superGashaponSpinsCnt}][spinsEnd]" value="0">
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">层级</th> 
                    <th>扭蛋类型</th> 
                    <th>权重</th> 
                    <th>出奖所需代币数</th> 
                    <th>奖品</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="superGashaponRewardBody_${rewardIdx}"> </tbody>
            <tr>
                <td colspan="7">
                    <div class="text-center">
                        <botton class="btn btn-primary addSuperGashaponRewardTrBtn" onclick="addSuperGashaponRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delSuperGashaponRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>
    `

    $(obj).before(html)
    $('.select2').select2({})
}


//体力大师添加进度奖行
function addSuperGashaponRewardTr(obj) {
    var lvIdx = $(obj).parents('.superGashaponLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.superGashaponSpinsWrap').data('idx')

    var lsrIdx = `${lvIdx}_${spinsIdx}`
    var rIdx = ++superGashaponRewardStages[lsrIdx]

    var rewardKey = `${lvIdx}_${spinsIdx}_${rIdx}`

    var optionsHtml = ''
    $.each({0: "普通", 1: "大奖", 2: "幸运蛋",}, function (i, o) {
        optionsHtml += `<option value="${i}">${o}</option>`
    });


    var prize = '[]';
    prize = escape(prize);
    var html = `
        <tr id="superGashaponRewardInfo_${rewardKey}" data-idx="${rIdx}"> 
            <td>
                <input type="number" class="form-control"  name="superGashaponConfig[${lvIdx}][stages][${spinsIdx}][reward][${rIdx}][layer]" value="0">
            </td> 
            <td>
                 <select class="form-control" name="superGashaponConfig[${lvIdx}][stages][${spinsIdx}][reward][${rIdx}][gashType]">
                    ${optionsHtml}
                </select>
            </td> 
            <td>
                <input type="number" class="form-control"  name="superGashaponConfig[${lvIdx}][stages][${spinsIdx}][reward][${rIdx}][weight]" value="0">
            </td> 
            <td>
                <input type="number" class="form-control"  name="superGashaponConfig[${lvIdx}][stages][${spinsIdx}][reward][${rIdx}][needCoins]" value="0">
            </td> 
            <td class="prizeShow"></td> 
            <td>
                <input type="hidden" class="prizeInput" name="superGashaponConfig[${lvIdx}][stages][${spinsIdx}][reward][${rIdx}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'superGashapon',1)">奖励配置</button>
            </td> 
        </tr> 
    `;

    $('#superGashaponRewardBody_' + lsrIdx).append(html)
}

function delSuperGashaponLvWrap() {
    if (superGashaponLvCnt < 1) {
        return;
    }
    $('#superGashaponLvWrap_' + superGashaponLvCnt).remove();
    superGashaponLvCnt--;
}

function delSuperGashaponSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.superGashaponLvWrap').data('idx')

    if (superGashaponSpinsStages[lvIdx] < 1) return
    $(obj).parents('.superGashaponLvWrap').find('.superGashaponSpinsWrap:last').remove()
    superGashaponSpinsStages[lvIdx]--
}


function delSuperGashaponRewardTr(obj) {
    var lvIdx = $(obj).parents('.superGashaponLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.superGashaponSpinsWrap').data('idx')

    var rewardIdx = `${lvIdx}_${spinsIdx}`
    var rewardKey = rewardIdx + '_' + superGashaponRewardStages[rewardIdx]

    if (superGashaponRewardStages[rewardIdx] < 1) return;
    $('#superGashaponRewardInfo_' + rewardKey).remove()
    superGashaponRewardStages[rewardIdx]--
}