function initReward_collect_jigsw(type) {
    console.log('init ' + type)
    changeTypeTo_collect_jigsw();

    // 奖励回写
    console.log('reward_info', reward_info)
    if (!reward_info) {
        return
    }
    $.each(reward_info, function (i, o) {
        $('#addJigsawLvWrapBtn').click()
        $('input[name="jigsawConfig[' + i + '][lvStart]"]').val(o.lvStart)
        $('input[name="jigsawConfig[' + i + '][lvEnd]"]').val(o.lvEnd)
        $.each(o.stages, function (i1, o1) {
            $('.addJigsawSpinsWrapBtn:last').click()
            $('input[name="jigsawConfig[' + i + '][stages][' + i1 + '][spinsStart]"]').val(o1.spinsStart)
            $('input[name="jigsawConfig[' + i + '][stages][' + i1 + '][spinsEnd]"]').val(o1.spinsEnd)
            $.each(o1.stages, function (i2, o2) {
                $('.addJigsawShushuWrapBtn:last').click()
                $('select[name="jigsawConfig[' + 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) {
                    $('.addJigsawRewardTrBtn:last').click()
                    $('select[name="jigsawConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][reward][' + i3 + '][stepnum]"]').val(o3.stepnum)
                    $('select[name="jigsawConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][reward][' + i3 + '][blockimg]"]').val(o3.blockimg)
                    $('input[name="jigsawConfig[' + 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)
                })
            })
        })
    })
    $('.select2').select2({})
}

function changeTypeTo_collect_jigsw() {
    jigsawLvCnt = 0
    jigsawSpinsStages = {}
    jigsawShushuStages = {}
    jigsawRewardStages = {}

    $('#reward_info').empty()
    var html = jigsawFirstWrap();
    $('#reward_info').html(html)

}

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

function addJigsawLvWrap(obj) {
    jigsawLvCnt++
    jigsawSpinsStages[jigsawLvCnt] = 0

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

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

function addJigsawSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.jigsawLvWrap').data('idx')
    var jigsawSpinsCnt = ++jigsawSpinsStages[lvIdx]
    var spinsIdx = `${lvIdx}_${jigsawSpinsCnt}`
    jigsawShushuStages[spinsIdx] = 0

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

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


function addJigsawShushuWrap(obj) {
    var lvIdx = $(obj).parents('.jigsawLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.jigsawSpinsWrap').data('idx')

    var jigsawSSCnt = ++jigsawShushuStages[`${lvIdx}_${spinsIdx}`]

    var rewardIdx = `${lvIdx}_${spinsIdx}_${jigsawSSCnt}`
    jigsawRewardStages[rewardIdx] = 0

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

    var html = `
        <div class="jigsawShushuWrap" id="jigsawShushuWrap_${rewardIdx}" data-idx="${jigsawSSCnt}" 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="jigsawConfig[${lvIdx}][stages][${spinsIdx}][stages][${jigsawSSCnt}][shushu][]"  multiple  style="min-width: 100%"> 
                                    ${optionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th width="200">拼图块数</th> 
                    <th width="300">拼图资源</th> 
                    <th>奖品</th> 
                    <th width="180">奖励设置</th>
                </tr>
            </thead> 
            <tbody id="jigsawRewardBody_${rewardIdx}"> </tbody>
            <tr>
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addJigsawRewardTrBtn" onclick="addJigsawRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delJigsawRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

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

//体力大师添加进度奖行
function addJigsawRewardTr(obj) {
    var lvIdx = $(obj).parents('.jigsawLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.jigsawSpinsWrap').data('idx')
    var rewardIdx = $(obj).parents('.jigsawShushuWrap').data('idx')

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

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

    var optionsHtml = ''
    $.each([4, 5, 6, 7, 8, 9], function (i, o) {
        optionsHtml += `<option value="${o}">${o}</option>`
    });

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

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

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

function delJigsawLvWrap() {
    if (jigsawLvCnt < 1) {
        return;
    }
    $('#jigsawLvWrap_' + jigsawLvCnt).remove();
    jigsawLvCnt--;
}

function delJigsawSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.jigsawLvWrap').data('idx')

    if (jigsawSpinsStages[lvIdx] < 1) return
    $(obj).parents('.jigsawLvWrap').find('.jigsawSpinsWrap:last').remove()
    jigsawSpinsStages[lvIdx]--
}

function delJigsawShushuWrap(obj) {
    var lvIdx = $(obj).parents('.jigsawLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.jigsawSpinsWrap').data('idx')

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

function delJigsawRewardTr(obj) {
    var lvIdx = $(obj).parents('.jigsawLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.jigsawSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.jigsawShushuWrap').data('idx')

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

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