let mayaLvStagesCnt = 0;
let mayaSpinStagesCnt = {};
let mayaSsStagesCnt = {};
let mayaStagesCnt = {};
let mayaProgressCnt = {};

function initReward_mayaWheel() {
    initMayaWheelWrap()

    activity_extra_data.mayaWheelSpend && $.each(activity_extra_data.mayaWheelSpend, function (i, o) {
        $(`input[name="mayaWheelSpend[${i}]"]`).val(o)
    })

    $.each(reward_conf, function (i, o) {
        $('#addMayaLvStageWrapBtn').click()

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

            $.each(o1.stages, function (i2, o2) {
                $('.addMayaSsStageWrapBtn:last').click()
                $(`select[name="mayaWheelReward[${i}][stages][${i1}][stages][${i2}][shushu][]"]`).val(o2.shushu)

                $.each(o2.rewards.diceConf, function (i3, o3) {
                    $('.addMayaStageWrapBtn:last').click()
                    let prizeSpanArr = $('.mayaWheelRewardConfigTable:last').find('.prizeShow')

                    $.each(o3.list, function (i4, o4) {
                        let bonus = o4.bonus ? 1 : 0
                        $(`input[name="mayaWheelReward[${i}][stages][${i1}][stages][${i2}][rewards][diceConf][${i3}][list][${i4}][weight]"]`).val(o4.weight)
                        $(`input[name="mayaWheelReward[${i}][stages][${i1}][stages][${i2}][rewards][diceConf][${i3}][list][${i4}][lastCnt]"]`).val(o4.lastCnt)
                        $(`select[name="mayaWheelReward[${i}][stages][${i1}][stages][${i2}][rewards][diceConf][${i3}][list][${i4}][bonus]"]`).val(bonus)
                        $(`input[name="mayaWheelReward[${i}][stages][${i1}][stages][${i2}][rewards][diceConf][${i3}][list][${i4}][prize]"]`).val(escape(JSON.stringify(o4.prize)))

                        var prizeHtml = ''
                        $.each(o4.prize, function (i, prize) {
                            prizeHtml += chestPropsHtml(prize)
                        })

                        $(prizeSpanArr[parseInt(i4) - 1]).html(prizeHtml)
                    })
                })

                // 进度奖励
                o2.rewards.progressConf && $.each(o2.rewards.progressConf, function (i3, o3) {
                    $('.addMayaWheelProgressTrBtn:last').click()

                    $(`input[name="mayaWheelReward[${i}][stages][${i1}][stages][${i2}][rewards][progressConf][${i3}][target]"]`).val(o3.target)
                    $(`input[name="mayaWheelReward[${i}][stages][${i1}][stages][${i2}][rewards][progressConf][${i3}][prize]"]`).val(escape(JSON.stringify(o3.prize)))

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

                renderNotokenAlert(`${i}_${i1}_${i2}`, extra_reward)
            })
        })
    })
}


function initMayaWheelWrap() {
    $('#reward_info').empty();
    mayaWheelBasicConfig();
}

function generateMayaWheelConfig(totalCnt, stageIdx) {
    totalCnt = parseInt(totalCnt)
    let html = '';
    let curCnt = $('.mayaWheelSpendConfigTable').find('tr').length;
    if (curCnt < totalCnt) {
        for (let i = (curCnt); i <= totalCnt; i++) {
            html += `<tr><td >第${i}次转</td><td ><input name="mayaWheelSpend[${parseInt(i) - 1}]" value="1"></td></tr>`;
        }
        $(".mayaWheelSpendConfigTable").append(html)
    } else {
        for (let i = curCnt - 1; i > totalCnt; i--) {
            $(".mayaWheelSpendConfigTable").find('tr:last').remove()
        }
    }
}

// 基础配置
function mayaWheelBasicConfig() {
    mayaLvStagesCnt = 0;
    mayaSpinStagesCnt = {};
    mayaSsStagesCnt = {};
    mayaStagesCnt = {};
    mayaProgressCnt = {};

    let totalCntArr = [12]
    let totalCnt = 12;
    if (activity_extra_data.totalCnt) totalCnt = activity_extra_data.totalCnt;

    let optionsHtml = ''
    $.each(totalCntArr, function (i, o) {
        optionsHtml += '<option>' + o + '</option>'
    })

    let html = `
            <div aria-hidden="true" id="mayaWheelBasicConfig">
                <div class="form-group">
                    <label class="col-sm-1 control-label">转盘总数:</label>
                    <div class="col-sm-2">
                        <select  name="mayaWheelTotalCnt" onchange="generateMayaWheelConfig($(this).val(), -1)"   class="form-control">
                        ${optionsHtml}
                        </select>
                    </div>
                </div>
            </div>
            <div class="mayaWheelSpendConfigTableWrap_' + mayaStagesCnt + '" data-idx="' + mayaStagesCnt + '" style="top:10px">
                <table class="mayaWheelSpendConfigTable table table-bordered">
                    <tr>
                        <td >第N次转</td>
                        <td >消耗代币</td>
                    </tr>
                </table>
                
                <hr style="border:solid 1px goldenrod;">
                
                <div style="top:10px;margin: 10px;">
                    <div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">层级配置</h5></div>
                </div>
                <div class="form-group text-center">
                    <div class=" ">
                        <button class="btn btn-primary" id="addMayaLvStageWrapBtn" onclick="addMayaLvStageWrap(this)" type="button">添加等级层级</button>
                        <button class="btn btn-white"   id="delMayaLvStageWrapBtn" onclick="delMayaLvStageWrap()" type="button">删除等级层级</button>
                    </div>
                </div>
                <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
            </div>
    `;

    $("#reward_info").append(html)
    $('select[name=mayaWheelTotalCnt]').val(totalCnt)
    generateMayaWheelConfig(totalCnt)
}


function changeTypeTo_mayaWheel() {
    initMayaWheelWrap()
}

function addMayaLvStageWrap(obj) {
    mayaLvStagesCnt++;
    mayaSpinStagesCnt[mayaLvStagesCnt] = 0

    let html = `
        <div class="form-group mayaWheelLvStageWrap " id="mayaWheelLvStageWrap_${mayaLvStagesCnt}" data-idx="${mayaLvStagesCnt}" 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="mayaWheelReward[${mayaLvStagesCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="mayaWheelReward[${mayaLvStagesCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addMayaSpinStageWrapBtn" onclick="addMayaSpinStageWrap(this)">添加体力层级</botton>
            <botton class="btn btn-danger del-tr delMayaSpinStageWrapBtn" onclick="delMayaSpinStageWrap(this)">删除体力层级</botton>
        </div>
`
    $(obj).before(html)
}

function delMayaLvStageWrap() {
    if (mayaLvStagesCnt > 0) {
        $('#mayaWheelLvStageWrap_' + mayaLvStagesCnt).remove()
        mayaLvStagesCnt--
    }
}


function addMayaSpinStageWrap(obj) {

    let lvIdx = $(obj).parents('.mayaWheelLvStageWrap').data('idx')
    let spinsIdx = ++mayaSpinStagesCnt[lvIdx]
    let ssIdx = `${lvIdx}_${spinsIdx}`
    mayaSsStagesCnt[ssIdx] = 0

    let html = `
        <div class="form-group mayaWheelSpinStageWrap " id="mayaWheelSpinStageWrap_${ssIdx}" data-idx="${spinsIdx}" 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="mayaWheelReward[${lvIdx}][stages][${spinsIdx}][spinsStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="mayaWheelReward[${lvIdx}][stages][${spinsIdx}][spinsEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addMayaSsStageWrapBtn" onclick="addMayaSsStageWrap(this)">添加数数分组层级</botton>
            <botton class="btn btn-danger del-tr delMayaSsStageWrapBtn" onclick="delMayaSsStageWrap(this)">删除数数分组层级</botton>
        </div>
    `;

    $(obj).before(html)
}

function delMayaSpinStageWrap(obj) {
    let lvStage = $(obj).parents('.mayaWheelLvStageWrap').data('idx');
    console.log(lvStage)
    console.log(mayaSpinStagesCnt[lvStage])
    if (mayaSpinStagesCnt[lvStage] > 0) {
        console.log($('#mayaWheelSpinStageWrap_' + mayaSpinStagesCnt[lvStage]))
        $('#mayaWheelSpinStageWrap_' + lvStage + '_' + mayaSpinStagesCnt[lvStage]).remove()
        mayaSpinStagesCnt[lvStage]--
    }
}

function addMayaSsStageWrap(obj) {

    let lvIdx = $(obj).parents('.mayaWheelLvStageWrap').data('idx')
    let spinsIdx = $(obj).parents('.mayaWheelSpinStageWrap').data('idx')
    let ssKey = `${lvIdx}_${spinsIdx}`
    let ssIdx = ++mayaSsStagesCnt[ssKey]
    let rewardIdx = `${lvIdx}_${spinsIdx}_${ssIdx}`
    mayaStagesCnt[rewardIdx] = 0
    mayaProgressCnt[rewardIdx] = 0
    let shushuOptions = getShushuSelectorOptions()
    let html = `
        <div class="form-group mayaWheelSsStageWrap " id="mayaWheelSsStageWrap_${rewardIdx}" data-idx="${ssIdx}" style="margin:10px;padding:10px;border:solid 1px black;">
            <table class="table table-bordered" > 
                <thead>
                     <tr>
                        <td colspan="3"> 
                            <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="mayaWheelReward[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][shushu][]" multiple>
                                        ${shushuOptions}
                                    </select>
                                </div>
                            </div> 
                        </td>
                    </tr>
                </thead> 
                <tbody id="mayaWheelRewardBody_${rewardIdx}"> </tbody>
                <tr>
                    <td colspan="3">
                        <div class="text-center">
                            <botton class="btn btn-primary addMayaStageWrapBtn" onclick="addMayaStageWrap(this)" type="button">添加奖励层级</botton>
                            <botton class="btn btn-danger delMayaStageWrapBtn" onclick="delMayaStageWrap(this)">删除奖励层级</botton> 
                        </div>
                    </td>
                </tr>
            </table>
             
            <hr style="border:solid 1px goldenrod;">
             
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th width="120">阶段</th>
                        <th width="140">阶段值</th>
                        <th>奖励</th>
                        <th width="140">奖励设置</th>
                    </tr>
                </thead>
                <tbody id="MayaWheelProgressBody_${rewardIdx}"></tbody>
                <tr>
                    <td colspan="7">
                        <div class="text-center" style="margin-top: 20px">
                            <botton class="btn btn-primary addMayaWheelProgressTrBtn" onclick="addMayaWheelProgressTr(this)">添加阶段奖励</botton>
                            <botton class="btn btn-danger del-tr" onclick="delMayaWheelProgressTr(this)">删除</botton>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    `;
    $(obj).before(html)
    // $(`#mayaWheelSsStageWrap_${rewardIdx}`).append(noTokenAlert(rewardIdx, 128))
    $('.select2').select2({})
}

function delMayaSsStageWrap(obj) {
    let lvStage = $(obj).parents('.mayaWheelLvStageWrap').data('idx');
    let spinsStage = $(obj).parents('.mayaWheelSpinStageWrap').data('idx');
    let ssKey = `${lvStage}_${spinsStage}`
    console.log(mayaSsStagesCnt[ssKey])
    if (mayaSsStagesCnt[ssKey] > 0) {
        console.log($('#mayaWheelSpinStageWrap_' + mayaSpinStagesCnt[lvStage]))
        $(`#mayaWheelSsStageWrap_${ssKey}_${mayaSsStagesCnt[ssKey]}`).remove()
        mayaSsStagesCnt[ssKey]--
    }
}


function addMayaStageWrap(obj) {
    let lvStage = $(obj).parents('.mayaWheelLvStageWrap').data('idx');
    let spinStageIdx = $(obj).parents('.mayaWheelSpinStageWrap').data('idx')
    let ssStageIdx = $(obj).parents('.mayaWheelSsStageWrap').data('idx')
    let lsrIdx = `${lvStage}_${spinStageIdx}_${ssStageIdx}`

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

    let trHtml = ''
    let totalCnt = 12
    for (let i = 1; i <= totalCnt; i++) {
        trHtml += `
            <tr class="commonStageRewardTr">
                <td >${i}</td>
                <td ><input name="mayaWheelReward[${lvStage}][stages][${spinStageIdx}][stages][${ssStageIdx}][rewards][diceConf][${rIdx}][list][${i}][weight]" value="1"></td>
                <td ><input name="mayaWheelReward[${lvStage}][stages][${spinStageIdx}][stages][${ssStageIdx}][rewards][diceConf][${rIdx}][list][${i}][lastCnt]" value="0"></td>
                <td >
                    <select name="mayaWheelReward[${lvStage}][stages][${spinStageIdx}][stages][${ssStageIdx}][rewards][diceConf][${rIdx}][list][${i}][bonus]" >
                        <option value="0">否</option>
                        <option value="1">是</option>
                    </select>
                </td>
                <td class="prizeShow"></td> 
                <td>
                    <input type="hidden" class="prizeInput" name="mayaWheelReward[${lvStage}][stages][${spinStageIdx}][stages][${ssStageIdx}][rewards][diceConf][${rIdx}][list][${i}][prize]" value="${prize}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td> 
            </tr>`
    }

    let html = `
            <table class="mayaWheelRewardConfigTable table table-bordered"  id="mayaWheelRewardConfigTable_${rewardKey}"  style="margin:10px;padding:5px;border:solid 1px goldenrod;">
                <thead>
                    <tr><td colspan="6">第${rIdx}层奖励</td></tr>
                    <tr>
                        <td >序号</td>
                        <td >权重</td>
                        <td >转N次后才出奖</td>
                        <td>是否大奖</td> 
                        <td>奖品</td> 
                        <td>奖励设置</td>
                    </tr>
                </thead>
                <tbody>
                    ${trHtml}
                </tbody>
            </table>
    `;

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

function delMayaStageWrap(obj) {
    let lvStage = $(obj).parents('.mayaWheelLvStageWrap').data('idx');
    let spinStageIdx = $(obj).parents('.mayaWheelSpinStageWrap').data('idx')
    let ssStageIdx = $(obj).parents('.mayaWheelSsStageWrap').data('idx')

    let rewardIdx = `${lvStage}_${spinStageIdx}_${ssStageIdx}`
    let rewardKey = rewardIdx + '_' + mayaStagesCnt[rewardIdx]
    console.log(rewardKey)
    $(`#mayaWheelRewardConfigTable_${rewardKey}`).remove()
    mayaStagesCnt[rewardIdx]--;
}

function addMayaWheelProgressTr(obj) {
    let lvIdx = $(obj).parents('.mayaWheelLvStageWrap').data('idx')
    let spinsIdx = $(obj).parents('.mayaWheelSpinStageWrap').data('idx')
    let ssIdx = $(obj).parents('.mayaWheelSsStageWrap').data('idx')
    let rewardIdx = `${lvIdx}_${spinsIdx}_${ssIdx}`

    let rewardStage = ++mayaProgressCnt[rewardIdx]
    let rewardKey = `${rewardIdx}_${rewardStage}`
    let prize = escape('[]');
    let html = `
        <tr class="MayaWheelProgressInfoTr commonStageRewardTr" id="MayaWheelProgressInfo_${rewardKey}" data-idx=" ${rewardStage} ">
            <td width="120"><input type="text" name="mayaWheelReward[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][rewards][progressConf][${rewardStage}][stage]" class="form-control" value="${rewardStage}" readonly ></td>
            <td><input name="mayaWheelReward[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][rewards][progressConf][${rewardStage}][target]" type="text"  class="form-control" value="0"></td>
            <td class="prizeShow"></td>
            <td>
                <input type="hidden" class="prizeInput" name="mayaWheelReward[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][rewards][progressConf][${rewardStage}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
            </td>
        </tr>`;

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

function delMayaWheelProgressTr(obj) {
    let lvIdx = $(obj).parents('.mayaWheelLvStageWrap').data('idx')
    let SpinsIdx = $(obj).parents('.mayaWheelSpinStageWrap').data('idx')
    let SsIdx = $(obj).parents('.mayaWheelSsStageWrap').data('idx')
    let rewardIdx = `${lvIdx}_${SpinsIdx}_${SsIdx}`

    let rewardKey = `${rewardIdx}_${mayaProgressCnt[rewardIdx]}`
    if (mayaProgressCnt[rewardIdx] < 1) return;
    $('#MayaWheelProgressInfo_' + rewardKey).remove()
    mayaProgressCnt[rewardIdx]--
}