function initReward_spins_master(type) {
    console.log('init ' + type)
    changeTypeTo_spins_master()

    // 奖励回写
    console.log('extra_reward', extra_reward)
    if (!extra_reward) {
        return
    }
    $.each(extra_reward, function (i, o) {
        $('#addSpinsMasterLvWrapBtn').click()
        $('input[name="spinsMasterConfig[' + i + '][lvStart]"]').val(o.lvStart)
        $('input[name="spinsMasterConfig[' + i + '][lvEnd]"]').val(o.lvEnd)
        $.each(o.stages, function (i1, o1) {
            $('.addSpinsMasterSpinsWrapBtn:last').click()
            $('input[name="spinsMasterConfig[' + i + '][stages][' + i1 + '][spinsStart]"]').val(o1.spinsStart)
            $('input[name="spinsMasterConfig[' + i + '][stages][' + i1 + '][spinsEnd]"]').val(o1.spinsEnd)
            $.each(o1.stages, function (i2, o2) {
                $('.addSpinsMasterShushuWrapBtn:last').click()
                $('select[name="spinsMasterConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][shushu][]"]').val(o2.shushu)
                var stage = `${i}_${i1}_${i2}`

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

                $.each(o2.reward, function (i3, o3) {
                    $('.addSpinsMasterRewardTrBtn:last').click()
                    $('input[name="spinsMasterConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][reward][' + i3 + '][stepnum]"]').val(o3.stepnum)
                    $('input[name="spinsMasterConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][reward][' + i3 + '][prize]"]').val(escape(JSON.stringify(o3.prize)))
                    var prizeHtml = ''
                    $.each(o3.prize, function (i, prize) {
                        prizeHtml += chestPropsHtml(prize)
                    })
                    $('.prizeShow:last').html(prizeHtml)
                })
            })
        })
    })
}

function changeTypeTo_spins_master() {
    spinsMasterLvCnt = 0
    spinsMasterSpinsStages = {}
    spinsMasterShushuStages = {}
    spinsMasterRewardStages = {}

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

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

function addSpinsMasterLvWrap(obj) {
    spinsMasterLvCnt++
    spinsMasterSpinsStages[spinsMasterLvCnt] = 0

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

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

function addSpinsMasterSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.spinsMasterLvWrap').data('idx')
    var spinsMasterSpinsCnt = ++spinsMasterSpinsStages[lvIdx]
    var spinsIdx = `${lvIdx}_${spinsMasterSpinsCnt}`
    spinsMasterShushuStages[spinsIdx] = 0

    var html = `
        <div class="form-group spinsMasterSpinsWrap " id="spinsMasterSpinsWrap_${spinsIdx}" data-idx="${spinsMasterSpinsCnt}" 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="spinsMasterConfig[${lvIdx}][stages][${spinsMasterSpinsCnt}][spinsStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="spinsMasterConfig[${lvIdx}][stages][${spinsMasterSpinsCnt}][spinsEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addSpinsMasterShushuWrapBtn" onclick="addSpinsMasterShushuWrap(this)">添加数数分组层级</botton>
            <botton class="btn btn-danger del-tr delSpinsMasterShushuWrapBtn" onclick="delSpinsMasterShushuWrap(this)">删除数数分组层级</botton>
        </div>
    `

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


function addSpinsMasterShushuWrap(obj) {
    var lvIdx = $(obj).parents('.spinsMasterLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.spinsMasterSpinsWrap').data('idx')

    var spinsMasterSSCnt = ++spinsMasterShushuStages[`${lvIdx}_${spinsIdx}`]

    var rewardIdx = `${lvIdx}_${spinsIdx}_${spinsMasterSSCnt}`
    spinsMasterRewardStages[rewardIdx] = 0

    var arr = JSON.parse($hideSSGroup);

    var optionsHtml = ''
    $.each(arr, function (i, o) {
        optionsHtml += '<option value="' + i + '">' + o + '</option>'
    });

    var html = `
        <div class="spinsMasterShushuWrap" id="spinsMasterShushuWrap_${rewardIdx}" data-idx="${spinsMasterSSCnt}" style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            <table class="table table-bordered" > 
            <thead>
                <tr>
                    <td colspan="5"> 
                        <div class="form-group">
                            <label class="col-sm-1 control-label m-b">数数分组 </label>
                            <div class="col-sm-3 input-group"> 
                                <select class=" select2" name="spinsMasterConfig[${lvIdx}][stages][${spinsIdx}][stages][${spinsMasterSSCnt}][shushu][]"  multiple  style="min-width: 100%"> 
                                    ${optionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th>阶段值</th> 
                    <th>奖品</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="spinsMasterRewardBody_${rewardIdx}"> </tbody>
            <tr>
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addSpinsMasterRewardTrBtn" onclick="addSpinsMasterRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delSpinsMasterRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

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

//体力大师添加进度奖行
function addSpinsMasterRewardTr(obj) {
    var lvIdx = $(obj).parents('.spinsMasterLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.spinsMasterSpinsWrap').data('idx')
    var rewardIdx = $(obj).parents('.spinsMasterShushuWrap').data('idx')

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

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

    var prize = '[]';
    var prize_show_arr = JSON.parse(prize);
    prize = escape(prize);
    var html = `
        <tr id="spinsMasterRewardInfo_${rewardKey}" data-idx="${rIdx}"> 
            <td>
                <input type="text" class="form-control" readonly name="spinsMasterConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][reward][${rIdx}][stage]" value="${rIdx}">
            </td> 
            <td>
                <input type="text" class="form-control"  name="spinsMasterConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][reward][${rIdx}][stepnum]" value="0">
            </td> 
            <td class="prizeShow"></td> 
            <td>
                <input type="hidden" class="prizeInput" name="spinsMasterConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][reward][${rIdx}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'spinsMaster',1)">奖励配置</button>
            </td> 
        </tr> 
    `;

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

function delSpinsMasterLvWrap() {
    if (spinsMasterLvCnt < 1) {
        return;
    }
    $('#spinsMasterLvWrap_' + spinsMasterLvCnt).remove();
    spinsMasterLvCnt--;
}

function delSpinsMasterSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.spinsMasterLvWrap').data('idx')

    if (spinsMasterSpinsStages[lvIdx] < 1) return
    $(obj).parents('.spinsMasterLvWrap').find('.spinsMasterSpinsWrap:last').remove()
    spinsMasterSpinsStages[lvIdx]--
}

function delSpinsMasterShushuWrap(obj) {
    var lvIdx = $(obj).parents('.spinsMasterLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.spinsMasterSpinsWrap').data('idx')

    if (spinsMasterShushuStages[`${lvIdx}_${spinsIdx}`] < 1) return
    $(obj).parents('.spinsMasterSpinsWrap').find('.spinsMasterShushuWrap:last').remove()
    spinsMasterShushuStages[`${lvIdx}_${spinsIdx}`]--
}

function delSpinsMasterRewardTr(obj) {
    var lvIdx = $(obj).parents('.spinsMasterLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.spinsMasterSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.spinsMasterShushuWrap').data('idx')

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

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