function initReward_surprise_collect(type) {
    console.log('init ' + type)
    changeTypeTo_surprise_collect()

    // 奖励回写
    console.log('activity_extra_data', activity_extra_data)
    if (activity_extra_data) {
        $.each(activity_extra_data, function (i, o) {
            $('#addCollectLvWrapBtn').click()
            $('input[name="surpriseCollectConfig[' + i + '][start]"]').val(o.start)
            $('input[name="surpriseCollectConfig[' + i + '][end]"]').val(o.end)
            $.each(o.stages, function (i1, o1) {
                $('.addSurpriseCollectSpinsWrapBtn:last').click()
                $('input[name="surpriseCollectConfig[' + i + '][stages][' + i1 + '][start]"]').val(o1.start)
                $('input[name="surpriseCollectConfig[' + i + '][stages][' + i1 + '][end]"]').val(o1.end)
                $.each(o1.stages, function (i2, o2) {
                    $('.addSurpriseCollectShushuWrapBtn:last').click()
                    $(`select[name="surpriseCollectConfig[${i}][stages][${i1}][stages][${i2}][shushu][]"]`).val(o2.shushu)
                    $(`select[name="surpriseCollectConfig[${i}][stages][${i1}][stages][${i2}][price]"]`).val(o2.price)
                    $(`input[name="surpriseCollectConfig[${i}][stages][${i1}][stages][${i2}][sku]"]`).val(o2.sku)
                    $(`input[name="surpriseCollectConfig[${i}][stages][${i1}][stages][${i2}][lastPrize]"]`).val(escape(JSON.stringify(o2.lastPrize)))
                    var prizeHtml = ''
                    $.each(o2.lastPrize, function (i, prize) {
                        prizeHtml += chestPropsHtml(prize)
                    })
                    $('.prizeShow:last').html(prizeHtml)
                    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 = data.data
                        }
                    });
                    console.log(stage, 'complete')

                    $.each(o2.list, function (i3, o3) {
                        console.log(o3);
                        $('.addSurpriseCollectRewardTrBtn:last').click()
                        $(`input[name="surpriseCollectConfig[${i}][stages][${i1}][stages][${i2}][list][${i3}][stepnum]"]`).val(o3.stepnum)
                        $(`input[name="surpriseCollectConfig[${i}][stages][${i1}][stages][${i2}][list][${i3}][freePrize]"]`).val(escape(JSON.stringify(o3.freePrize)))
                        $(`input[name="surpriseCollectConfig[${i}][stages][${i1}][stages][${i2}][list][${i3}][paiedPrize]"]`).val(escape(JSON.stringify(o3.paiedPrize)))
                        $('.freePrize:last').html(prizeListHtml(o3.freePrize))
                        $('.paiedPrize:last').html(prizeListHtml(o3.paiedPrize))
                    })
                })
            })
        })
    }

}

function changeTypeTo_surprise_collect() {
    console.log('changeToCollect')
    $("#reward_info").empty();
    initSurpriseCollectWrap();
}

function initSurpriseCollectWrap() {
    surprisecollectLvCnt = 0;
    surprisecollectSpinsStages = {}
    surprisecollectShushuStages = {}
    surprisecollectRewardStages = {}
    var html = ``

    html += surpriseCollectWrapStageTable();

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

function surpriseCollectWrapStageTable() {
    return `
        <div class="" id="spinStageTable" style="top:10px">
            <div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">分层级配置</h5></div>
        </div>
        <div class="form-group">
            <div class=" ">
                <button class="btn btn-primary" id="addCollectLvWrapBtn" onclick="addSurpriseCollectLvWrap(this)" type="button">添加等级层级</button>
                <button class="btn btn-white"   id="delCollectLvWrapBtn" onclick="delSurpriseCollectLvWrap()" type="button">删除等级层级</button>
            </div>
      </div>
      <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

function addSurpriseCollectLvWrap(obj) {
    surprisecollectLvCnt++
    surprisecollectSpinsStages[surprisecollectLvCnt] = 0

    var html = `
        <div class="form-group surprisecollectLvWrap " id="surprisecollectLvWrap_${surprisecollectLvCnt}" data-idx="${surprisecollectLvCnt}" 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="surpriseCollectConfig[${surprisecollectLvCnt}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="surpriseCollectConfig[${surprisecollectLvCnt}][end]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addSurpriseCollectSpinsWrapBtn" onclick="addSurpriseCollectSpinsWrap(this)">添加体力层级</botton>
            <botton class="btn btn-danger del-tr delSurpriseCollectSpinsWrapBtn" onclick="delSurpriseCollectSpinsWrap(this)">删除体力层级</botton>
        </div>
`

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

function addSurpriseCollectSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.surprisecollectLvWrap').data('idx')
    var surprisecollectSpinsCnt = ++surprisecollectSpinsStages[lvIdx]
    var spinsIdx = `${lvIdx}_${surprisecollectSpinsCnt}`
    surprisecollectShushuStages[spinsIdx] = 0

    var html = `
        <div class="form-group surprisecollectSpinsWrap " id="surprisecollectSpinsWrap_${spinsIdx}" data-idx="${surprisecollectSpinsCnt}" 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="surpriseCollectConfig[${lvIdx}][stages][${surprisecollectSpinsCnt}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="surpriseCollectConfig[${lvIdx}][stages][${surprisecollectSpinsCnt}][end]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addSurpriseCollectShushuWrapBtn" onclick="addSurpriseCollectShushuWrap(this)">添加数数分组层级</botton>
            <botton class="btn btn-danger del-tr delSurpriseCollectShushuWrapBtn" onclick="delSurpriseCollectShushuWrap(this)">删除数数分组层级</botton>
        </div>
`

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

function addSurpriseCollectShushuWrap(obj) {
    var lvIdx = $(obj).parents('.surprisecollectLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.surprisecollectSpinsWrap').data('idx')

    var surprisecollectSSCnt = ++surprisecollectShushuStages[`${lvIdx}_${spinsIdx}`]

    var rewardIdx = `${lvIdx}_${spinsIdx}_${surprisecollectSSCnt}`
    surprisecollectRewardStages[rewardIdx] = 0

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

    var prize = '[]';
    var prize_show_arr = JSON.parse(prize);
    prize = escape(prize);
    var priceSelector = priceSelectorHtml(`surpriseCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${surprisecollectSSCnt}][price]`)
    var html = `
        <div class="surprisecollectShushuWrap" id="surprisecollectShushuWrap_${rewardIdx}" data-idx="${surprisecollectSSCnt}" 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="surpriseCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${surprisecollectSSCnt}][shushu][]"  multiple  style="min-width: 100%"> 
                                    ${optionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <td colspan="5"> 
                        <div class="form-group priceSkuGroup">
                            <label class="col-sm-1 control-label m-b">解锁价格 </label>
                            <div class="col-sm-3 input-group"> 
                                ${priceSelector}
                            </div>
                            <input class="skuInput" name="surpriseCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${surprisecollectSSCnt}][sku]" type="hidden">
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th>阶段值</th> 
                    <th>免费奖励</th> 
                    <th>付费奖励</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="surprisecollectRewardBody_${rewardIdx}"> </tbody>
            <tr class="commonStageRewardTr"> 
                <td>
                    <input type="text" class="form-control" disabled  value="最终大奖">
                </td> 
                <td class="prizeShow" colspan="3"></td> 
                <td>
                    <input type="hidden" class="prizeInput" name="surpriseCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${surprisecollectSSCnt}][lastPrize]" value="${prize}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td> 
            </tr> 
            <tr>
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addSurpriseCollectRewardTrBtn" onclick="addSurpriseCollectRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delSurpriseCollectRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

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

function addSurpriseCollectRewardTr(obj) {
    var lvIdx = $(obj).parents('.surprisecollectLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.surprisecollectSpinsWrap').data('idx')
    var rewardIdx = $(obj).parents('.surprisecollectShushuWrap').data('idx')

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

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

    var prize = '[]';
    var prize_show_arr = JSON.parse(prize);
    prize = escape(prize);

    var html = `
        <tr id="surprisecollectRewardInfo_${rewardKey}" data-idx="${rIdx}" class="commonStageRewardTr"> 
            <td>
                <input type="text" class="form-control" readonly name="surpriseCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][list][${rIdx}][stage]" value="${rIdx}">
            </td> 
            <td>
                <input type="text" class="form-control"  name="surpriseCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][list][${rIdx}][stepnum]" value="0">
            </td> 
            <td class="freePrize"></td> 
            <td class="paiedPrize"></td> 
            <td>
                <input type="hidden" class="freePrizeInput" name="surpriseCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][list][${rIdx}][freePrize]" value="${prize}">
                <input type="hidden" class="paiedPrizeInput" name="surpriseCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][list][${rIdx}][paiedPrize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize2',1,'freePrize')">免费奖励配置</button>
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize2',1,'paiedPrize')">付费奖励配置</button>
            </td> 
        </tr> 
    `;

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

function delSurpriseCollectLvWrap() {
    if (surprisecollectLvCnt < 1) {
        return;
    }
    $('#surprisecollectLvWrap_' + surprisecollectLvCnt).remove();
    surprisecollectLvCnt--;
}

function delSurpriseCollectSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.surprisecollectLvWrap').data('idx')

    if (surprisecollectSpinsStages[lvIdx] < 1) return
    $(obj).parents('.surprisecollectLvWrap').find('.surprisecollectSpinsWrap:last').remove()
    surprisecollectSpinsStages[lvIdx]--
}

function delSurpriseCollectShushuWrap(obj) {
    var lvIdx = $(obj).parents('.surprisecollectLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.surprisecollectSpinsWrap').data('idx')

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

function delSurpriseCollectRewardTr(obj) {
    var lvIdx = $(obj).parents('.surprisecollectLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.surprisecollectSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.surprisecollectShushuWrap').data('idx')

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

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


