
function changeTypeTo_easterEgg(){
    initEasterWrap()
}
function initReward_easterEgg(){
    initEasterWrap()
    // 关卡修正系数
    if(reward_pro){
        $.each(reward_pro, function(i, o){
            addEasterStageCeTr()
            $('input[name="pro['+i+'][stage]"]').val(o.stage)
            $('input[name="pro['+i+'][stageCe]"]').val(o.stageCe)
        })
    }

    // 复活间隔系数
    if(reward_conf){
        $.each(reward_conf, function(i, o){
            addEasterBasicLine()
            $('input[name="common_conf['+i+'][ri]"]').val(o.ri)
            $('input[name="common_conf['+i+'][riCe]"]').val(o.riCe)
        })
    }


    // 复活礼包回写
    $.each(activity_extra_data, function(i, o){
        addEasterGift()
        $('select[name="extraData['+o.stage+'][price]"]').val(o.price)
        $('input[name="extraData['+o.stage+'][stage]"]').val(o.stage)
        $('input[name="extraData['+o.stage+'][sku]"]').val(o.sku)
        // $('input[name="extraData['+o.stage+'][bet]"]').val(o.bet)
        $('input[name="extraData['+o.stage+'][spins]"]').val(o.spins)
    })

    // 保护配置回写
    if (reward_info['protect']) {
        $.each(reward_info['protect'], function (i, o) {
            addEasterProtectTr()
            for (const oKey in o) {
                $(`input[name="easterBasicConfig[protect][${i}][${oKey}]"]`).val(o[oKey])
            }
        })
    }
    if(reward_info['randConf'] ){
        $.each(reward_info['randConf'], function(i,o){
            $(`input[name="easterBasicConfig[randConf][${i}][min]"]`).val(o['min'])
            $(`input[name="easterBasicConfig[randConf][${i}][max]"]`).val(o['max'])
        })
    }

    // 关卡回写
    if(extra_reward && extra_reward!='null'){
        $.each(extra_reward, function(i, o){
            let lvIdx = i
            addEasterLvStageWrap()
            $(`input[name="easterRewardConfig[${i}][start]"]`).val(o.start)
            $(`input[name="easterRewardConfig[${i}][end]"]`).val(o.end)
            $.each(o.shushuStages, function(i0, o0){
                let shushuIdx = i0
                addEasterShushuStageWrap(lvIdx)
                $(`select[name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][shushu][]"]`).val(o0.shushu)
                $(`input[name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][spinCe]"]`).val(o0.spinCe)
                $.each(o0.stages, function(i1, o1){
                    let rewardIdx = i1
                    addEasterStageRewardTable(lvIdx, shushuIdx)
                    $(`input[name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][stages][${rewardIdx}][star]"]`).val(o1.star)

                    $.each(o1.list, function(i2, o2){
                        addEasterRewardTr(lvIdx, shushuIdx, rewardIdx)
                        $(`input[name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][stages][${rewardIdx}][list][${i2}][stage]"]`).val(o2.stage)
                        $(`input[name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][stages][${rewardIdx}][list][${i2}][stepnum]"]`).val(o2.stepnum)
                        $(`input[name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][stages][${rewardIdx}][list][${i2}][prize]"]`).val(escape(JSON.stringify(o2.prize)))
                        var prizeShowStr = ''
                        $.each(o2.prize, function(prizdIdx, prize){
                            prizeShowStr += chestPropsHtml(prize)
                        })
                        $('.prizeShow:last').html(prizeShowStr)
                    })

                })
            })
        })
    }
}
function initEasterWrap(){
    $("#reward_info").empty()
    // 关卡修正系数
    $("#reward_info").prepend(easterStageCeConfig());
    $("#reward_info").prepend('<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">')

    // 复活间隔系数
    $("#reward_info").prepend(easterBasicConfig('复活间隔系数'));
    $("#reward_info").prepend('<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">')

    // 复活礼包
    $("#reward_info").prepend(easterGiftTable());
    $("#reward_info").prepend('<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">')

    // 保护系数配置
    $("#reward_info").prepend(easterProtectCeConfig());

    // 关卡数据
    easterMainConfig();
}

// 关卡修正系数
function easterStageCeConfig(){
    easterStageCeCnt = 0;
    var html =`
        <div class="ibox-title" style="border-top: 0;">
            <h5 style="font-weight: bold">关卡修正系数</h5>
        </div>`
    html += `
        <table class="table table-bordered scoreCGRewardTable" id="reward_table">
            <thead>
                <tr>
                    <td colspan="6">
                    </td>
                </tr>
            </thead>
            <tr>
               <th>关卡</th>
               <th>关卡修正系数</th>
           </tr>
            <tbody id="stageCeConfigTbody">
            </tbody>
            <tr>
                <td colspan="6">
                    <div class="text-center">
                        <botton class="btn btn-primary" onclick="addEasterStageCeTr()">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delEasterStageCeTr()">删除</botton>
                    </div>
                </td>
            </tr>
        </table>
`;
    return html;
}
function addEasterStageCeTr() {
    easterStageCeCnt++
    var html = '';
    html += '<tr  class="easterStageCeCnt">';
    html += '<td><input type="text" class="form-control"  name="pro[' + easterStageCeCnt + '][stage]" value=""></td>';
    html += '<td><input type="text" class="form-control"  name="pro[' + easterStageCeCnt + '][stageCe]" value=""></td>';
    html +="</tr>";
    $('#stageCeConfigTbody').append(html)
}
function delEasterStageCeTr() {
    if(easterStageCeCnt < 1)return;
    easterStageCeCnt--
    $('.easterStageCeCnt:last').remove()
}

// 复活间隔系数
function easterBasicConfig(title) {
    commonLineTitles =[{"tname":"复活次数间隔","vname":"ri"},{"tname":"复活次数间隔系数","vname":"riCe"}];
    var html=`
        <div>
            <div class="ibox-title" style="border-top: 0;">
                <h5 style="font-weight: bold">${title}</h5>
            </div>
            <div class="text-center" id="commonLine">
            </div>
            <botton class="btn btn-primary" id="addEasterBasicLine" onclick="addEasterBasicLine()">添加</botton>
            <botton class="btn btn-danger del-tr" onclick="delEasterBasicLine()">删除</botton>
        </div>`;
    return html;
}
function addEasterBasicLine() {
    var html='';
    commonLineNum++;
    var data = {"id":commonLineNum,"limit":commonLineNum,"ri":1,"riCe":1};
    html +='<div class="row" style="border: 1px solid #CCC;margin:10px 5px;padding: 5px 0;"><div class="form-group" >';
    // html += '<input type="hidden" class="form-control" readonly name="common_conf['+data["id"]+'][id]" value="'+data["id"]+'">';
    $.each(commonLineTitles,function(key,val) {
        html += '<label class="col-sm-1 control-label">'+val['tname']+'</label><div class="col-sm-1"><input type="text" class="form-control"  name="common_conf[' + data["id"] + ']['+val['vname']+']" value="' + data[val['vname']] + '"></div>';
    });
    html += '</div></div>';
    $('#commonLine').append(html);
}
function delEasterBasicLine() {
    if(commonLineNum>1){
        commonLineNum--;
        $("#commonLine").children(".row")[commonLineNum].remove();
    }
}

// 复活礼包
function easterGiftTable(){
    easterGiftStage=0;
    var html ='';
    html+='<div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">复活礼包配置</h5></div>';
    html +='<table class="table table-bordered scoreCGRewardTable" id="reward_table">';
    html +='<thead><tr><td colspan="4"><div class="form-group"></div></thead>';
    html +='<tr>' +
        '<th>阶段</th>' +
        '<th>体力值</th>' +
        '<th>价格</th>' +
        '<th>sku</th>' +
        '</tr>';
    html +='<tbody id="table_body">';
    html +='</tbody><tr><td colspan="4"><div class="text-center"><botton class="btn btn-primary" onclick="addEasterGift()">添加</botton><botton class="btn btn-danger del-tr" onclick="delEasterGift()">删除</botton>';
    html +='</div></td></tr></table>';
    return html;
}
function addEasterGift(){
    easterGiftStage++;
    var d = {"stage":easterGiftStage,"bet":0,"spins":0,"prize":0,"sku":""};
    $("#table_body").append(easterGiftLine(d));
}
function delEasterGift() {
    if(easterGiftStage>1){
        $("#table_body").children("tr:last-child").remove();
        easterGiftStage--;
    }
}
function easterGiftLine(data){
    var html = '';
    html += '<tr id="rewardInfo_' + data['stage'] + '" class="priceSkuGroup">';
    html += '<td><input type="text" class="form-control" readonly name="extraData[' + data['stage'] + '][stage]" value="' + data['stage'] + '"></td>';
    html += '<td>' +
        '<input type="hidden" class="form-control" name="extraData[' + data['stage'] + '][bet]" value="1">' +
        '<input type="text" class="form-control" name="extraData[' + data['stage'] + '][spins]" value="' + data['spins'] + '">' +
        '</td>';
    html += '<td><select class="form-control m-b " onchange="priceChanged(this)" name="extraData[' + data['stage'] + '][price]">';
    $.each(goodSku, function (k, v) {
        if (data["price"] == v["price"]) {
            sel = "selected";
        } else {
            sel = "";
        }
        html += '<option value="' + v["price"] + '" ' + sel + '>' + v["price"] + '</option>';
    });
    html +='</select></td>' +
        '<td><input class="form-control t_sku skuInput" readonly type="text" name="extraData['+data['stage']+'][sku]" value="'+data['sku']+'"></td>';
    html +="</tr>";
    return html;
}

// 保护系数配置
function easterProtectCeConfig(){
    easterProtectCnt = 0;
    let randLimit = [1,2]
    var html =`
        <div class="ibox-title" style="border-top: 0;">
            <h5 style="font-weight: bold">保护系数配置</h5>
        </div>`
        for(let i of randLimit){
            html += `
        <div class="form-group">
            <label style="text-align: right" class="col-sm-2" >随机下限-${i}：</label>
            <input class="col-sm-2" name="easterBasicConfig[randConf][${i}][min]">
            <label style="text-align: right" class="col-sm-2" >随机上限-${i}：</label>
            <input class="col-sm-2" name="easterBasicConfig[randConf][${i}][max]">
        </div>`
        }
    html += `
        <table class="table table-bordered scoreCGRewardTable" id="reward_table">
            <thead>
                <tr>
                    <td colspan="7">
                    </td>
                </tr>
            </thead>
            <tr>
               <th>等级下限</th>
               <th>等级上限</th>
               <th>产出系数(M)</th>
               <th>上限倍率</th>
               <th>下限倍率</th>
               <th>保护层数</th>
               <th>保护基数</th>
           </tr>
            <tbody id="protectConfigTbody">
            </tbody>
            <tr>
                <td colspan="7">
                    <div class="text-center">
                        <botton class="btn btn-primary" onclick="addEasterProtectTr()">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delEasterProtectTr()">删除</botton>
                    </div>
                </td>
            </tr>
        </table>
`;
    return html;
}

function addEasterProtectTr() {
    easterProtectCnt++
    var html = '';
    html += '<tr  class="easterProtectTr">';
    html += '<td><input type="number" class="form-control"  name="easterBasicConfig[protect][' + easterProtectCnt + '][lvMin]" value=""></td>';
    html += '<td><input type="number" class="form-control"  name="easterBasicConfig[protect][' + easterProtectCnt + '][lvMax]" value=""></td>';
    html += '<td><input type="number" class="form-control"  name="easterBasicConfig[protect][' + easterProtectCnt + '][m]" value=""></td>';
    html += '<td><input type="number" class="form-control"  name="easterBasicConfig[protect][' + easterProtectCnt + '][maxBet]" value=""></td>';
    html += '<td><input type="number" class="form-control"  name="easterBasicConfig[protect][' + easterProtectCnt + '][minBet]" value=""></td>';
    html += '<td><input type="number" class="form-control"  name="easterBasicConfig[protect][' + easterProtectCnt + '][stage]" value=""></td>';
    html += '<td><input type="number" class="form-control"  name="easterBasicConfig[protect][' + easterProtectCnt + '][ce]" value=""></td>';
    html += "</tr>";
    $('#protectConfigTbody').append(html)
}
function delEasterProtectTr() {
    if(easterProtectCnt < 1)return;
    easterProtectCnt--
    $('.easterProtectTr:last').remove()
}

// 主配置
function easterMainConfig(){
    var html ='';
    var str='关卡奖励'
    easterLvCnt = 0;
    easterShushuCnt = {};
    easterStageCnt = {}
    easterStageRewardCnt = {}
    html+='<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    html +='<div class="" id="spinStageTable" style="top:10px">';
    html +='<div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">'+str+'</h5></div>';

    html+='</div>';
    html+='<div class="form-group"><div class="col-sm-12">';
    html+='<button class="btn btn-primary" id="addEasterLvWrapBtn" onclick="addEasterLvStageWrap()" type="button">添加等级区间</button>';
    html+='<button class="btn btn-white"   id="delEasterLvWrapBtn" onclick="delEasterLvStageWrap()" type="button">删除等级区间</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    $("#reward_info").append(html);
}
function addEasterLvStageWrap(){
    easterLvCnt++
    easterShushuCnt[easterLvCnt] = 0;
    var html = `
         <div class="form-group easterLvWrap " id="easterLvWrap_${easterLvCnt}" data-idx="${easterLvCnt}" 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="easterRewardConfig[${easterLvCnt}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="easterRewardConfig[${easterLvCnt}][end]" value="0">
                </div>
            </div>
            <div class="btnGroups-shushu">
                <botton class="btn btn-primary  addEasterStageWrapBtn" onclick="addEasterShushuStageWrap(${easterLvCnt})">添加数数分组层级</botton>
                <botton class="btn btn-danger del-tr delEasterStageWrapBtn" onclick="delEasterShushuStageWrap(${easterLvCnt})">删除数数分组层级</botton>
            </div>
         </div>`

    $('#addEasterLvWrapBtn').before(html)
    $('.select2').select2({})
}
function delEasterLvStageWrap(){
    if(easterLvCnt < 1) return
    $(`#easterLvWrap_${easterLvCnt}`).remove()
    easterLvCnt--
}

// 数数分组
function addEasterShushuStageWrap(lvIdx){
    let shushuIdx = ++easterShushuCnt[lvIdx]
    let lvShushuIdx = `${lvIdx}_${shushuIdx}`
    easterStageCnt[lvShushuIdx] = 0;
    var html = `
         <div class="form-group easterShushuWrap easterShushuWrap_${shushuIdx}" id="easterShushuWrap_${lvShushuIdx}" data-idx="${lvShushuIdx}" style="margin:10px;padding:10px;border:solid 1px black;">
             <div class="row">
                 <div class="col-sm-1"><lable class="form-control">数数分组：</lable></div>
                 <div class="col-sm-5">
                    <select class=" select2" name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][shushu][]"  multiple  style="min-width: 100%">`
                        $.each(shushuGroups, function(i,o){
                            html += '<option value="'+i+'">'+o+'</option>'
                        });
        html+=`
                    </select>
                 </div>
                 <div class="col-sm-1"><lable class="form-control">体力参数：</lable></div>
                 <div class="col-sm-1">
                     <input class="form-control" name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][spinCe]">
                 </div>
             </div>
             <div class="btnGroups-rewards">
                 <botton class="btn btn-primary  addEasterStageWrapBtn" onclick="addEasterStageRewardTable(${lvIdx}, ${shushuIdx})">添加关卡</botton>
                 <botton class="btn btn-danger del-tr delEasterStageWrapBtn" onclick="delEasterStageRewardTable(${lvIdx}, ${shushuIdx})">删除关卡</botton>
             </div>
          </div>`

    $(`#easterLvWrap_${lvIdx}`).find('.btnGroups-shushu').before(html)
    $('.select2').select2({})
}
function delEasterShushuStageWrap(lvIdx){
    if(easterShushuCnt[lvIdx] < 1)return;
    $(`#easterLvWrap_${lvIdx}`).find('.easterShushuWrap:last').remove()
    easterShushuCnt[lvIdx]--
}

//阶段奖励配置表html
function addEasterStageRewardTable(lvIdx, shushuIdx) {
    let lvShushuIdx = `${lvIdx}_${shushuIdx}`
    let rewardIdx = ++easterStageCnt[lvShushuIdx]
    var index = rewardIdx
    var data ={"star":rewardIdx,"end":0};
    var stageIdx = lvShushuIdx + '_' + index
    easterStageRewardCnt[stageIdx] = 0;
    var cols = 4;
    var html = `
    <table data-idx="${index}" class="table table-bordered easterStageTable" id="reward_table_${stageIdx}">
        <thead>
            <tr>
                <td colspan="${cols}">
                    <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="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][stages][${rewardIdx}][star]" value="${data['star']}">
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <th>阶段</th>
                <th>权重值</th>
                <th>奖品</th>
                <th>奖励设置</th>
            </tr>
        </thead>
        <tbody id="table_body_${stageIdx}">
    
        </tbody>
        <tr>
            <td colspan="${cols}">
                <div class="text-center">
                    <botton class="btn btn-primary addEasterRewardTrBtn" onclick="addEasterRewardTr(${lvIdx}, ${shushuIdx}, ${rewardIdx})">添加</botton>
                    <botton class="btn btn-danger  delEasterRewardTrBtn" onclick="delEasterRewardTr(${lvIdx}, ${shushuIdx}, ${rewardIdx})">删除</botton>
                </div>
            </td>
        </tr>
    </table>`
    $(`#easterShushuWrap_${lvShushuIdx}`).find('.btnGroups-rewards').before(html);
}
function delEasterStageRewardTable(lvIdx, shushuIdx){
    let lvShushuIdx = `${lvIdx}_${shushuIdx}`
    if(easterStageCnt[lvShushuIdx]<1)return
    $(`#easterShushuWrap_${lvShushuIdx}`).find('.easterStageTable:last').remove();
    easterStageCnt[lvShushuIdx]--
}


// 奖励行
function addEasterRewardTr(lvIdx, shushuIdx, rewardIdx) {
    var complexIdx = `${lvIdx}_${shushuIdx}_${rewardIdx}`
    easterStageRewardCnt[complexIdx]++
    var data ={"stage":easterStageRewardCnt[complexIdx],"stepnum":0,"progressBarId":0,"lv":1};

    var prize = '[]';
    prize = escape(prize);
    var html =`
            <tr class="easterRewardTr commonStageRewardTr" data-idx="${data.stage}">
                <td>
                    <input type="text" class="form-control" name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][stages][${rewardIdx}][list][${data.stage}][stage]" value="${data.stage}">
                </td>
                <td>
                    <input type="text" class="form-control" name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][stages][${rewardIdx}][list][${data.stage}][stepnum]" value="0">
                </td>
                <td class="prizeShow "></td>
                <td>
                    <input type="hidden" class="prizeInput" name="easterRewardConfig[${lvIdx}][shushuStages][${shushuIdx}][stages][${rewardIdx}][list][${data.stage}][prize]" value="${prize}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td>
            </tr>`

    // console.log($('#table_body_' + complexIdx));
    $('#table_body_'+complexIdx).append(html);
}
function delEasterRewardTr(lvIdx, shushuIdx, rewardIdx) {
    var complexIdx = `${lvIdx}_${shushuIdx}_${rewardIdx}`
    if(easterStageRewardCnt[complexIdx]<1)return
    $('#table_body_'+complexIdx).find('.easterRewardTr:last').remove();
    easterStageRewardCnt[complexIdx]--
}


