let serialMissionLvCnt = 0;
let serialMissionSpinsMap = {};
let serialMissionShushuMap = {};
let serialMissionStagesMap = {};
let serialMissionStagesTaskMap = {};
function changeTypeTo_SerialMission() {
    initSerialMissionWrap().then()
}

function renderSerialMissionConfig() {
    initSerialMissionWrap().then(function(){
        reward_conf && $.each(reward_conf,  function(lvIdx, item1){
            console.log(item1);
            addSerialMissionLvWrap();
            $(`input[name="serialMissionConfig[${lvIdx}][start]"]`).val(item1.start);
            $(`input[name="serialMissionConfig[${lvIdx}][end]"]`).val(item1.end);


            item1.stages && $.each(item1.stages,  function(spinsIdx, item2){
                addSerialMissionSpinsWrap(lvIdx);
                $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][start]"]`).val(item2.start);
                $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][end]"]`).val(item2.end);

                $(`#serialMissionSpinsWrap_${lvIdx}_${spinsIdx}`).find('.opBtns').remove()
                $(`#serialMissionSpinsWrap_${lvIdx}_${spinsIdx}`).append(`<button class="btn btn-primary asyncLoadConfigBtn" style="" type="button" onclick="asyncLoadSerialMissionConfig(${lvIdx}, ${spinsIdx})">加载配置</button>`)
                $('#submitBtn').hide();

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

async function initSerialMissionWrap() {
    if (typeof serialMissionTaskMap === 'undefined') {
        showLoading()
        await $.get('/activity/getActBasicConfig/serial_missions',function(response){
            if (typeof response === 'string')response = JSON.parse(response)
            hideLoading()
            serialMissionTaskMap = response.data
        })
    }
    $("#reward_info").append(serialMissionStagesRewards());
}

// 分阶段奖励配置
function serialMissionStagesRewards(){
    return `<div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">分阶段奖励配置</h5></div>
    <div class="rewardsWrap serialMissionStagesRewardsWrap" id="serialMissionStagesRewardsWrap">
        <div class="opBtns">
            <button type="button" class="btn btn-primary addSerialMissionLvWrap" onclick="addSerialMissionLvWrap()">添加等级区间</button>
            <button type="button" class="btn btn-danger  delSerialMissionLvWrap" onclick="delSerialMissionLvWrap()">删除等级区间</button>
        </div>
    </div>`;
}


// 等级区间配置
function addSerialMissionLvWrap(){
    serialMissionLvCnt++;
    serialMissionSpinsMap[serialMissionLvCnt] = 0;
    let html = `<div class="rewardsWrap serialMissionLvWrap_${serialMissionLvCnt}" id="serialMissionLvWrap_${serialMissionLvCnt}">
                <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="serialMissionConfig[${serialMissionLvCnt}][start]" value="0">
                        <span class="input-group-addon">-</span>
                        <input type="text" style="text-align: center" class="form-control" name="serialMissionConfig[${serialMissionLvCnt}][end]" value="0">
                    </div>
                </div>
                <div class="opBtns ">
                    <botton  type="button" class="btn btn-primary addSerialMissionSpinsWrap"  onclick="addSerialMissionSpinsWrap(${serialMissionLvCnt})" >添加体力区间</botton>
                    <botton  type="button" class="btn btn-danger  delSerialMissionSpinsWrap"  onclick="delSerialMissionSpinsWrap(${serialMissionLvCnt})" >删除体力区间</botton>
                </div>
            </div>`;
    $(`#serialMissionStagesRewardsWrap`).children('.opBtns').before(html)
}
function delSerialMissionLvWrap(){
    if(serialMissionLvCnt > 0 && $(`#serialMissionLvWrap_${serialMissionLvCnt}`).length > 0){
        $(`#serialMissionLvWrap_${serialMissionLvCnt}`).remove();
        serialMissionLvCnt--
    }
}


// 体力区间配置
function addSerialMissionSpinsWrap(idx1){
    let idx2 = ++serialMissionSpinsMap[idx1]
    let key3 = `${idx1}_${idx2}`
    serialMissionShushuMap[key3] = 0
    let html = `<div class=" rewardsWrap serialMissionSpinsWrap_${key3}" id="serialMissionSpinsWrap_${key3}">
                <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="serialMissionConfig[${idx1}][stages][${idx2}][start]" value="0">
                        <span class="input-group-addon">-</span>
                        <input type="text" style="text-align: center" class="form-control" name="serialMissionConfig[${idx1}][stages][${idx2}][end]" value="0">
                    </div>
                </div>
                <div class="opBtns ">
                    <botton type="button" class="btn btn-primary addSerialMissionShushuWrap"  onclick="addSerialMissionShushuWrap(${idx1}, ${idx2})" >添加数数区间</botton>
                    <botton type="button" class="btn btn-danger  delSerialMissionShushuWrap"  onclick="delSerialMissionShushuWrap(${idx1}, ${idx2})" >删除数数区间</botton>
                </div>
            </div>`;
    $(`#serialMissionLvWrap_${idx1}`).children('.opBtns').before(html)
}
function delSerialMissionSpinsWrap(idx1){
    let idx2 = serialMissionSpinsMap[idx1]
    let key3 = `${idx1}_${idx2}`
    if(idx2 > 0 && $(`#serialMissionSpinsWrap_${key3}`).length > 0){
        $(`#serialMissionSpinsWrap_${key3}`).remove();
        serialMissionSpinsMap[idx1]--
    }
}

// 数数区间配置
function addSerialMissionShushuWrap(idx1, idx2){
    let key3 = `${idx1}_${idx2}`;
    let idx3 = ++serialMissionShushuMap[key3];
    let key4 = `${idx1}_${idx2}_${idx3}`;
    serialMissionStagesMap[key4] = -1;
    serialMissionStagesTaskMap[key4] = -1;

    let optionsHtml = getShushuSelectorOptions()
    let html = `<div class=" rewardsWrap serialMissionShushuWrap_${key4}" id="serialMissionShushuWrap_${key4}">
                    <div class="form-group">
                        <label class="col-sm-2 control-label m-b">数数分组</label>
                        <div class="col-sm-3 input-group">
                            <select class="select2  " style="width: 100%" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][shushu][]" multiple>
                                ${optionsHtml}
                            </select>
                        </div>
                        <libel class="col-sm-2 control-label m-b">体力总量:</libel>
                        <div class="col-sm-3 input-group">
                            <input type="text" class="form-control" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][sumSpins]">
                        </div>                        
                    </div>`

    html += `
            <label class="col-sm-2 control-label m-b " style="text-align: left">任务配置</label>
            <table class="table table-bordered ">
                <thead>
                    <th>任务id</th>
                    <th>所在天</th>
                    <th>任务类型</th>
                    <th>任务值</th>
                    <th>积分</th>
                    <th>奖品</th>
                    <th>奖励设置</th>
                </thead>
                <tbody class="serialMissionStageTaskWrap_${key4}" id="serialMissionStageTaskWrap_${key4}">
                
                </tbody>
            </table>
            <div class="opBtns ">
                <botton type="button" class="btn btn-primary addSerialMissionStageTaskRewardTr"  onclick="addSerialMissionStageTaskRewardTr( ${idx1}, ${idx2}, ${idx3})" >添加</botton>
                <botton type="button" class="btn btn-danger  delSerialMissionStageTaskRewardTr"  onclick="delSerialMissionStageTaskRewardTr( ${idx1}, ${idx2}, ${idx3})" >删除</botton>
            </div>`


    html += `
                <label class="col-sm-2 control-label m-b " style="text-align: left">阶段奖励配置</label>
                <table class="table table-bordered ">
                    <thead>
                        <th>阶段</th>
                        <th>阶段值</th>
                        <th>奖品</th>
                        <th>奖励设置</th>
                    </thead>
                    <tbody class="serialMissionStageWrap_${key4}" id="serialMissionStageWrap_${key4}">
                    
                    </tbody>
                </table>
                <div class="opBtns ">
                    <botton type="button" class="btn btn-primary addSerialMissionStageRewardTr"  onclick="addSerialMissionStageRewardTr(${idx1}, ${idx2}, ${idx3})" >添加</botton>
                    <botton type="button" class="btn btn-danger  delSerialMissionStageRewardTr"  onclick="delSerialMissionStageRewardTr(${idx1}, ${idx2}, ${idx3})" >删除</botton>
                </div>`;
    html += ` </div>`

    $(`#serialMissionSpinsWrap_${key3}`).children('.opBtns').before(html)
    $('.select2').select2({})
}
function delSerialMissionShushuWrap(idx1, idx2){
    let key3 = `${idx1}_${idx2}`;
    let idx3 = serialMissionShushuMap[key3];
    let key4 = `${idx1}_${idx2}_${idx3}`;
    if(idx3 > 0 && $(`#serialMissionShushuWrap_${key4}`).length > 0){
        $(`#serialMissionShushuWrap_${key4}`).remove();
        serialMissionShushuMap[key3]--
    }
}

// 任务配置
function addSerialMissionStageTaskRewardTr(idx1, idx2, idx3){
    let key4 = `${idx1}_${idx2}_${idx3}`;
    let idx4 = ++serialMissionStagesTaskMap[key4];

    let taskOptions = '';
    $.each(serialMissionTaskMap, function(i, t){
        taskOptions += `<option value="${i}">${t}</option>`
    })
    let html = `
<tr class="commonStageRewardTr">
    
    <td><input type="text" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][stage]" value="${idx4 + 1}" readonly></td>
    <td><input type="text" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][day]" value="${idx4}" ></td>
    <td><select class=""  name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][taskId]" >${taskOptions}</select></td>
    <td><input type="text" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][maxNum]" value="${idx4}" ></td>
    <td><input type="text" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][addRate]" value="${idx4}" ></td>
    <td class=" prizeShow showStagePrize"></td>
    <td class=" ">
        <input class="prizeInput" type="hidden" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][prize]" value="%5B%5D">
        <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this, 'setCommonPrize', 1)">奖励配置</button>
    </td>
</tr>
`;
    $(`#serialMissionStageTaskWrap_${key4}`).append(html)
}
function delSerialMissionStageTaskRewardTr(idx1, idx2, idx3){
    let key4 = `${idx1}_${idx2}_${idx3}`;
    let idx4 = serialMissionStagesTaskMap[key4];

    if(idx4 > -1 && $(`#serialMissionStageTaskWrap_${key4}`).find('tr').length > 0  ){
        $(`#serialMissionStageTaskWrap_${key4}`).find('tr:last').remove();
        serialMissionStagesTaskMap[key4]--
    }
}

// 阶段奖励配置
function addSerialMissionStageRewardTr(idx1, idx2, idx3){
    let key4 = `${idx1}_${idx2}_${idx3}`;
    let idx4 = ++serialMissionStagesMap[key4];

    let html = `
    <tr class="commonStageRewardTr">
        <td><input type="text" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][stages][${idx4}][stage]" value="${idx4 + 1}" readonly></td>
        <td><input type="text" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][stages][${idx4}][rate]" value="0" ></td>
        <td class=" prizeShow showStagePrize"></td>
        <td class=" ">
            <input class="prizeInput" type="hidden" name="serialMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][stages][${idx4}][prize]" value="%5B%5D">
            <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this, 'setCommonPrize', 1)">奖励配置</button>
        </td>
    </tr>
`;
    $(`#serialMissionStageWrap_${key4}`).append(html)
}

function delSerialMissionStageRewardTr(idx1, idx2, idx3){
    let key4 = `${idx1}_${idx2}_${idx3}`;
    let idx4 = serialMissionStagesMap[key4];

    if(idx4 > -1 && $(`#serialMissionStageWrap_${key4}`).find('tr').length > 0  ){
        $(`#serialMissionStageWrap_${key4}`).find('tr:last').remove();
        serialMissionStagesMap[key4]--
    }
}


function asyncLoadSerialMissionConfig(lvIdx, spinsIdx){
    showLoading()
    var btnsHtml = `
                <div class="opBtns ">
                    <botton type="button" class="btn btn-primary addSerialMissionShushuWrap"  onclick="addSerialMissionShushuWrap(${lvIdx}, ${spinsIdx})" >添加数数区间</botton>
                    <botton type="button" class="btn btn-danger  delSerialMissionShushuWrap"  onclick="delSerialMissionShushuWrap(${lvIdx}, ${spinsIdx})" >删除数数区间</botton>
                </div>` ;
    $(`#serialMissionSpinsWrap_${lvIdx}_${spinsIdx}`).find('.asyncLoadConfigBtn').remove()
    $(`#serialMissionSpinsWrap_${lvIdx}_${spinsIdx}`).append(btnsHtml)
    reward_conf[lvIdx]['stages'][spinsIdx]['stages'] && $.each(reward_conf[lvIdx]['stages'][spinsIdx]['stages'],  function(ssIdx, item3){
        addSerialMissionShushuWrap(lvIdx, spinsIdx);
        $(`select[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][shushu][]"]`).val(item3.shushu);
        if(item3.stages && item3.stages.sumSpins) {
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][sumSpins]"]`).val(item3.stages.sumSpins);
        }
        item3.stages && item3.stages.tasks && $.each(item3.stages.tasks,  function(taskIdx, item){
            addSerialMissionStageTaskRewardTr(lvIdx, spinsIdx,ssIdx);
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][stage]"]`).val(item.stage);
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][day]"]`).val(item.day);
            $(`select[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][taskId]"]`).val(item.taskId);
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][maxNum]"]`).val(item.maxNum);
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][addRate]"]`).val(item.addRate);
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][prize]"]`).val(escape(JSON.stringify(item.prize)));
            $(`.serialMissionStageTaskWrap_${lvIdx}_${spinsIdx}_${ssIdx}`).find(`.prizeShow:last`).html(prizeListHtml(item.prize))
        })
        item3.stages && item3.stages.stages && $.each(item3.stages.stages,  function(stageId, item){
            addSerialMissionStageRewardTr(lvIdx, spinsIdx,ssIdx);
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][stages][${stageId}][stage]"]`).val(item.stage);
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][stages][${stageId}][rate]"]`).val(item.rate);
            $(`input[name="serialMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][stages][${stageId}][prize]"]`).val(escape(JSON.stringify(item.prize)));
            $(`.serialMissionStageWrap_${lvIdx}_${spinsIdx}_${ssIdx}`).find(`.prizeShow:last`).html(prizeListHtml(item.prize))
        })
    })

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

}







