function initReward_crazyBuild(type) {
    console.log('init ' + type)
    changetypeTo_crazyBuild();
    
    // 奖励回写
    console.log('reward_info ', reward_info)
    if (!reward_info) {
        return
    }

    $(`input[name="extraData[layer]"]`).val(JSON.parse(extraData).layer)
    $.each(reward_info, (ssIdx, ssItem) => {
        $('#addCrazyBuildSsWrapBtn').click()
        $(`select[name="crazyBuildConfig[${ssIdx}][shushu][]"]`).val(ssItem.shushu)

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

        $.each(ssItem.reward.basicReward, (stageKey, rewardItem) => {
            $('.addCollectRewardTrBtn:last').click()

            $(`input[name="crazyBuildConfig[${ssIdx}][reward][basicReward][${stageKey}][prize]"]`).val(escape(JSON.stringify(rewardItem.prize)))
            $(`input[name="crazyBuildConfig[${ssIdx}][reward][finalReward][${stageKey}][prize]"]`).val(escape(JSON.stringify(ssItem.reward.finalReward[stageKey].prize)))

            let prizeHtml = ''
            $.each(rewardItem.prize, (reKey, reValue) => {
                prizeHtml += chestPropsHtml(reValue)
            })
            $('.basicRewardShow:last').html(prizeHtml)

            prizeHtml = ''
            $.each(ssItem.reward.finalReward[stageKey].prize, (reKey, reValue) => {
                prizeHtml += chestPropsHtml(reValue)
            })
            $('.finalRewardShow:last').html(prizeHtml)
        })
    })
}

function changetypeTo_crazyBuild() {
    $("#reward_info").empty();
    initCrazyBuildWrap();
}

function initCrazyBuildWrap() {
    crazyBuildSSCnt = 0;
    crazyBuildSsStages = {}

    var html = CrazyBuildWrapStageTable();
    $("#reward_info").append(html);
}

function CrazyBuildWrapStageTable() {
    return `
        <div class="form-group" style="margin-bottom: 50px">
            <label class="col-sm-2" style="text-align: center">活动层数（不小于5层）：</label>
            <input class="col-sm-3" oninput="if (this.value < 5) this.value = 5" type="number" name="extraData[layer]" value="0">
        </div>
        <div class="text-right" style="margin:5px;padding:5px;border:solid 1px lightgray;">
            <button style="margin-top: 20px" id="addCrazyBuildSsWrapBtn" class="btn btn-primary" onclick="addCrazyBuildSsWrap(this)" type="button">添加数数分组层级</button>
            <button style="margin-top: 20px" class="btn btn-danger" onclick="delCrazyBuildSsWrap()" type="button">删除数数分组层级</button>
        </div>`
}

function addCrazyBuildSsWrap(obj) {
    var ssIdx = ++crazyBuildSSCnt

    crazyBuildSsStages[ssIdx] = 0

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

    var html = `
        <div class="crazyBuildSsWrap" id="crazyBuildSsWrap_${ssIdx}" data-idx="${ssIdx}" 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="crazyBuildConfig[${ssIdx}][shushu][]"  multiple  style="min-width: 100%"> 
                                    ${optionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">等级</th> 
                    <th>阶段奖励</th> 
                    <th>最终大奖</th> 
                    <th width="120">奖励配置</th> 
                    <th width="120">奖励配置</th> 
                </tr>
            </thead> 
            <tbody id="crazyBuildRewardBody_${ssIdx}"> </tbody>
            <tr>
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addCollectRewardTrBtn" onclick="addCrazyBuildRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delCrazyBuildRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

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

function delCrazyBuildSsWrap() {
    if (crazyBuildSSCnt < 1) {
        return;
    }
    $('#crazyBuildSsWrap_' + crazyBuildSSCnt).remove();
    crazyBuildSSCnt--;
}


function addCrazyBuildRewardTr(obj) {
    var ssIdx = $(obj).parents('.crazyBuildSsWrap').data('idx')
    var rewardStage = ++crazyBuildSsStages[ssIdx]
    var rewardKey = ssIdx + '_' + rewardStage
    var prize = '[]';
    prize = escape(prize);
    var html = `
        <tr class="CrazyBuildRewardInfoTr" id="crazyBuildRewardInfo_${rewardKey}" data-idx=" ${rewardStage} ">
            <td width="120"><input type="text"  class="form-control" value="${rewardStage}" readonly ></td>
            <td class="basicRewardShow"></td>
            <td class="finalRewardShow"></td>

            <td>
                <input type="hidden" class="basicPrizeInput" name="crazyBuildConfig[${ssIdx}][reward][basicReward][${rewardStage}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'crazyBuild_basic',${ssIdx})">阶段奖励配置</button>
            </td>
            <td>
                <input type="hidden" class="finalPrizeInput" name="crazyBuildConfig[${ssIdx}][reward][finalReward][${rewardStage}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'crazyBuild_final',${ssIdx})">最终大奖配置</button>
            </td>
        </tr>`;

    $('#crazyBuildRewardBody_' + ssIdx).append(html)
}

function delCrazyBuildRewardTr(obj) {
    var ssIdx = $(obj).parents('.crazyBuildSsWrap').data('idx')
    var rewardKey = ssIdx + '_' + crazyBuildSsStages[ssIdx]
    if (crazyBuildSsStages[ssIdx] < 1) return;
    $('#crazyBuildRewardInfo_' + rewardKey).remove()
    crazyBuildSsStages[ssIdx]--
}
