const adMissionsStageMax = 3;
function initRewardAdMissions(type) {
    console.log('init ' + type)
    changeTypeToAdMissions()

    // 奖励回写
    $.each(reward_conf, function (i, o) {
        $('#addAdMissionsLvWrapBtn').click()
        $('input[name="adMissionsConfig[' + i + '][lvStart]"]').val(o.lvStart)
        $('input[name="adMissionsConfig[' + i + '][lvEnd]"]').val(o.lvEnd)
        $.each(o.stages, function (i1, o1) {
            $('.addAdMissionsSpinsWrapBtn:last').click()
            $('input[name="adMissionsConfig[' + i + '][stages][' + i1 + '][spinsStart]"]').val(o1.spinsStart)
            $('input[name="adMissionsConfig[' + i + '][stages][' + i1 + '][spinsEnd]"]').val(o1.spinsEnd)
            $.each(o1.stages, function (i2, o2) {
                $('.addAdMissionsShushuWrapBtn:last').click()
                $('select[name="adMissionsConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][shushu][]"]').val(o2.shushu)

                $.each(o2.stages, function (i3, o3) {
                    let prizeInput = $('input[name="adMissionsConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][stages][' + i3 + '][prize]"]')
                    $('input[name="adMissionsConfig[' + i + '][stages][' + i1 + '][stages][' + i2 + '][stages][' + i3 + '][dst]"]').val(o3.dst)
                    prizeInput.val(escape(JSON.stringify(o3.prize)))
                    prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o3.prize))
                })
            })
        })
    })
}

function changeTypeToAdMissions() {
    adMissionsLvCnt = 0
    adMissionsSpinsStages = {}
    adMissionsShushuStages = {}
    adMissionsRewardStages = {}

    $('#reward_info').empty()
    var html = adMissionsStageTable();
    $("#reward_info").append(html);
}

function adMissionsStageTable() {
    return `
        <div class="form-group text-center">
            <div class=" ">
                <button class="btn btn-primary" id="addAdMissionsLvWrapBtn" onclick="addAdMissionsLvWrap(this)" type="button">添加等级层级</button>
                <button class="btn btn-white"   id="delAdMissionsLvWrapBtn" onclick="delAdMissionsLvWrap()" type="button">删除等级层级</button>
            </div>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

function addAdMissionsLvWrap(obj) {
    adMissionsLvCnt++
    adMissionsSpinsStages[adMissionsLvCnt] = 0

    var html = `
        <div class="form-group adMissionsLvWrap " id="adMissionsLvWrap_${adMissionsLvCnt}" data-idx="${adMissionsLvCnt}" style="margin:10px;padding:10px;border:solid 1px lightgray;">
            <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="adMissionsConfig[${adMissionsLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="adMissionsConfig[${adMissionsLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addAdMissionsSpinsWrapBtn" onclick="addAdMissionsSpinsWrap(this)">添加体力层级</botton>
            <botton class="btn btn-danger del-tr delAdMissionsSpinsWrapBtn" onclick="delAdMissionsSpinsWrap(this)">删除体力层级</botton>
        </div>
`

    $(obj).before(html)
    $('.select2').select2({})
}

function addAdMissionsSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.adMissionsLvWrap').data('idx')
    var adMissionsSpinsCnt = ++adMissionsSpinsStages[lvIdx]
    var spinsIdx = `${lvIdx}_${adMissionsSpinsCnt}`
    adMissionsShushuStages[spinsIdx] = 0

    var html = `
        <div class="form-group adMissionsSpinsWrap " id="adMissionsSpinsWrap_${spinsIdx}" data-idx="${adMissionsSpinsCnt}" 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="adMissionsConfig[${lvIdx}][stages][${adMissionsSpinsCnt}][spinsStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="adMissionsConfig[${lvIdx}][stages][${adMissionsSpinsCnt}][spinsEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addAdMissionsShushuWrapBtn" onclick="addAdMissionsShushuWrap(this)">添加数数分组层级</botton>
            <botton class="btn btn-danger del-tr delAdMissionsShushuWrapBtn" onclick="delAdMissionsShushuWrap(this)">删除数数分组层级</botton>
        </div>
    `

    $(obj).before(html)
    $('.select2').select2({})
}


function addAdMissionsShushuWrap(obj) {
    var lvIdx = $(obj).parents('.adMissionsLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.adMissionsSpinsWrap').data('idx')

    var adMissionsSSCnt = ++adMissionsShushuStages[`${lvIdx}_${spinsIdx}`]

    var rewardIdx = `${lvIdx}_${spinsIdx}_${adMissionsSSCnt}`
    adMissionsRewardStages[rewardIdx] = 0

    var arr = JSON.parse($hideSSGroup);

    var optionsHtml = ''
    $.each(arr, function (i, o) {
        optionsHtml += '<option value="' + i + '">' + o + '</option>'
    });

    var html = `
        <div class="adMissionsShushuWrap" id="adMissionsShushuWrap_${rewardIdx}" data-idx="${adMissionsSSCnt}" style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            <table class="table table-bordered" > 
            <thead>
                <tr>
                    <td colspan="5"> 
                        <div class="form-group">
                            <label class="col-sm-1 control-label m-b">数数分组 </label>
                            <div class="col-sm-3 input-group"> 
                                <select class=" select2" name="adMissionsConfig[${lvIdx}][stages][${spinsIdx}][stages][${adMissionsSSCnt}][shushu][]"  multiple  style="min-width: 100%"> 
                                    ${optionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th>阶段值</th> 
                    <th>奖品</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="adMissionsRewardBody_${rewardIdx}"> </tbody>
            <tr style="display: none">
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addAdMissionsRewardTrBtn" onclick="addAdMissionsRewardTr(this)">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delAdMissionsRewardTr(this)">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

    $(obj).before(html)
    $('.select2').select2({})
    let i = 0
    while (i < adMissionsStageMax){
        i++
        $(`#adMissionsShushuWrap_${rewardIdx}`).find('.addAdMissionsRewardTrBtn').click()
    }
}

//体力大师添加进度奖行
function addAdMissionsRewardTr(obj) {
    var lvIdx = $(obj).parents('.adMissionsLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.adMissionsSpinsWrap').data('idx')
    var rewardIdx = $(obj).parents('.adMissionsShushuWrap').data('idx')

    var lsrIdx = `${lvIdx}_${spinsIdx}_${rewardIdx}`
    var rIdx = adMissionsRewardStages[lsrIdx]++

    var rewardKey = `${lvIdx}_${spinsIdx}_${rewardIdx}_${rIdx}`

    var prize = '[]';
    var prize_show_arr = JSON.parse(prize);
    prize = escape(prize);
    var html = `
        <tr id="adMissionsRewardInfo_${rewardKey}" data-idx="${rIdx}" class="commonStageRewardTr"> 
            <td>
                <input type="text" class="form-control" readonly name="adMissionsConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][stages][${rIdx}][id]" value="${rIdx+1}">
            </td> 
            <td>
                <input type="text" class="form-control"  name="adMissionsConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][stages][${rIdx}][dst]" value="0">
            </td> 
            <td class="prizeShow"></td> 
            <td>
                <input type="hidden"  class="prizeInput" name="adMissionsConfig[${lvIdx}][stages][${spinsIdx}][stages][${rewardIdx}][stages][${rIdx}][prize]" value="${prize}">
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this, 'setCommonPrize', 1)">奖励配置</button>
            </td> 
        </tr> 
    `;

    $('#adMissionsRewardBody_' + lsrIdx).append(html)
}

function delAdMissionsLvWrap() {
    if (adMissionsLvCnt < 1) {
        return;
    }
    $('#adMissionsLvWrap_' + adMissionsLvCnt).remove();
    adMissionsLvCnt--;
}

function delAdMissionsSpinsWrap(obj) {
    var lvIdx = $(obj).parents('.adMissionsLvWrap').data('idx')

    if (adMissionsSpinsStages[lvIdx] < 1) return
    $(obj).parents('.adMissionsLvWrap').find('.adMissionsSpinsWrap:last').remove()
    adMissionsSpinsStages[lvIdx]--
}

function delAdMissionsShushuWrap(obj) {
    var lvIdx = $(obj).parents('.adMissionsLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.adMissionsSpinsWrap').data('idx')

    if (adMissionsShushuStages[`${lvIdx}_${spinsIdx}`] < 1) return
    $(obj).parents('.adMissionsSpinsWrap').find('.adMissionsShushuWrap:last').remove()
    adMissionsShushuStages[`${lvIdx}_${spinsIdx}`]--
}

function delAdMissionsRewardTr(obj) {
    var lvIdx = $(obj).parents('.adMissionsLvWrap').data('idx')
    var spinsIdx = $(obj).parents('.adMissionsSpinsWrap').data('idx')
    var shushuIdx = $(obj).parents('.adMissionsShushuWrap').data('idx')

    var rewardIdx = `${lvIdx}_${spinsIdx}_${shushuIdx}`
    var rewardKey = rewardIdx + '_' + adMissionsRewardStages[rewardIdx]

    if (adMissionsRewardStages[rewardIdx] < 1) return;
    $('#adMissionsRewardInfo_' + rewardKey).remove()
    adMissionsRewardStages[rewardIdx]--
}