let luckyJourneyLvCnt = 0
let luckyJourneySpinsStages = {}
let luckyJourneyShushuStages = {}
let luckyJourneyStageRewardStages = {}
let luckyJourneyDiceWeightStages = {}
let luckyJourneyTokenShopStages = {}

function initReward_luckyJourney(type) {
    console.log('init ' + type)
    changeTypeTo_luckyJourney()

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

    $.each(reward_info, function (i, o) {
        $('#addLuckyJourneyLvWrapBtn').click()
        $(`input[name="luckyJourneyConfig[${i}][lvStart]"]`).val(o.lvStart)
        $(`input[name="luckyJourneyConfig[${i}][lvEnd]"]`).val(o.lvEnd)
        $.each(o.stages, function (i1, o1) {
            $('.addLuckyJourneySpinsWrapBtn:last').click()
            $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][spinsStart]"]`).val(o1.spinsStart)
            $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][spinsEnd]"]`).val(o1.spinsEnd)

            console.log(27, o1);
            $.each(o1.reward.dice_weight, function (i2, o2) {
                $('.addLuckyJourneyDiceWeightTrBtn:last').click()
                $(`select[name="luckyJourneyConfig[${i}][stages][${i1}][reward][dice_weight][${i2}][dice_type]"]`).val(o2.dice_type)
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][dice_weight][${i2}][dice_num]"]`).val(o2.dice_num)
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][dice_weight][${i2}][weight]"]`).val(o2.weight)
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][dice_weight][${i2}][limit]"]`).val(o2.limit)
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][dice_weight][${i2}][min_cost]"]`).val(o2.min_cost)
            })

            $.each(o1.reward.stage_reward, function (i2, o2) {
                $('.addLuckyJourneyStageRewardTrBtn:last').click()
                $(`select[name="luckyJourneyConfig[${i}][stages][${i1}][reward][stage_reward][${i2}][bonus]"]`).val(o2.bonus)
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][stage_reward][${i2}][target]"]`).val(o2.target)
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][stage_reward][${i2}][prize]"]`).val(escape(JSON.stringify(o2.prize)))

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

            $.each(o1.reward.token_shop, function (i2, o2) {
                $('.addLuckyJourneyTokenShopTrBtn:last').click()
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][token_shop][${i2}][price]"]`).val(o2.price)
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][token_shop][${i2}][limit]"]`).val(o2.limit)
                $(`input[name="luckyJourneyConfig[${i}][stages][${i1}][reward][token_shop][${i2}][prize]"]`).val(escape(JSON.stringify(o2.prize)))

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

            renderNotokenAlert(`${i}_${i1}`, activity_extra_data)
        })
    })
}

function changeTypeTo_luckyJourney() {
    luckyJourneyLvCnt = 0
    luckyJourneySpinsStages = {}
    luckyJourneyShushuStages = {}
    luckyJourneyStageRewardStages = {}
    luckyJourneyDiceWeightStages = {}
    luckyJourneyTokenShopStages = {}

    $('#reward_info').empty()

    let html = luckyJourneyStageTable();
    $("#reward_info").append(html);
}

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

function addLuckyJourneyLvWrap(obj) {
    luckyJourneyLvCnt++
    luckyJourneySpinsStages[luckyJourneyLvCnt] = 0

    let html = `
        <div class="form-group luckyJourneyLvWrap " id="luckyJourneyLvWrap_${luckyJourneyLvCnt}" data-idx="${luckyJourneyLvCnt}" 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="luckyJourneyConfig[${luckyJourneyLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="luckyJourneyConfig[${luckyJourneyLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addLuckyJourneySpinsWrapBtn" onclick="addLuckyJourneySpinsWrap(this)">添加体力层级</botton>
            <botton class="btn btn-danger del-tr delLuckyJourneySpinsWrapBtn" onclick="delLuckyJourneySpinsWrap(this)">删除体力层级</botton>
        </div>
`

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

function delLuckyJourneyLvWrap() {
    if (luckyJourneyLvCnt < 1) {
        return;
    }
    $('#luckyJourneyLvWrap_' + luckyJourneyLvCnt).remove();
    luckyJourneyLvCnt--;
}


function addLuckyJourneySpinsWrap(obj) {

    let lvIdx = $(obj).parents('.luckyJourneyLvWrap').data('idx')
    let luckyJourneySpinsCnt = ++luckyJourneySpinsStages[lvIdx]
    let rewardIdx = `${lvIdx}_${luckyJourneySpinsCnt}`
    luckyJourneyStageRewardStages[rewardIdx] = 0
    luckyJourneyDiceWeightStages[rewardIdx] = 0
    luckyJourneyTokenShopStages[rewardIdx] = 0

    let html = `
        <div class="form-group luckyJourneySpinsWrap " id="luckyJourneySpinsWrap_${rewardIdx}" data-idx="${luckyJourneySpinsCnt}" style="margin:10px;padding:10px;border:solid 1px black;">
            <table class="table table-bordered" > 
                <thead>
                     <tr>
                        <td colspan="6"> 
                            <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="luckyJourneyConfig[${lvIdx}][stages][${luckyJourneySpinsCnt}][spinsStart]" value="0">
                                    <span class="input-group-addon">-</span>
                                    <input type="text" style="text-align: center" class="form-control" name="luckyJourneyConfig[${lvIdx}][stages][${luckyJourneySpinsCnt}][spinsEnd]" value="0">
                                </div>
                            </div> 
                        </td>
                    </tr>
                    <tr>
                        <th width="120">骰子面</th> 
                        <th>骰子类型</th> 
                        <th>骰子点数(Bonus 倍数)</th> 
                        <th>权重</th> 
                        <th>最大次数</th> 
                        <th>最低消耗代币数</th> 
                    </tr>
                </thead> 
                <tbody id="luckyJourneyDiceWeightBody_${rewardIdx}"> </tbody>
                <tr>
                    <td colspan="4">
                        <div class="text-center">
                            <botton class="btn btn-primary addLuckyJourneyDiceWeightTrBtn" onclick="addLuckyJourneyDiceWeightTr(this)">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delLuckyJourneyDiceWeightTr(this)">删除</botton> 
                        </div>
                    </td>
                </tr>
            </table>
         
            <hr style="border:solid 1px goldenrod;">
         
            <table class="table table-bordered" > 
                <thead>
                    <tr>
                        <th width="120">序号</th> 
                        <th>是否为阶段奖励</th> 
                        <th>目标值</th> 
                        <th>奖品</th> 
                        <th>奖励设置</th>
                    </tr>
                </thead> 
                <tbody id="luckyJourneyStageRewardBody_${rewardIdx}"> </tbody>
                <tr>
                    <td colspan="5">
                        <div class="text-center">
                            <botton class="btn btn-primary addLuckyJourneyStageRewardTrBtn" onclick="addLuckyJourneyStageRewardTr(this)">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delLuckyJourneyStageRewardTr(this)">删除</botton> 
                        </div>
                    </td>
                </tr>
            </table> 
                   
            <hr style="border:solid 1px goldenrod;">
         
            <table class="table table-bordered" > 
                <thead>
                    <tr>
                        <th width="120">商品ID</th> 
                        <th>兑换所需代币</th> 
                        <th>可兑换次数</th> 
                        <th>奖品</th> 
                        <th>奖励设置</th>
                    </tr>
                </thead> 
                <tbody id="luckyJourneyTokenShopBody_${rewardIdx}"> </tbody>
                <tr>
                    <td colspan="5">
                        <div class="text-center">
                            <botton class="btn btn-primary addLuckyJourneyTokenShopTrBtn" onclick="addLuckyJourneyTokenShopTr(this)">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delLuckyJourneyTokenShopTr(this)">删除</botton> 
                        </div>
                    </td>
                </tr>
            </table> 
        </div>
    `

    $(obj).before(html)

    $(`#luckyJourneySpinsWrap_${rewardIdx}`).append(noTokenAlert(rewardIdx, 129))
    $('.select2').select2({})
}

function delLuckyJourneySpinsWrap(obj) {
    let lvIdx = $(obj).parents('.luckyJourneyLvWrap').data('idx')

    if (luckyJourneySpinsStages[lvIdx] < 1) return
    $(obj).parents('.luckyJourneyLvWrap').find('.luckyJourneySpinsWrap:last').remove()
    luckyJourneySpinsStages[lvIdx]--
}


function addLuckyJourneyStageRewardTr(obj) {
    let lvIdx = $(obj).parents('.luckyJourneyLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.luckyJourneySpinsWrap').data('idx')
    let lsrIdx = `${lvIdx}_${spinsIdx}`

    let rIdx = ++luckyJourneyStageRewardStages[lsrIdx]
    let rewardKey = `${lvIdx}_${spinsIdx}_${rIdx}`
    let prize = escape('[]');

    let optionsHtml = ''
    $.each({0: "否", 1: "是",}, function (i, o) {
        optionsHtml += `<option value="${i}">${o}</option>`
    });

    let html = `
        <tr id="luckyJourneyStageRewardInfo_${rewardKey}" data-idx="${rIdx}" class="commonStageRewardTr"> 
            <td>
                <input type="number" class="form-control" value="${rIdx}" readonly>
            </td> 
            <td>
                 <select class="form-control" name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][stage_reward][${rIdx - 1}][bonus]">
                    ${optionsHtml}
                </select>
            </td> 
            <td>
                <input type="number" class="form-control"  name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][stage_reward][${rIdx - 1}][target]" value="0">
            </td> 
            <td class="prizeShow"></td> 
            <td>
                <input type="hidden" class="prizeInput" name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][stage_reward][${rIdx - 1}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
            </td> 
        </tr> 
    `;

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

function delLuckyJourneyStageRewardTr(obj) {
    let lvIdx = $(obj).parents('.luckyJourneyLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.luckyJourneySpinsWrap').data('idx')

    let rewardIdx = `${lvIdx}_${spinsIdx}`
    let rewardKey = rewardIdx + '_' + luckyJourneyStageRewardStages[rewardIdx]

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


function addLuckyJourneyTokenShopTr(obj) {
    let lvIdx = $(obj).parents('.luckyJourneyLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.luckyJourneySpinsWrap').data('idx')
    let lsrIdx = `${lvIdx}_${spinsIdx}`

    let rIdx = ++luckyJourneyTokenShopStages[lsrIdx]
    let rewardKey = `${lvIdx}_${spinsIdx}_${rIdx}`
    let prize = escape('[]');

    let html = `
        <tr id="luckyJourneyTokenShopInfo_${rewardKey}" data-idx="${rIdx}" class="commonStageRewardTr"> 
            <td>
                <input type="number" class="form-control" name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][token_shop][${rIdx - 1}][id]" value="${rIdx}" readonly>
            </td> 
            <td>
                <input type="number" class="form-control"  name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][token_shop][${rIdx - 1}][price]" value="0">
            </td> 
            <td>
                <input type="number" class="form-control"  name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][token_shop][${rIdx - 1}][limit]" value="0">
            </td> 
            <td class="prizeShow"></td> 
            <td>
                <input type="hidden" class="prizeInput" name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][token_shop][${rIdx - 1}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
            </td> 
        </tr> 
    `;

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

function delLuckyJourneyTokenShopTr(obj) {
    let lvIdx = $(obj).parents('.luckyJourneyLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.luckyJourneySpinsWrap').data('idx')

    let rewardIdx = `${lvIdx}_${spinsIdx}`
    let rewardKey = rewardIdx + '_' + luckyJourneyTokenShopStages[rewardIdx]

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


function addLuckyJourneyDiceWeightTr(obj) {
    let lvIdx = $(obj).parents('.luckyJourneyLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.luckyJourneySpinsWrap').data('idx')
    let lsrIdx = `${lvIdx}_${spinsIdx}`

    let rIdx = ++luckyJourneyDiceWeightStages[lsrIdx]
    let rewardKey = `${lsrIdx}_${rIdx}`

    let html = `
        <tr id="luckyJourneyDiceWeightInfo_${rewardKey}" data-idx="${rIdx}"> 
            <td>
                <input type="number" class="form-control" value="${rIdx}" readonly>
            </td> 
            <td>
                <select class="form-control"  name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][dice_weight][${rIdx - 1}][dice_type]">
                    <option value="1">点数</option>
                    <option value="2">Bonus</option>
                </select>
            </td> 
            <td>
                <input type="number" class="form-control"  name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][dice_weight][${rIdx - 1}][dice_num]" value="0">
            </td> 
            <td>
                <input type="number" class="form-control"  name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][dice_weight][${rIdx - 1}][weight]" value="0">
            </td> 
            <td>
                <input type="number" class="form-control"  name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][dice_weight][${rIdx - 1}][limit]" value="0">
            </td> 
            <td>
                <input type="number" class="form-control"  name="luckyJourneyConfig[${lvIdx}][stages][${spinsIdx}][reward][dice_weight][${rIdx - 1}][min_cost]" value="0">
            </td> 
        </tr> 
    `;

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

function delLuckyJourneyDiceWeightTr(obj) {
    let lvIdx = $(obj).parents('.luckyJourneyLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.luckyJourneySpinsWrap').data('idx')

    let rewardIdx = `${lvIdx}_${spinsIdx}`
    let rewardKey = rewardIdx + '_' + luckyJourneyDiceWeightStages[rewardIdx]

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



