function initReward_luckyFox(type) {
    console.log('init ' + type)
    changetypeTo_luckyFox()

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

    $.each(reward_info, function (lvStage, lvItem) {
        $('#addLuckyFoxLvWrapBtn').click()
        $('input[name="luckyFoxConfig[' + lvStage + '][lvStart]"]').val(lvItem.lvStart)
        $('input[name="luckyFoxConfig[' + lvStage + '][lvEnd]"]').val(lvItem.lvEnd)
        $.each(lvItem.stages, function (spinsStage, spinsItem) {
            $('.addLuckyFoxSpinsWrapBtn:last').click()

            $('input[name="luckyFoxConfig[' + lvStage + '][stages][' + spinsStage + '][spinsStart]"]').val(spinsItem.spinsStart)
            $('input[name="luckyFoxConfig[' + lvStage + '][stages][' + spinsStage + '][spinsEnd]"]').val(spinsItem.spinsEnd)
            // var stage = lvStage + '_' + spinsStage
            // $.ajax({
            //     type: "GET", // 数据提交类型
            //     url: "activity/getActStageReward/" + $asyncId + '/' + stage, // 发送地址
            //     data: {}, //发送数据
            //     async: false, // 是否异步
            //     success: function (data) { // 成功回调
            //         console.log(stage, 'finish')
            //         data = JSON.parse(data);
            //         spinsItem.list = data.data
            //     }
            // });
            // console.log(stage, 'complete')
            $.each(spinsItem.list, function (rewardIndex, rewardItem) {
                $('.addLuckyFoxRewardTrBtn:last').click()
                $('input[name="luckyFoxConfig[' + lvStage + '][stages][' + spinsStage + '][list][' + rewardIndex + '][grid]"]').val(rewardItem.grid)
                $('input[name="luckyFoxConfig[' + lvStage + '][stages][' + spinsStage + '][list][' + rewardIndex + '][layer]"]').val(rewardItem.layer)
                $('input[name="luckyFoxConfig[' + lvStage + '][stages][' + spinsStage + '][list][' + rewardIndex + '][weight]"]').val(rewardItem.weight)
                $('input[name="luckyFoxConfig[' + lvStage + '][stages][' + spinsStage + '][list][' + rewardIndex + '][prize]"]').val(escape(JSON.stringify(rewardItem.prize)))

                var prizeHtml = ''
                $.each(rewardItem.prize, function (i, prize) {
                    prizeHtml += chestPropsHtml(prize)
                })
                $('.prizeShow:last').html(prizeHtml)
            })
        })
    })
}

function changetypeTo_luckyFox() {
    $("#reward_info").empty();
    initLuckyFoxWrap();
}

function initLuckyFoxWrap() {
    LuckyFoxLvCnt = 0;
    LuckyFoxSpinsStages = {}
    LuckyFoxRewardStages = {}

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

function LuckyFoxWrapStageTable() {
    return `
         <div class="text-right" style="margin:5px;padding:5px;border:solid 1px lightgray;">
            <button style="margin-top: 20px" id="addLuckyFoxLvWrapBtn" class="btn btn-primary" onclick="addLuckyFoxLvWrap(this)" type="button">添加等级区间</button>
            <button style="margin-top: 20px" class="btn btn-danger" onclick="delLuckyFoxLvWrap()" type="button">删除等级区间</button>
         </div>`
}

function addLuckyFoxLvWrap(obj) {
    LuckyFoxLvCnt++
    LuckyFoxSpinsStages[LuckyFoxLvCnt] = 0
    var html = `
        <div class="form-group text-left LuckyFoxLvWrap" id="LuckyFoxLvWrap_${LuckyFoxLvCnt}" data-idx="${LuckyFoxLvCnt}" style="margin:10px;padding:5px;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="luckyFoxConfig[${LuckyFoxLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="luckyFoxConfig[${LuckyFoxLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton style="margin-top: 10px" class="btn btn-primary  addLuckyFoxSpinsWrapBtn" onclick="addLuckyFoxSpinsWrap( this)">添加体力分组层级</botton>
            <botton style="margin-top: 10px" class="btn btn-danger del-tr delLuckyFoxSpinsWrapBtn" onclick="delLuckyFoxSpinsWrap( this)">删除体力分组层级</botton>
        </div>`
    $(obj).before(html)
}

function delLuckyFoxLvWrap() {
    if (LuckyFoxLvCnt < 1) {
        return;
    }
    $('#LuckyFoxLvWrap_' + LuckyFoxLvCnt).remove();
    LuckyFoxLvCnt--;
}

function addLuckyFoxSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.LuckyFoxLvWrap').data('idx')
    LuckyFoxSpinsStages[lvIdx]++
    var rewardIdx = lvIdx + '_' + LuckyFoxSpinsStages[lvIdx]
    LuckyFoxRewardStages[rewardIdx] = 0
    var html = `
        <div style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            <table class="table table-bordered LuckyFoxSpinsWrap" id="LuckyFoxSpinsWrap_${rewardIdx}" data-idx="${LuckyFoxSpinsStages[lvIdx]}">
                <thead>
                    <tr>
                        <td colspan="7">
                             <div class="form-group">
                                <label class="col-sm-2 control-label m-b">体力区间: </label>
                                <div class="col-sm-3 input-group">
                                    <input type="text" style="text-align: center" class="form-control" name="luckyFoxConfig[${lvIdx}][stages][${LuckyFoxSpinsStages[lvIdx]}][spinsStart]" value="0">
                                    <span class="input-group-addon">-</span>
                                    <input type="text" style="text-align: center" class="form-control" name="luckyFoxConfig[${lvIdx}][stages][${LuckyFoxSpinsStages[lvIdx]}][spinsEnd]" value="0">
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <th width="120">Index</th>
                        <th>格数</th>
                        <th>层级</th>
                        <th>权重</th>
                        <th>奖励</th>
                        <th width="120">奖励设置</th>
                    </tr>
                </thead>
                <tbody id="LuckyFoxRewardBody_${rewardIdx}"></tbody>
                <tr>
                    <td colspan="7">
                        <div class="text-center" style="margin-top: 20px">
                            <botton class="btn btn-primary addLuckyFoxRewardTrBtn" onclick="addLuckyFoxRewardTr(this)">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delLuckyFoxRewardTr(this)">删除</botton>
                        </div>
                    </td>
                </tr>
           </table>
        </div>`
    $(obj).before(html)
}

function delLuckyFoxSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.LuckyFoxLvWrap').data('idx')
    if (LuckyFoxSpinsStages[lvIdx] < 1) return
    $(obj).parents('.LuckyFoxLvWrap').find('.LuckyFoxSpinsWrap:last').remove()
    LuckyFoxSpinsStages[lvIdx]--
}

function addLuckyFoxRewardTr(obj) {
    var lvIdx = $(obj).parents('.LuckyFoxLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.LuckyFoxSpinsWrap').data('idx')
    var rewardIdx = lvIdx + '_' + spinsIdx
    var rewardStage = ++LuckyFoxRewardStages[rewardIdx]
    var rewardKey = lvIdx + '_' + spinsIdx + '_' + rewardStage
    var prize = '[]';
    prize = escape(prize);
    var html = `
        <tr class="LuckyFoxRewardInfoTr" id="LuckyFoxRewardInfo_${rewardKey}" data-idx=" ${rewardStage} ">
            <td width="120"><input type="text"  class="form-control" value="${rewardStage}" readonly ></td>
            <td><input name="luckyFoxConfig[${lvIdx}][stages][${spinsIdx}][list][${rewardStage}][grid]" type="text"  class="form-control" value="0"></td>
            <td><input name="luckyFoxConfig[${lvIdx}][stages][${spinsIdx}][list][${rewardStage}][layer]" type="text"  class="form-control" value="0"></td>
            <td><input name="luckyFoxConfig[${lvIdx}][stages][${spinsIdx}][list][${rewardStage}][weight]" type="text"  class="form-control" value="0"></td>
            <td class="prizeShow"></td>
            <td>
                <input type="hidden" class="prizeInput" name="luckyFoxConfig[${lvIdx}][stages][${spinsIdx}][list][${rewardStage}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,\'luckyFox\',${lvIdx},${spinsIdx})">奖励配置</button>
            </td>
        </tr>`;

    $('#LuckyFoxRewardBody_' + rewardIdx).append(html)
}

function delLuckyFoxRewardTr(obj) {
    var lvIdx = $(obj).parents('.LuckyFoxLvWrap').data('idx')
    var SpinsIdx = $(obj).parents('.LuckyFoxSpinsWrap').data('idx')
    var rewardIdx = lvIdx + '_' + SpinsIdx
    var rewardKey = lvIdx + '_' + SpinsIdx + '_' + LuckyFoxRewardStages[rewardIdx]
    if (LuckyFoxRewardStages[rewardIdx] < 1) return;
    $('#LuckyFoxRewardInfo_' + rewardKey).remove()
    LuckyFoxRewardStages[rewardIdx]--
}
