
function changeTypeTo_pinata(){
    initPinataWrap()
}
function initReward_pinata(){
    initPinataWrap()
    // 关卡回写
    if(activity_extra_data && activity_extra_data!='null'){
        $.each(activity_extra_data, function(i, o){
            let lvIdx = i
            addPinataLvStageWrap()
            $(`input[name="pinataRewardConfig[${i}][start]"]`).val(o.start)
            $(`input[name="pinataRewardConfig[${i}][end]"]`).val(o.end)
            $.each(o.stages, function(i0, o0){
                let spinIdx = i0
                addPinataSpinStageWrap(lvIdx)

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

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

                    o1.collect && $.each(o1.collect, function(i2, o2){
                        let rewardIdx = i2
                        addPinataCollectRewardTr(lvIdx, spinIdx, shushuIdx)
                        $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${rewardIdx}][stage]"]`).val(o2.stage)
                        $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${rewardIdx}][target]"]`).val(o2.target)
                        $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${rewardIdx}][rewards]"]`).val(escape(JSON.stringify(o2.rewards)))
                        var prizeShowStr = ''
                        $.each(o2.rewards, function(prizdIdx, reward){
                            prizeShowStr += chestPropsHtml(reward)
                        })
                        $('.prizeShow:last').html(prizeShowStr)

                    })

                    o1.pinata && $.each(o1.pinata, function(i2, o2){
                        let rewardIdx = i2
                        addPinataStageRewardTable(lvIdx, spinIdx, shushuIdx)
                        $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][stage]"]`).val(o2.stage)
                        $.each(o2.rewards, function(i2, o2){
                            addPinataRewardTr(lvIdx, spinIdx, shushuIdx, rewardIdx)
                            $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${i2}][collect_num]"]`).val(o2.collect_num)
                            $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${i2}][id]"]`).val(o2.id)
                            $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${i2}][min_token_spend]"]`).val(o2.min_token_spend)
                            $(`select[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${i2}][type]"]`).val(o2.type)
                            $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${i2}][weight]"]`).val(o2.weight)
                            $(`input[name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${i2}][reward]"]`).val(escape(JSON.stringify(o2.reward)))
                            var prizeShowStr = ''
                            $.each(o2.reward, function(prizdIdx, reward){
                                prizeShowStr += chestPropsHtml(reward)
                            })
                            $('.prizeShow:last').html(prizeShowStr)
                        })

                    })

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



// 主配置
function pinataMainConfig(){
    var html ='';
    var str='关卡奖励'
    pinataLvCnt = 0;
    pinataSpinsCnt = {};
    pinataShushuCnt = {};
    pinataStageCnt = {}
    pinataStageRewardCnt = {}
    pinataCollectStageRewardCnt = {}
    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="addPinataLvWrapBtn" onclick="addPinataLvStageWrap()" type="button">添加等级区间</button>';
    html+='<button class="btn btn-white"   id="delPinataLvWrapBtn" onclick="delPinataLvStageWrap()" type="button">删除等级区间</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    $("#reward_info").append(html);
}

function delPinataLvStageWrap(){
    if(pinataLvCnt < 1) return
    $(`#pinataLvWrap_${pinataLvCnt}`).remove()
    pinataLvCnt--
}
function addPinataLvStageWrap(){
    pinataLvCnt++
    pinataSpinsCnt[pinataLvCnt] = 0;
    var html = `
         <div class="form-group pinataLvWrap " id="pinataLvWrap_${pinataLvCnt}" data-idx="${pinataLvCnt}" 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="pinataRewardConfig[${pinataLvCnt}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="pinataRewardConfig[${pinataLvCnt}][end]" value="0">
                </div>
            </div>
            <div class="btnGroups">
                <botton class="btn btn-primary  addPinataStageWrapBtn" onclick="addPinataSpinStageWrap(${pinataLvCnt})">添加体力层级</botton>
                <botton class="btn btn-danger del-tr delPinataStageWrapBtn" onclick="delPinataSpinStageWrap(${pinataLvCnt})">删除体力层级</botton>
            </div>
         </div>`

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

function delPinataSpinStageWrap(lvIdx){
    let spinIdx = pinataSpinsCnt[lvIdx]
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    if(spinIdx < 1) return
    $(`#pinataSpinWrap_${lvSpinIdx}`).remove()
    pinataSpinsCnt[lvIdx]--
}
function addPinataSpinStageWrap(lvIdx){
    let spinIdx = ++pinataSpinsCnt[lvIdx]
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    pinataShushuCnt[lvSpinIdx] = 0;
    var html = `
         <div class="form-group pinataSpinWrap " id="pinataSpinWrap_${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="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][end]" value="0">
                </div>
            </div>
            <div class="btnGroups">
                <botton class="btn btn-primary  addPinataStageWrapBtn" onclick="addPinataShushuStageWrap(${lvIdx}, ${spinIdx})">添加数数分组层级</botton>
                <botton class="btn btn-danger del-tr delPinataStageWrapBtn" onclick="delPinataShushuStageWrap(${lvIdx}, ${spinIdx})">删除数数分组层级</botton>
            </div>
         </div>`

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

// 数数分组
function delPinataShushuStageWrap(lvIdx, spinIdx){
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${pinataShushuCnt[lvSpinIdx]}`
    if(pinataShushuCnt[lvSpinIdx] < 1)return;
    $(`#pinataShushuWrap_${lvSpinShushuIdx}`).remove()
    pinataShushuCnt[lvSpinIdx]--
}
function addPinataShushuStageWrap(lvIdx, spinIdx){
    let lvSpinIdx = `${lvIdx}_${spinIdx}`
    let shushuIdx = ++pinataShushuCnt[lvSpinIdx]
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    pinataStageCnt[lvSpinShushuIdx] = 0;
    var html = `
         <div class="form-group pinataShushuWrap pinataShushuWrap_${lvSpinShushuIdx}" id="pinataShushuWrap_${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="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][shushu][]"  multiple  style="min-width: 100%">`
    $.each(shushuGroups, function(i,o){
        html += '<option value="'+i+'">'+o+'</option>'
    });
    html+=`
                    </select>
                 </div>
             </div>
             <div class="btnGroups">
                 <botton class="btn btn-primary  addPinataStageWrapBtn" onclick="addPinataStageRewardTable(${lvIdx}, ${spinIdx}, ${shushuIdx})">添加关卡</botton>
                 <botton class="btn btn-danger del-tr delPinataStageWrapBtn" onclick="delPinataStageRewardTable(${lvIdx}, ${spinIdx}, ${shushuIdx})">删除关卡</botton>
             </div>
             
          </div>`

    $(`#pinataSpinWrap_${lvSpinIdx}`).find('.btnGroups:last').before(html)
    $('.select2').select2({})
    addPinataCollectStageRewardTable(lvIdx, spinIdx, shushuIdx)
    // addPinataStageRewardTable(lvIdx, spinIdx, shushuIdx)
}

//奖励配置表html
function delPinataStageRewardTable(lvIdx, spinIdx, shushuIdx){
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    let lvSpinShushuRewardIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${pinataStageCnt[lvSpinShushuIdx]}`
    if(pinataStageCnt[lvSpinShushuIdx]<1)return
    $(`#reward_table_${lvSpinShushuRewardIdx}`).remove();
    pinataStageCnt[lvSpinShushuIdx]--
}
function addPinataStageRewardTable(lvIdx, spinIdx, shushuIdx) {
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`

    let rewardIdx = ++pinataStageCnt[lvSpinShushuIdx]
    var index = rewardIdx
    var data ={"stage":rewardIdx,"end":0};
    var stageIdx = lvSpinShushuIdx + '_' + index
    pinataStageRewardCnt[stageIdx] = -1;
    var cols = 7;
    var html = `
    <table data-idx="${index}" class="table table-bordered pinataStageTable" 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 readonly type="text" style="text-align: center" class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][stage]" value="${data['stage']}">
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <th>序号</th>
                <th>是否大奖</th>
                <th>权重值</th>
                <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 addPinataRewardTrBtn" onclick="addPinataRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx}, ${rewardIdx})">添加</botton>
                    <botton class="btn btn-danger  delPinataRewardTrBtn" onclick="delPinataRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx}, ${rewardIdx})">删除</botton>
                </div>
            </td>
        </tr>
    </table>`
    $(`#pinataShushuWrap_${lvSpinShushuIdx}`).find('.btnGroups:last').before(html);
}


// 奖励行
function addPinataRewardTr(lvIdx, spinIdx, shushuIdx, rewardIdx) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${rewardIdx}`
    var stage = ++pinataStageRewardCnt[complexIdx]

    var reward = '[]';
    reward = escape(reward);
    var html =`
            <tr class="pinataRewardTr commonStageRewardTr" data-idx="${stage}" id="commonStageRewardTr_${complexIdx}">
                <td>
                    <input type="text" class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${stage}][id]" value="${stage + 1}">
                </td>
                <td>
                    <select class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${stage}][type]"  >
                        <option value="3">通用奖励</option>
                        <option value="2">纪念品</option>
                        <option value="1">大奖</option>
                    </select>
                </td>
                <td>
                    <input type="text" class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${stage}][weight]" value="0">
                </td>
                <td>
                    <input type="text" class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${stage}][min_token_spend]" value="0">
                </td>
                <td>
                    <input type="text" class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${stage}][collect_num]" value="0">
                </td>
                <td class="prizeShow "></td>
                <td>
                    <input type="hidden" class="prizeInput" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][pinata][${rewardIdx}][rewards][${stage}][reward]" value="${reward}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                </td>
            </tr>`

    $('#table_body_'+complexIdx).append(html);
}
function delPinataRewardTr(lvIdx, spinIdx, shushuIdx, rewardIdx) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}_${rewardIdx}`
    if(pinataStageRewardCnt[complexIdx] < 0)return
    $('#table_body_'+complexIdx).find('tr:last').remove();
    pinataStageRewardCnt[complexIdx]--
}

//收集阶段奖励配置表html
function addPinataCollectStageRewardTable(lvIdx, spinIdx, shushuIdx) {
    let lvSpinShushuIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    pinataCollectStageRewardCnt[lvSpinShushuIdx] = 0;
    var cols = 4;
    var html = `
    <table data-idx="${lvSpinShushuIdx}" class="table table-bordered pinataCollectStageTable" id="collect_table_${lvSpinShushuIdx}">
        <thead>
            <tr>
                <th>阶段</th>
                <th>阶段值</th>
                <th>奖品</th>
                <th>奖励设置</th>
            </tr>
        </thead>
        <tbody id="collect_tbody_${lvSpinShushuIdx}">
    
        </tbody>
        <tr>
            <td colspan="${cols}">
                <div class="text-center">
                    <botton class="btn btn-primary addPinataRewardTrBtn" onclick="addPinataCollectRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx})">添加阶段</botton>
                    <botton class="btn btn-danger  delPinataRewardTrBtn" onclick="delPinataCollectRewardTr(${lvIdx}, ${spinIdx}, ${shushuIdx})">删除阶段</botton>
                </div>
            </td>
        </tr>
    </table>`
    $(`#pinataShushuWrap_${lvSpinShushuIdx}`).find('.btnGroups:last').before(html);
}
// 收集阶段奖励行
function addPinataCollectRewardTr(lvIdx, spinIdx, shushuIdx) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    var stage = ++pinataCollectStageRewardCnt[complexIdx]

    var reward = '[]';
    reward = escape(reward);
    var html =`
            <tr class=" commonStageRewardTr pinataRewardTr pinataCollectStageRewardTr" data-idx="${stage}" id="pinataCollectStageRewardTr_${complexIdx}_${pinataCollectStageRewardCnt[complexIdx]}">
                <td>
                    <input type="text" class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${stage}][stage]" value="${stage}">
                </td>
                <td>
                    <input type="text" class="form-control" name="pinataRewardConfig[${lvIdx}][stages][${spinIdx}][stages][${shushuIdx}][collect][${stage}][target]" value="0">
                </td>
                <td class="prizeShow "></td>
                <td>
                    <input type="hidden" class="prizeInput" name="pinataRewardConfig[${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 delPinataCollectRewardTr(lvIdx, spinIdx, shushuIdx) {
    var complexIdx = `${lvIdx}_${spinIdx}_${shushuIdx}`
    if(pinataCollectStageRewardCnt[complexIdx]<1)return
    $(`#pinataCollectStageRewardTr_${complexIdx}_${pinataCollectStageRewardCnt[complexIdx]}`).remove();
    pinataCollectStageRewardCnt[complexIdx]--
}