let dreamMissionLvCnt = 0;
let dreamMissionSpinsMap = {};
let dreamMissionShushuMap = {};
let dreamMissionStagesMap = {};
let dreamMissionStagesTaskMap = {};
function changeTypeTo_DreamMission() {
    initDreamMissionWrap().then()
}

function renderDreamMissionConfig() {
    initDreamMissionWrap().then(function(){
        reward_conf && $.each(reward_conf,  function(lvIdx, item1){
            console.log(item1);
            addDreamMissionLvWrap();
            $(`input[name="dreamMissionConfig[${lvIdx}][start]"]`).val(item1.start);
            $(`input[name="dreamMissionConfig[${lvIdx}][end]"]`).val(item1.end);


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

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

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

async function initDreamMissionWrap() {
    if (typeof dreamMissionTaskMap === 'undefined') {
        showLoading()
        await $.get('/activity/getActBasicConfig/dream_missions',function(response){
            if (typeof response === 'string')response = JSON.parse(response)
            hideLoading()
            dreamMissionTaskMap = response.data
        })
    }
    $("#reward_info").append(dreamMissionStagesRewards());
}

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


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


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

// 数数区间配置
function addDreamMissionShushuWrap(idx1, idx2){
    let key3 = `${idx1}_${idx2}`;
    let idx3 = ++dreamMissionShushuMap[key3];
    let key4 = `${idx1}_${idx2}_${idx3}`;
    dreamMissionStagesMap[key4] = -1;
    dreamMissionStagesTaskMap[key4] = -1;

    let optionsHtml = getShushuSelectorOptions()
    let html = `<div class=" rewardsWrap dreamMissionShushuWrap_${key4}" id="dreamMissionShushuWrap_${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="dreamMissionConfig[${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="dreamMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][sumSpins]">
                        </div>
                    </div>`

    let goodSkuHtml = ""
    $.each(goodSku, function (k, v) {
        goodSkuHtml += `<option value="${v["price"]}">${v["price"]}</option>`;
    });
    html +=`<table class="table table-bordered ">
                            <thead>
                    <th>价格</th>
                    <th>sku</th>
                </thead>
                <tbody class="priceSkuGroup">
                    <tr>
                        <td>
                            <select onchange="priceChanged(this)" class="form-control" name="dreamMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][price]" >
                                ${goodSkuHtml}
                            </select>
                        </td>
                        <td>
                            <input type="text" class="form-control skuInput" name="dreamMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][sku]" value="" readonly>
                        </td>
                    <tr>
                </tbody>                              
            </table>
            `

    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>
                </thead>
                <tbody class="dreamMissionStageTaskWrap_${key4}" id="dreamMissionStageTaskWrap_${key4}">
                
                </tbody>
            </table>
            <div class="opBtns ">
                <botton type="button" class="btn btn-primary addDreamMissionStageTaskRewardTr"  onclick="addDreamMissionStageTaskRewardTr( ${idx1}, ${idx2}, ${idx3})" >添加</botton>
                <botton type="button" class="btn btn-danger  delDreamMissionStageTaskRewardTr"  onclick="delDreamMissionStageTaskRewardTr( ${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="dreamMissionStageWrap_${key4}" id="dreamMissionStageWrap_${key4}">
    //
    //                 </tbody>
    //             </table>
    //             <div class="opBtns ">
    //                 <botton type="button" class="btn btn-primary addDreamMissionStageRewardTr"  onclick="addDreamMissionStageRewardTr(${idx1}, ${idx2}, ${idx3})" >添加</botton>
    //                 <botton type="button" class="btn btn-danger  delDreamMissionStageRewardTr"  onclick="delDreamMissionStageRewardTr(${idx1}, ${idx2}, ${idx3})" >删除</botton>
    //             </div>`;
    html += ` </div>`

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

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

    let taskOptions = '';
    $.each(dreamMissionTaskMap, function(i, t){
        taskOptions += `<option value="${i}">${t}</option>`
    })
    let html = `
<tr class="commonStageRewardTr">
    <td><input type="text" name="dreamMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][stage]" value="${idx4 + 1}" readonly></td>
    <td><select class=""  name="dreamMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][taskId]" >${taskOptions}</select></td>
    <td><input type="text" name="dreamMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][tasks][${idx4}][maxNum]" value="${idx4}" ></td>
<!--    <td><input type="text" name="dreamMissionConfig[${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="dreamMissionConfig[${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>
`;
    $(`#dreamMissionStageTaskWrap_${key4}`).append(html)
}
function delDreamMissionStageTaskRewardTr(idx1, idx2, idx3){
    let key4 = `${idx1}_${idx2}_${idx3}`;
    let idx4 = dreamMissionStagesTaskMap[key4];

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

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

    let html = `
    <tr class="commonStageRewardTr">
        <td><input type="text" name="dreamMissionConfig[${idx1}][stages][${idx2}][stages][${idx3}][stages][stages][${idx4}][stage]" value="${idx4 + 1}" readonly></td>
        <td><input type="text" name="dreamMissionConfig[${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="dreamMissionConfig[${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>
`;
    $(`#dreamMissionStageWrap_${key4}`).append(html)
}

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

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


function asyncLoadDreamMissionConfig(lvIdx, spinsIdx){
    showLoading()
    var btnsHtml = `
                <div class="opBtns ">
                    <botton type="button" class="btn btn-primary addDreamMissionShushuWrap"  onclick="addDreamMissionShushuWrap(${lvIdx}, ${spinsIdx})" >添加数数区间</botton>
                    <botton type="button" class="btn btn-danger  delDreamMissionShushuWrap"  onclick="delDreamMissionShushuWrap(${lvIdx}, ${spinsIdx})" >删除数数区间</botton>
                </div>` ;
    $(`#dreamMissionSpinsWrap_${lvIdx}_${spinsIdx}`).find('.asyncLoadConfigBtn').remove()
    $(`#dreamMissionSpinsWrap_${lvIdx}_${spinsIdx}`).append(btnsHtml)
    reward_conf[lvIdx]['stages'][spinsIdx]['stages'] && $.each(reward_conf[lvIdx]['stages'][spinsIdx]['stages'],  function(ssIdx, item3){
        addDreamMissionShushuWrap(lvIdx, spinsIdx);
        $(`select[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][shushu][]"]`).val(item3.shushu);
        if(item3.stages && item3.stages.sumSpins){
            $(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][sumSpins]"]`).val(item3.stages.sumSpins);
        }
        if (item3.stages && item3.stages.price){
            $(`select[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][price]"]`).val(item3.stages.price);
            $(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][sku]"]`).val(item3.stages.sku);
        }
        item3.stages && item3.stages.tasks && $.each(item3.stages.tasks,  function(taskIdx, item){
            addDreamMissionStageTaskRewardTr(lvIdx, spinsIdx,ssIdx);
            $(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][stage]"]`).val(item.stage);
            $(`select[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][taskId]"]`).val(item.taskId);
            $(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][maxNum]"]`).val(item.maxNum);
            //$(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][addRate]"]`).val(item.addRate);
            $(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][tasks][${taskIdx}][prize]"]`).val(escape(JSON.stringify(item.prize)));
            $(`.dreamMissionStageTaskWrap_${lvIdx}_${spinsIdx}_${ssIdx}`).find(`.prizeShow:last`).html(prizeListHtml(item.prize))
        })
        // item3.stages && item3.stages.stages && $.each(item3.stages.stages,  function(stageId, item){
        //     addDreamMissionStageRewardTr(lvIdx, spinsIdx,ssIdx);
        //     $(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][stages][${stageId}][stage]"]`).val(item.stage);
        //     $(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][stages][${stageId}][rate]"]`).val(item.rate);
        //     $(`input[name="dreamMissionConfig[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stages][stages][${stageId}][prize]"]`).val(escape(JSON.stringify(item.prize)));
        //     $(`.dreamMissionStageWrap_${lvIdx}_${spinsIdx}_${ssIdx}`).find(`.prizeShow:last`).html(prizeListHtml(item.prize))
        // })
    })

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

}







