function initReward_greatMemories(type) {
    console.log('init ' + type)
    changeTypeTo_greatMemories()

    // 奖励回写
    console.log('reward_info', reward_info)
    if (!reward_info) {
        return
    }

    const extraInfo = JSON.parse(extraData)
    for (const field in extraInfo) {
        $(`input[name="extraData[${field}]"]`).val(extraInfo[field])
    }

    $.each(reward_info, function (i, o) {
        $('#addGreatMemoriesLvWrapBtn').click()
        $(`input[name="greatMemoriesConfig[${i}][lvStart]"]`).val(o.lvStart)
        $(`input[name="greatMemoriesConfig[${i}][lvEnd]"]`).val(o.lvEnd)
        $.each(o.stages, function (i1, o1) {
            $('.addGreatMemoriesVipScoreWrapBtn:last').click()
            $(`input[name="greatMemoriesConfig[${i}][stages][${i1}][VipStart]"]`).val(o1.VipStart)
            $(`input[name="greatMemoriesConfig[${i}][stages][${i1}][VipEnd]"]`).val(o1.VipEnd)
            $.each(o1.stages, function (i2, o2) {
                $('.addGreatMemoriesLostDaysWrapBtn:last').click()
                $(`input[name="greatMemoriesConfig[${i}][stages][${i1}][stages][${i2}][lostDayStart]"]`).val(o2.lostDayStart)
                $(`input[name="greatMemoriesConfig[${i}][stages][${i1}][stages][${i2}][lostDayEnd]"]`).val(o2.lostDayEnd)
                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')

                $(`input[name="greatMemoriesConfig[${i}][stages][${i1}][stages][${i2}][act_end_time]"]`).val(o2.act_end_time)
                o2.rewards && $.each(o2.rewards, function (i3, o3) {
                    $('.addGreatMemoriesRewardTrBtn:last').click()
                    $(`input[name="greatMemoriesConfig[${i}][stages][${i1}][stages][${i2}][rewards][${i3}][cool_down_time]"]`).val(o3.cool_down_time)
                    // $(`input[name="greatMemoriesConfig[${i}][stages][${i1}][stages][${i2}][rewards][${i3}][timeout]"]`).val(o3.timeout)
                    $(`input[name="greatMemoriesConfig[${i}][stages][${i1}][stages][${i2}][rewards][${i3}][prizes]"]`).val(escape(JSON.stringify(o3.prizes)))
                    var prizeHtml = ''
                    $.each(o3.prizes, function (i, prize) {
                        prizeHtml += chestPropsHtml(prize)
                    })
                    $('.prizeShow:last').html(prizeHtml)
                })
            })
        })
    })
}

function changeTypeTo_greatMemories() {
    greatMemoriesLvCnt = 0
    greatMemoriesVipScoreStages = {}
    greatMemoriesLostDaysStages = {}
    greatMemoriesRewardStages = {}

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

function greatMemoriesStageTable() {
    return `
        <div class="form-group text-center" >
            <h4 style="font-weight: bold" class="text-left">收集基础值配置*:</h4>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>奖励1倒计时(分钟)</td>
                        <td>再次触发活动间隔(天)</td>
<!--                        <td>完美偷</td>-->
<!--                        <td>打成功</td>-->
<!--                        <td>打到盾</td>-->
                    <tr>
                </thead>
                <tr>
                    <td><input type="number" placeholder="" name="extraData[reward_end_time]" value="0" class="form-control"></td>
                    <td>
                        <input type="number" placeholder="" name="extraData[act_gap_day]" value="0" class="form-control">
<!--                        <input type="hidden" placeholder="" name="extraData[act_gap_time]" value="0" class="form-control">-->
                    </td>
<!--                    <td><input type="number" placeholder="" name="extraData[perfectRaid]" value="0" class="form-control"></td>-->
<!--                    <td><input type="number" placeholder="" name="extraData[attackSuccess]" value="0" class="form-control"></td>-->
<!--                    <td><input type="number" placeholder="" name="extraData[attackFail]" value="0" class="form-control"></td>-->
                </tr>
            </table>
        </div>
        <div class="form-group text-center">
            <h4 style="font-weight: bold" class="text-left">阶段奖励配置*:</h4>
            <button class="btn btn-primary" id="addGreatMemoriesLvWrapBtn" onclick="addGreatMemoriesLvWrap(this)" type="button">添加等级层级</button>
            <button class="btn btn-white"   id="delGreatMemoriesLvWrapBtn" onclick="delGreatMemoriesLvWrap()" type="button">删除等级层级</button>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

function addGreatMemoriesLvWrap(obj) {
    greatMemoriesLvCnt++
    greatMemoriesVipScoreStages[greatMemoriesLvCnt] = 0

    var html = `
        <div class="form-group greatMemoriesLvWrap " id="greatMemoriesLvWrap_${greatMemoriesLvCnt}" data-idx="${greatMemoriesLvCnt}" 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="greatMemoriesConfig[${greatMemoriesLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="greatMemoriesConfig[${greatMemoriesLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addGreatMemoriesVipScoreWrapBtn" onclick="addGreatMemoriesVipScoreWrap(this)">添加VIP积分层级</botton>
            <botton class="btn btn-danger del-tr delGreatMemoriesVipScoreWrapBtn" onclick="delGreatMemoriesVipScoreWrap(this)">删除VIP积分层级</botton>
        </div>
`

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

function addGreatMemoriesVipScoreWrap(obj) {
    var lvIdx = $(obj).parents('.greatMemoriesLvWrap').data('idx')
    var greatMemoriesVipScoreCnt = ++greatMemoriesVipScoreStages[lvIdx]
    var vipIdx = `${lvIdx}_${greatMemoriesVipScoreCnt}`
    greatMemoriesLostDaysStages[vipIdx] = 0

    var html = `
        <div class="form-group greatMemoriesVipScoreWrap " id="greatMemoriesVipScoreWrap_${vipIdx}" data-idx="${greatMemoriesVipScoreCnt}" style="margin:10px;padding:10px;border:solid 1px black;">
            <div class="form-group">
                <label class="col-sm-1 control-label m-b">VIP积分区间: </label>
                <div class="col-sm-3 input-group">
                    <input type="text" style="text-align: center" class="form-control" name="greatMemoriesConfig[${lvIdx}][stages][${greatMemoriesVipScoreCnt}][VipStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="greatMemoriesConfig[${lvIdx}][stages][${greatMemoriesVipScoreCnt}][VipEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addGreatMemoriesLostDaysWrapBtn" onclick="addGreatMemoriesLostDaysWrap(this)">添加流失小时层级</botton>
            <botton class="btn btn-danger del-tr delGreatMemoriesLostDaysWrapBtn" onclick="delGreatMemoriesLostDaysWrap(this)">删除流失小时层级</botton>
        </div>
    `

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


function addGreatMemoriesLostDaysWrap(obj) {
    var lvIdx = $(obj).parents('.greatMemoriesLvWrap').data('idx')
    var vipIdx = $(obj).parents('.greatMemoriesVipScoreWrap').data('idx')

    var greatMemoriesSSCnt = ++greatMemoriesLostDaysStages[`${lvIdx}_${vipIdx}`]

    var rewardIdx = `${lvIdx}_${vipIdx}_${greatMemoriesSSCnt}`
    greatMemoriesRewardStages[rewardIdx] = 0

    var arr = JSON.parse($hideSSGroup);

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

    var html = `
        <div class="greatMemoriesLostDaysWrap" id="greatMemoriesLostDaysWrap_${rewardIdx}" data-idx="${greatMemoriesSSCnt}" 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">
                                <input type="text" style="text-align: center" class="form-control" name="greatMemoriesConfig[${lvIdx}][stages][${vipIdx}][stages][${greatMemoriesSSCnt}][lostDayStart]" value="0">
                                <span class="input-group-addon">-</span>
                                <input type="text" style="text-align: center" class="form-control" name="greatMemoriesConfig[${lvIdx}][stages][${vipIdx}][stages][${greatMemoriesSSCnt}][lostDayEnd]" value="0">
                            </div>
                            <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="greatMemoriesConfig[${lvIdx}][stages][${vipIdx}][stages][${greatMemoriesSSCnt}][act_end_time]" value="0">
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th>领奖冷却时间(秒)</th> 
<!--                    <th>领奖倒计时</th> -->
                    <th>奖品</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="greatMemoriesRewardBody_${rewardIdx}"> </tbody>
            <tr>
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addGreatMemoriesRewardTrBtn" onclick="addGreatMemoriesRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delGreatMemoriesRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

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

// 添加进度奖行
function addGreatMemoriesRewardTr(obj) {
    var lvIdx = $(obj).parents('.greatMemoriesLvWrap').data('idx')
    var vipIdx = $(obj).parents('.greatMemoriesVipScoreWrap').data('idx')
    var rewardIdx = $(obj).parents('.greatMemoriesLostDaysWrap').data('idx')

    var lsrIdx = `${lvIdx}_${vipIdx}_${rewardIdx}`
    var rIdx = ++greatMemoriesRewardStages[lsrIdx]

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

    var prize = '[]';
    var prize_show_arr = JSON.parse(prize);
    prize = escape(prize);
    var html = `
        <tr id="greatMemoriesRewardInfo_${rewardKey}" data-idx="${rIdx}" class="commonStageRewardTr"> 
            <td>
                <input type="text" class="form-control" readonly name="greatMemoriesConfig[${lvIdx}][stages][${vipIdx}][stages][${rewardIdx}][rewards][${rIdx}][stage]" value="${rIdx}">
            </td> 
            <td>
                <input type="text" class="form-control"  name="greatMemoriesConfig[${lvIdx}][stages][${vipIdx}][stages][${rewardIdx}][rewards][${rIdx}][cool_down_time]" value="0">
            </td> 
<!--            <td>-->
<!--                <input type="text" class="form-control"  name="greatMemoriesConfig[${lvIdx}][stages][${vipIdx}][stages][${rewardIdx}][rewards][${rIdx}][timeout]" value="0">-->
<!--            </td> -->
            <td class="prizeShow"></td> 
            <td>
                <input type="hidden" class="prizeInput" name="greatMemoriesConfig[${lvIdx}][stages][${vipIdx}][stages][${rewardIdx}][rewards][${rIdx}][prizes]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
            </td> 
        </tr> 
    `;

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

function delGreatMemoriesLvWrap() {
    if (greatMemoriesLvCnt < 1) {
        return;
    }
    $('#greatMemoriesLvWrap_' + greatMemoriesLvCnt).remove();
    greatMemoriesLvCnt--;
}

function delGreatMemoriesVipScoreWrap(obj) {
    var lvIdx = $(obj).parents('.greatMemoriesLvWrap').data('idx')

    if (greatMemoriesVipScoreStages[lvIdx] < 1) return
    $(obj).parents('.greatMemoriesLvWrap').find('.greatMemoriesVipScoreWrap:last').remove()
    greatMemoriesVipScoreStages[lvIdx]--
}

function delGreatMemoriesLostDaysWrap(obj) {
    var lvIdx = $(obj).parents('.greatMemoriesLvWrap').data('idx')
    var vipIdx = $(obj).parents('.greatMemoriesVipScoreWrap').data('idx')

    if (greatMemoriesLostDaysStages[`${lvIdx}_${vipIdx}`] < 1) return
    $(obj).parents('.greatMemoriesVipScoreWrap').find('.greatMemoriesLostDaysWrap:last').remove()
    greatMemoriesLostDaysStages[`${lvIdx}_${vipIdx}`]--
}

function delGreatMemoriesRewardTr(obj) {
    var lvIdx = $(obj).parents('.greatMemoriesLvWrap').data('idx')
    var vipIdx = $(obj).parents('.greatMemoriesVipScoreWrap').data('idx')
    var lostDaysIdx = $(obj).parents('.greatMemoriesLostDaysWrap').data('idx')

    var rewardIdx = `${lvIdx}_${vipIdx}_${lostDaysIdx}`
    var rewardKey = rewardIdx + '_' + greatMemoriesRewardStages[rewardIdx]

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