let KingOfCardsLvCnt = 0;
let KingOfCardsSpinsStages = {}
let KingOfCardsSsStages = {}
let KingOfCardsProgressStages = {}

function initRewardKingOfCards(type) {
    console.log('init ' + type)
    changeTypeToKingOfCards()

    // 奖励回写
    console.log('reward_conf ', reward_conf)
    reward_conf && $.each(reward_conf, function (lvStage, lvItem) {
        $('#addKingOfCardsLvWrapBtn').click()
        $(`input[name="kingOfCardsConfig[${lvStage}][lvStart]"]`).val(lvItem.lvStart)
        $(`input[name="kingOfCardsConfig[${lvStage}][lvEnd]"]`).val(lvItem.lvEnd)

        $.each(lvItem.stages, function (spinsStage, spinsItem) {
            $('.addKingOfCardsSpinsWrapBtn:last').click()
            $(`input[name="kingOfCardsConfig[${lvStage}][stages][${spinsStage}][spinsStart]"]`).val(spinsItem.spinsStart)
            $(`input[name="kingOfCardsConfig[${lvStage}][stages][${spinsStage}][spinsEnd]"]`).val(spinsItem.spinsEnd)

            // 进度奖励
            spinsItem.stages && $.each(spinsItem.stages, function (ssIdx, ssItem) {
                $('.addKingOfCardsSsWrapBtn:last').click()
                $(`select[name="kingOfCardsConfig[${lvStage}][stages][${spinsStage}][stages][${ssIdx}][shushu][]"]`).val(ssItem.shushu)

                ssItem.progress && $.each(ssItem.progress, (rewardIdx, rewardItem)=>{

                    $('.addKingOfCardsProgressTrBtn:last').click()
                    $(`input[name="kingOfCardsConfig[${lvStage}][stages][${spinsStage}][stages][${ssIdx}][progress][${rewardIdx}][target]"]`).val(rewardItem.target)
                    $(`input[name="kingOfCardsConfig[${lvStage}][stages][${spinsStage}][stages][${ssIdx}][progress][${rewardIdx}][prize]"]`).val(escape(JSON.stringify(rewardItem.prize)))

                    $('.prizeShow:last').html(prizeListHtml(rewardItem.prize))
                })

            })
        })
    })
    $('.select2').select2({})
}

function changeTypeToKingOfCards() {
    $("#reward_info").empty();
    initKingOfCardsWrap();
}

function initKingOfCardsWrap() {
    KingOfCardsLvCnt = 0;
    KingOfCardsSpinsStages = {}
    KingOfCardsSsStages = {}
    KingOfCardsProgressStages = {}

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

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

function addKingOfCardsLvWrap() {
    KingOfCardsLvCnt++
    KingOfCardsSpinsStages[KingOfCardsLvCnt] = 0
    let html = `
        <div class="form-group text-center KingOfCardsLvWrap" id="KingOfCardsLvWrap_${KingOfCardsLvCnt}" data-idx="${KingOfCardsLvCnt}" 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="kingOfCardsConfig[${KingOfCardsLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="kingOfCardsConfig[${KingOfCardsLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton style="margin-top: 10px" class="btn btn-primary  addKingOfCardsSpinsWrapBtn" onclick="addKingOfCardsSpinsWrap( this)">添加体力分组层级</botton>
            <botton style="margin-top: 10px" class="btn btn-danger del-tr delKingOfCardsSpinsWrapBtn" onclick="delKingOfCardsSpinsWrap( this)">删除体力分组层级</botton>
        </div>
        `
    $("#addKingOfCardsLvWrapBtn").before(html)
}
function delKingOfCardsLvWrap() {
    if (KingOfCardsLvCnt < 1) {
        return;
    }
    $('#KingOfCardsLvWrap_' + KingOfCardsLvCnt).remove();
    KingOfCardsLvCnt--;
}

function addKingOfCardsSpinsWrap(obj) {
    let lvIdx = $(obj).parents('.KingOfCardsLvWrap').data('idx')
    let spinsCnt = ++KingOfCardsSpinsStages[lvIdx]
    let ssKey = `${lvIdx}_${spinsCnt}`

    KingOfCardsSsStages[ssKey] = 0

    let html = `
        <div class="KingOfCardsSpinsWrap" id="KingOfCardsSpinsWrap_${ssKey}" data-idx="${spinsCnt}" style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            <table class="table table-bordered">
                <thead>
                    <tr><td colspan="4">
                         <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="kingOfCardsConfig[${lvIdx}][stages][${spinsCnt}][spinsStart]" value="0">
                                <span class="input-group-addon">-</span>
                                <input type="text" style="text-align: center" class="form-control" name="kingOfCardsConfig[${lvIdx}][stages][${spinsCnt}][spinsEnd]" value="0">
                            </div>
                        </div>
                    </td></tr>
                </thead>
                <tr>
                    <td colspan="7">
                        <div class="text-center" style="margin-top: 20px">
                            <botton class="btn btn-primary addKingOfCardsSsWrapBtn" onclick="addKingOfCardsSsWrap(this)">添加数数分组分层</botton>
                            <botton class="btn btn-danger del-tr" onclick="delKingOfCardsSsWrap(this)">删除数数分组分层</botton>
                        </div>
                    </td>
                </tr>
            </table>
        </div>`
    $(obj).before(html)
}
function delKingOfCardsSpinsWrap(obj) {
    let lvIdx = $(obj).parents('.KingOfCardsLvWrap').data('idx')
    if (KingOfCardsSpinsStages[lvIdx] < 1) return
    $(obj).parents('.KingOfCardsLvWrap').find('.KingOfCardsSpinsWrap:last').remove()
    KingOfCardsSpinsStages[lvIdx]--
}

function addKingOfCardsSsWrap(obj) {
    let lvIdx = $(obj).parents('.KingOfCardsLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.KingOfCardsSpinsWrap').data('idx')
    let ssIdx = ++KingOfCardsSsStages[`${lvIdx}_${spinsIdx}`]
    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}`
    KingOfCardsProgressStages[rewardKey] = 0

    let ssOptionsHtml = getShushuSelectorOptions()
    let html = `
        <div class="KingOfCardsSsWrap" id="KingOfCardsSsWrap_${rewardKey}" data-idx="${ssIdx}" style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            <table class="table table-bordered">
                <thead>
                    <tr><td colspan="4">
                         <div class="form-group">
                            <label class="col-sm-1 control-label m-b">数数分组: </label>
                            <div class="col-sm-3 input-group">
                                <select class=" select2 " multiple name="kingOfCardsConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][shushu][]"  style="width: 100%">
                                    ${ssOptionsHtml}
                                </select>
                            </div>
                        </div>
                    </td></tr>
                    <tr>
                        <th width="120">阶段</th>
                        <th width="140">阶段值</th>
                        <th>奖励</th>
                        <th width="140">奖励设置</th>
                    </tr>
                </thead>
                <tbody id="KingOfCardsProgressBody_${rewardKey}"></tbody>
                <tr>
                    <td colspan="7">
                        <div class="text-center" style="margin-top: 20px">
                            <botton class="btn btn-primary addKingOfCardsProgressTrBtn" onclick="addKingOfCardsProgressTr(this)">添加阶段奖励</botton>
                            <botton class="btn btn-danger del-tr" onclick="delKingOfCardsProgressTr(this)">删除</botton>
                        </div>
                    </td>
                </tr>
            </table>
        </div>`
    $(obj).before(html)
    $('.select2').select2({})
}
function delKingOfCardsSsWrap(obj) {
    let lvIdx = $(obj).parents('.KingOfCardsLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.KingOfCardsSpinsWrap').data('idx')
    if (KingOfCardsSsStages[`${lvIdx}_${spinsIdx}`] < 1) return
    $(obj).parents('.KingOfCardsSpinsWrap').find('.KingOfCardsSsWrap:last').remove()
    KingOfCardsSsStages[`${lvIdx}_${spinsIdx}`]--
}

function addKingOfCardsProgressTr(obj) {
    let lvIdx = $(obj).parents('.KingOfCardsLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.KingOfCardsSpinsWrap').data('idx')
    let ssIdx = $(obj).parents('.KingOfCardsSsWrap').data('idx')
    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}`

    let rewardStage = ++KingOfCardsProgressStages[rewardKey]
    let rewardIdx = `${rewardKey}_${rewardStage}`
    let prize = escape('[]');

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

    $('#KingOfCardsProgressBody_' + rewardKey).append(html)
}
function delKingOfCardsProgressTr(obj) {
    let lvIdx = $(obj).parents('.KingOfCardsLvWrap').data('idx')
    let spinsIdx = $(obj).parents('.KingOfCardsSpinsWrap').data('idx')
    let ssIdx = $(obj).parents('.KingOfCardsSsWrap').data('idx')
    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}`

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