function initReward_driftTreasure(type) {
    console.log('init ' + type)
    changeTypeTo_driftTreasure()

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

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

    $.each(reward_info, function (i, o) {
        $('#addDriftTreasureLvWrapBtn').click()
        $(`input[name="driftTreasureConfig[${i}][lvStart]"]`).val(o.lvStart)
        $(`input[name="driftTreasureConfig[${i}][lvEnd]"]`).val(o.lvEnd)
        $.each(o.stages, function (i1, o1) {
            $('.addDriftTreasureSpinsWrapBtn:last').click()
            $(`input[name="driftTreasureConfig[${i}][stages][${i1}][spinsStart]"]`).val(o1.spinsStart)
            $(`input[name="driftTreasureConfig[${i}][stages][${i1}][spinsEnd]"]`).val(o1.spinsEnd)
            $.each(o1.stages, function (i2, o2) {
                $('.addDriftTreasureShushuWrapBtn:last').click()
                $(`select[name="driftTreasureConfig[${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) {
                    $('.addDriftTreasureRewardTrBtn:last').click()
                    $(`input[name="driftTreasureConfig[${i}][stages][${i1}][stages][${i2}][reward][${i3}][stepnum]"]`).val(o3.stepnum)
                    $(`input[name="driftTreasureConfig[${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)
                })
            })
        })
    })
}

function changeTypeTo_driftTreasure() {
    driftTreasureLvCnt = 0
    driftTreasureSpinsStages = {}
    driftTreasureShushuStages = {}
    driftTreasureRewardStages = {}

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

function driftTreasureStageTable() {
    return `
        <div class="form-group text-center" >
            <h4 style="font-weight: bold" class="text-left">收集基础值配置*:</h4>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>触发事件</td>
                        <td>普通偷</td>
                        <td>完美偷</td>
                        <td>打成功</td>
                        <td>打到盾</td>
                    <tr>
                </thead>
                <tr>
                    <td>基础积分值</td>
                    <td><input type="number" placeholder="" name="reward_conf[steal_break]" value="0" class="form-control"></td>
                    <td><input type="number" placeholder="" name="reward_conf[steal_success]" value="0" class="form-control"></td>
                    <td><input type="number" placeholder="" name="reward_conf[attack_success]" value="0" class="form-control"></td>
                    <td><input type="number" placeholder="" name="reward_conf[attack_break]" 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="addDriftTreasureLvWrapBtn" onclick="addDriftTreasureLvWrap(this)" type="button">添加等级层级</button>
            <button class="btn btn-white"   id="delDriftTreasureLvWrapBtn" onclick="delDriftTreasureLvWrap()" type="button">删除等级层级</button>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

function addDriftTreasureLvWrap(obj) {
    driftTreasureLvCnt++
    driftTreasureSpinsStages[driftTreasureLvCnt] = 0

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

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

function addDriftTreasureSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.driftTreasureLvWrap').data('idx')
    var driftTreasureSpinsCnt = ++driftTreasureSpinsStages[lvIdx]
    var spinsIdx = `${lvIdx}_${driftTreasureSpinsCnt}`
    driftTreasureShushuStages[spinsIdx] = 0

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

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


function addDriftTreasureShushuWrap(obj) {
    var lvIdx = $(obj).parents('.driftTreasureLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.driftTreasureSpinsWrap').data('idx')

    var driftTreasureSSCnt = ++driftTreasureShushuStages[`${lvIdx}_${spinsIdx}`]

    var rewardIdx = `${lvIdx}_${spinsIdx}_${driftTreasureSSCnt}`
    driftTreasureRewardStages[rewardIdx] = 0

    var arr = JSON.parse($hideSSGroup);

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

    var html = `
        <div class="driftTreasureShushuWrap" id="driftTreasureShushuWrap_${rewardIdx}" data-idx="${driftTreasureSSCnt}" 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="driftTreasureConfig[${lvIdx}][stages][${spinsIdx}][stages][${driftTreasureSSCnt}][shushu][]"  multiple  style="min-width: 100%"> 
                                    ${optionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th>阶段值</th> 
                    <th>奖品</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="driftTreasureRewardBody_${rewardIdx}"> </tbody>
            <tr>
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addDriftTreasureRewardTrBtn" onclick="addDriftTreasureRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delDriftTreasureRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

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

//体力大师添加进度奖行
function addDriftTreasureRewardTr(obj) {
    var lvIdx = $(obj).parents('.driftTreasureLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.driftTreasureSpinsWrap').data('idx')
    var rewardIdx = $(obj).parents('.driftTreasureShushuWrap').data('idx')

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

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

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

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

function delDriftTreasureLvWrap() {
    if (driftTreasureLvCnt < 1) {
        return;
    }
    $('#driftTreasureLvWrap_' + driftTreasureLvCnt).remove();
    driftTreasureLvCnt--;
}

function delDriftTreasureSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.driftTreasureLvWrap').data('idx')

    if (driftTreasureSpinsStages[lvIdx] < 1) return
    $(obj).parents('.driftTreasureLvWrap').find('.driftTreasureSpinsWrap:last').remove()
    driftTreasureSpinsStages[lvIdx]--
}

function delDriftTreasureShushuWrap(obj) {
    var lvIdx = $(obj).parents('.driftTreasureLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.driftTreasureSpinsWrap').data('idx')

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

function delDriftTreasureRewardTr(obj) {
    var lvIdx = $(obj).parents('.driftTreasureLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.driftTreasureSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.driftTreasureShushuWrap').data('idx')

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

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