function initReward_super_collect(type) {
    console.log('init ' + type)
    changeTypeTo_super_collect()

    // 收集物回写
    console.log('reward_conf', reward_conf)
    var groupId = reward_conf.groupId ? reward_conf.groupId : 1;
    $('#collectChildGroupBasic').empty();
    var data = {
        "groupId": groupId,
        "attack_break": 0,
        "attack_success": 0,
        "steal_break": 0,
        "steal_success": 0,
        "one_hit": 0,
        "two_hit": 0,
        "three_hit": 0,
        "one_attack": 0,
        "two_attack": 0,
        "one_steal": 0,
        "two_steal": 0
    };
    $('#collectChildGroupBasic').append(collectChildGroupBasicCfg(data));
    selGroupId = parseInt(groupId);
    $('select[name=groupId]').val(groupId)
    console.log('reward_conf', reward_conf)
    if (reward_conf) {
        $.each(reward_conf, function (i, o) {
            if (i !== 'groupId') {
                $('input[name=' + i + ']').val(o)
            }
        })
    }

    // 奖励回写
    console.log('reward_info', reward_info)
    if (reward_info) {
        $.each(reward_info, function (i, o) {
            $('#addCollectLvWrapBtn').click()
            $('input[name="superCollectConfig[' + i + '][lvStart]"]').val(o.lvStart)
            $('input[name="superCollectConfig[' + i + '][lvEnd]"]').val(o.lvEnd)
            $.each(o.stages, function (i1, o1) {
                $('.addCollectSpinsWrapBtn:last').click()
                $('input[name="superCollectConfig[' + i + '][stages][' + i1 + '][spinsStart]"]').val(o1.spinsStart)
                $('input[name="superCollectConfig[' + i + '][stages][' + i1 + '][spinsEnd]"]').val(o1.spinsEnd)
                $.each(o1.stages, function (i2, o2) {
                    $('.addCollectShushuWrapBtn:last').click()
                    $('select[name="superCollectConfig[' + 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.list = data.data
                        }
                    });
                    console.log(stage, 'complete')

                    $.each(o2.list, function (i3, o3) {
                        $('.addCollectRewardTrBtn:last').click()
                        $('input[name="superCollectConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][list][' + i3 + '][stageSpins]"]').val(o3.stageSpins)
                        $('input[name="superCollectConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][list][' + i3 + '][stepnum]"]').val(o3.stepnum)
                        $('input[name="superCollectConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][list][' + 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_super_collect() {
    console.log('changeToCollect')
    $("#reward_info").empty();
    initCollectWrap();
}

function initCollectWrap() {
    collectLvCnt = 0;
    collectSpinsStages = {}
    collectShushuStages = {}
    collectRewardStages = {}
    $("#reward_info").append(addTimeLimit()); // 限时配置

    $("#reward_info").append(setProChange());   //体力及阶段对概率的修正

    $("#reward_info").append(slotWeightGroupsSelector('weightGroupId', reward_conf['weightGroupId'])); // 老虎机概率分组选择
    var html = ''
    var data = {
        "groupId": 1,
        "attack_break": 0,
        "attack_success": 0,
        "steal_break": 0,
        "steal_success": 0,
        "one_hit": 0,
        "two_hit": 0,
        "three_hit": 0,
        "one_attack": 0,
        "two_attack": 0,
        "one_steal": 0,
        "two_steal": 0
    };
    html += addStagePro('super_collect', data);
    html += collectWrapStageTable();

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


function collectWrapStageTable() {
    return `
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
        <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="addCollectLvWrap(this)" type="button">添加等级层级</button>
                <button class="btn btn-white"   id="delCollectLvWrapBtn" onclick="delCollectLvWrap()" type="button">删除等级层级</button>
            </div>
      </div>
      <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

function addCollectLvWrap(obj) {
    collectLvCnt++
    collectSpinsStages[collectLvCnt] = 0

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

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

function addCollectSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.collectLvWrap').data('idx')
    var collectSpinsCnt = ++collectSpinsStages[lvIdx]
    var spinsIdx = `${lvIdx}_${collectSpinsCnt}`
    collectShushuStages[spinsIdx] = 0

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

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

function addCollectShushuWrap(obj) {
    var lvIdx = $(obj).parents('.collectLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.collectSpinsWrap').data('idx')

    var collectSSCnt = ++collectShushuStages[`${lvIdx}_${spinsIdx}`]

    var rewardIdx = `${lvIdx}_${spinsIdx}_${collectSSCnt}`
    collectRewardStages[rewardIdx] = 0

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

    var html = `
        <div class="collectShushuWrap" id="collectShushuWrap_${rewardIdx}" data-idx="${collectSSCnt}" 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="superCollectConfig[${lvIdx}][stages][${spinsIdx}][stages][${collectSSCnt}][shushu][]"  multiple  style="min-width: 100%"> 
                                    ${optionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th>预设消耗体力</th> 
                    <th>阶段值</th> 
                    <th>奖品</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="collectRewardBody_${rewardIdx}"> </tbody>
            <tr>
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addCollectRewardTrBtn" onclick="addCollectRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delCollectRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

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

function addCollectRewardTr(obj) {
    var lvIdx = $(obj).parents('.collectLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.collectSpinsWrap').data('idx')
    var rewardIdx = $(obj).parents('.collectShushuWrap').data('idx')

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

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

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

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

function delCollectLvWrap() {
    if (collectLvCnt < 1) {
        return;
    }
    $('#collectLvWrap_' + collectLvCnt).remove();
    collectLvCnt--;
}

function delCollectSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.collectLvWrap').data('idx')

    if (collectSpinsStages[lvIdx] < 1) return
    $(obj).parents('.collectLvWrap').find('.collectSpinsWrap:last').remove()
    collectSpinsStages[lvIdx]--
}

function delCollectShushuWrap(obj) {
    var lvIdx = $(obj).parents('.collectLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.collectSpinsWrap').data('idx')

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

function delCollectRewardTr(obj) {
    var lvIdx = $(obj).parents('.collectLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.collectSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.collectShushuWrap').data('idx')

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

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


