
function changeTypeToLuckyScratch(){
    initLuckyScratchWrap()
}
function initRewardLuckyScratch(){
    initLuckyScratchWrap()
    // 关卡回写
    if(activity_extra_data && activity_extra_data!='null'){
        $.each(activity_extra_data, function(i, o){
            let lvIdx = i
            addLuckyScratchLvStageWrap()
            $(`input[name="luckyScratchRewardConfig[${i}][start]"]`).val(o.start)
            $(`input[name="luckyScratchRewardConfig[${i}][end]"]`).val(o.end)
            $.each(o.stages, function(i0, o0){
                let spinIdx = i0
                addLuckyScratchSpinStageWrap(lvIdx)

                $(`input[name="luckyScratchRewardConfig[${i}][stages][${i0}][start]"]`).val(o0.start)
                $(`input[name="luckyScratchRewardConfig[${i}][stages][${i0}][end]"]`).val(o0.end)

                $.each(o0.stages, function(i1, o1){
                    let shushuIdx = i1
                    addLuckyScratchShushuStageWrap(lvIdx, spinIdx)
                    $(`select[name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][shushu][]"]`).val(o1.shushu)
                    console.log(o1);

                    o1.collect && $.each(o1.collect, function(i2, o2){
                        let rewardIdx = i2
                        $(`input[name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${rewardIdx}][grid_num]"]`).val(o2.grid_num)
                        $(`input[name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${rewardIdx}][obtain_num]"]`).val(o2.obtain_num)
                        $(`input[name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${rewardIdx}][begin_num]"]`).val(o2.begin_num)
                        $(`input[name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${rewardIdx}][rewards]"]`).val(escape(JSON.stringify(o2.rewards)))
                        $(`.luckyScratchRewardConfig_${lvIdx}_${spinIdx}_${shushuIdx}_${rewardIdx}_prizeShow`).html(prizeListHtml(o2.rewards))
                    })


                })
            })
        })
    }
}
function initLuckyScratchWrap(){
    $("#reward_info").empty()
    // 关卡数据
    luckyScratchMainConfig();
}

// 主配置
function luckyScratchMainConfig(){
    var html ='';
    var str='奖励配置'
    luckyScratchLvCnt = 0;
    luckyScratchSpinsCnt = {};
    luckyScratchShushuCnt = {};
    luckyScratchStageCnt = {}
    luckyScratchStageRewardCnt = {}
    luckyScratchCollectStageRewardCnt = {}
    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="addLuckyScratchLvWrapBtn" onclick="addLuckyScratchLvStageWrap()" type="button">添加等级区间</button>';
    html+='<button class="btn btn-white"   id="delLuckyScratchLvWrapBtn" onclick="delLuckyScratchLvStageWrap()" type="button">删除等级区间</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    $("#reward_info").append(html);
}

function delLuckyScratchLvStageWrap(){
    if(luckyScratchLvCnt < 1) return
    $(`#luckyScratchLvWrap_${luckyScratchLvCnt}`).remove()
    luckyScratchLvCnt--
}
function addLuckyScratchLvStageWrap(){
    luckyScratchLvCnt++
    luckyScratchSpinsCnt[luckyScratchLvCnt] = 0;
    var html = `
         <div class="form-group luckyScratchLvWrap " id="luckyScratchLvWrap_${luckyScratchLvCnt}" data-idx="${luckyScratchLvCnt}" 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="luckyScratchRewardConfig[${luckyScratchLvCnt}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="luckyScratchRewardConfig[${luckyScratchLvCnt}][end]" value="0">
                </div>
            </div>
            <div class="btnGroups">
                <botton class="btn btn-primary  addLuckyScratchStageWrapBtn" onclick="addLuckyScratchSpinStageWrap(${luckyScratchLvCnt})">添加体力层级</botton>
                <botton class="btn btn-danger del-tr delLuckyScratchStageWrapBtn" onclick="delLuckyScratchSpinStageWrap(${luckyScratchLvCnt})">删除体力层级</botton>
            </div>
         </div>`

    $('#addLuckyScratchLvWrapBtn').before(html)
    $('.select2').select2({})
}

function delLuckyScratchSpinStageWrap(lvIdx){
    let spinIdx = luckyScratchSpinsCnt[lvIdx]
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    if(spinIdx < 1) return
    $(`#luckyScratchSpinWrap_${lvSpinIdx}`).remove()
    luckyScratchSpinsCnt[lvIdx]--
}
function addLuckyScratchSpinStageWrap(lvIdx){
    let spinIdx = ++luckyScratchSpinsCnt[lvIdx]
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    luckyScratchShushuCnt[lvSpinIdx] = 0;
    var html = `
         <div class="form-group luckyScratchSpinWrap " id="luckyScratchSpinWrap_${lvSpinIdx}" data-idx="${lvSpinIdx}" 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="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][end]" value="0">
                </div>
            </div>
            <div class="btnGroups">
                <botton class="btn btn-primary  addLuckyScratchStageWrapBtn" onclick="addLuckyScratchShushuStageWrap(${lvIdx}, ${spinIdx})">添加数数分组层级</botton>
                <botton class="btn btn-danger del-tr delLuckyScratchStageWrapBtn" onclick="delLuckyScratchShushuStageWrap(${lvIdx}, ${spinIdx})">删除数数分组层级</botton>
            </div>
         </div>`

    $(`#luckyScratchLvWrap_${lvIdx}`).find('.btnGroups:last').before(html)
    $('.select2').select2({})
}

// 数数分组
function delLuckyScratchShushuStageWrap(lvIdx, spinIdx){
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${luckyScratchShushuCnt[lvSpinIdx]}`
    if(luckyScratchShushuCnt[lvSpinIdx] < 1)return;
    $(`#luckyScratchShushuWrap_${lvSpinShushuIdx}`).remove()
    luckyScratchShushuCnt[lvSpinIdx]--
}
function addLuckyScratchShushuStageWrap(lvIdx, spinIdx){
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    let shushuIdx = ++luckyScratchShushuCnt[lvSpinIdx]
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    let ssOptionHtml =   ''
    $.each(shushuGroups, function(i,o){
        ssOptionHtml += `<option value="${i}">${o}</option>`
    });
    luckyScratchStageCnt[lvSpinShushuIdx] = 0;
    var html = `
         <div class="form-group luckyScratchShushuWrap luckyScratchShushuWrap_${lvSpinShushuIdx}" id="luckyScratchShushuWrap_${lvSpinShushuIdx}" data-idx="${lvSpinShushuIdx}" style="margin:10px;padding:10px;border:solid 1px black;">
             <div class="row">
                 <div class="col-sm-2"><lable class="form-control">数数分组：</lable></div>
                 <div class="col-sm-5">
                    <select class=" select2" name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][shushu][]"  multiple  style="min-width: 100%">
                        ${ssOptionHtml}
                    </select>
                 </div>
             </div>
             <div class="btnGroups" style="display: none">
                 <botton class="btn btn-primary  addLuckyScratchStageWrapBtn" onclick="addLuckyScratchStageRewardTable(${lvIdx}, ${spinIdx}, ${shushuIdx})">添加关卡</botton>
                 <botton class="btn btn-danger del-tr delLuckyScratchStageWrapBtn" onclick="delLuckyScratchStageRewardTable(${lvIdx}, ${spinIdx}, ${shushuIdx})">删除关卡</botton>
             </div>
             
          </div>`

    $(`#luckyScratchSpinWrap_${lvSpinIdx}`).find('.btnGroups:last').before(html)
    $('.select2').select2({})
    addLuckyScratchCollectStageRewardTable(lvIdx, spinIdx, shushuIdx)
}

//收集阶段奖励配置表html
function addLuckyScratchCollectStageRewardTable(lvIdx, spinIdx, shushuIdx) {
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    luckyScratchCollectStageRewardCnt[lvSpinShushuIdx] = 0;
    var cols = 4;
    var html = `
    <table data-idx="${lvSpinShushuIdx}" class="table table-bordered luckyScratchCollectStageTable" id="collect_table_${lvSpinShushuIdx}">
        <thead>
            <tr>
                <th>总格数</th>
                <th>活动开始直接获得印章数</th>
                <th>每次购买获得印章数</th>
                <th>奖励</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="collect_tbody_${lvSpinShushuIdx}">
    
        </tbody>
        <tr style="display: none">
            <td colspan="${cols}">
                <div class="text-center">
                    <botton class="btn btn-primary addLuckyScratchRewardTrBtn" onclick="addLuckyScratchCollectRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx})">添加阶段</botton>
                    <botton class="btn btn-danger  delLuckyScratchRewardTrBtn" onclick="delLuckyScratchCollectRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx})">删除阶段</botton>
                </div>
            </td>
        </tr>
    </table>`
    $(`#luckyScratchShushuWrap_${lvSpinShushuIdx}`).find('.btnGroups:last').before(html);
    addLuckyScratchCollectRewardTr(lvIdx, spinIdx, shushuIdx)
}
// 收集阶段奖励行
function addLuckyScratchCollectRewardTr(lvIdx, spinIdx, shushuIdx) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    var stage = luckyScratchCollectStageRewardCnt[complexIdx]

    var reward = '[]';
    reward = escape(reward);
    var html =`
            <tr class=" commonStageRewardTr luckyScratchRewardTr luckyScratchCollectStageRewardTr" data-idx="${stage}" id="luckyScratchCollectStageRewardTr_${complexIdx}_${luckyScratchCollectStageRewardCnt[complexIdx]}">
                <td>
                    <input type="text" class="form-control" name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${stage}][grid_num]" value="0">
                </td>
                <td>
                    <input type="text" class="form-control" name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${stage}][begin_num]" value="0">
                </td>
                <td>
                    <input type="text" class="form-control" name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${stage}][obtain_num]" value="0">
                </td>
                <td class="prizeShow  luckyScratchRewardConfig_${lvIdx}_${spinIdx}_${shushuIdx}_${stage}_prizeShow"></td>
                <td>
                    <input type="hidden" class="prizeInput" name="luckyScratchRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${stage}][rewards]" value="${reward}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td>
            </tr>`

    $('#collect_tbody_'+complexIdx).append(html);
}
function delLuckyScratchCollectRewardTr(lvIdx, spinIdx, shushuIdx) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    if(luckyScratchCollectStageRewardCnt[complexIdx]<1)return
    $(`#luckyScratchCollectStageRewardTr_${complexIdx}_${luckyScratchCollectStageRewardCnt[complexIdx]}`).remove();
    luckyScratchCollectStageRewardCnt[complexIdx]--
}