let starlightTreasureStageCnt = 0
let magicNum = 0
skinChangedCallbacks.push(()=>{
    magicNum = getMagicNum()
    let magicPatterTypeHtml = getMagicPatterTypeOptions(magicNum)
    $('.patternType').html(magicPatterTypeHtml)
    $('.magicPatternTarget').val(magicNum)
})
function initRewardStarlightTreasure(type) {
    console.log('init ' + type)
    getActBasicConfig(type).then(()=>{
        initStarlightTreasureWrap()
        magicNum = getMagicNum()
        $(`input[name='starlightConfExtra[collect_base_num]']`).val(reward_conf['collect_base_num'])
        // 奖励回写
        $.each(activity_extra_data, function (i, o) {
            addStarlightTreasureStages()
            $(`input[name="starlightTreasureConf[${i}][lv_start]"]`).val(o['lv_start'])
            $(`input[name="starlightTreasureConf[${i}][lv_end]"]`).val(o['lv_end'])
            $(`input[name="starlightTreasureConf[${i}][spins_start]"]`).val(o['spins_start'])
            $(`input[name="starlightTreasureConf[${i}][spins_end]"]`).val(o['spins_end'])
            $('select[name="starlightTreasureConf[' + i + '][shushu][]"]').val(o['shushu'])
            console.log(o);
            o['tokenCollectConfig'] && $.each(o['tokenCollectConfig'], function (i1, o1) {
                addCommonTokenCollectTr(`starlightTreasureTokenWrap${i}`, `starlightTreasureConf[${i}][tokenCollectConfig]`)
                $(`input[name="starlightTreasureConf[${i}][tokenCollectConfig][${i1}][target]"]`).val(o1.target)
                $(`input[name="starlightTreasureConf[${i}][tokenCollectConfig][${i1}][value]"]`).val(o1.value)
            })

            o['starlightConfig'] && $.each(o['starlightConfig'], function (i1, o1) {
                addStarlightTreasureLevelTr(i)
                $(`input[name="starlightTreasureConf[${i}][starlightConfig][${i1}][level]"]`).val(o1.level)
                $(`input[name="starlightTreasureConf[${i}][starlightConfig][${i1}][target]"]`).val(o1.target)
                $(`select[name="starlightTreasureConf[${i}][starlightConfig][${i1}][patternType]"]`).val(o1.patternType)
                let prizeInput = $(`input[name="starlightTreasureConf[${i}][starlightConfig][${i1}][prize]"]`)
                prizeInput.val(encodeURI(JSON.stringify(o1['prize'])))
                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o1['prize']))
            })

            o['tokenStageConfig'] && $.each(o['tokenStageConfig'], function (i1, o1) {
                addCommonPrizeCollectTr(`starlightTreasureStarCollectWrap${i}`, `starlightTreasureConf[${i}][tokenStageConfig]`)
                $(`input[name="starlightTreasureConf[${i}][tokenStageConfig][${i1}][target]"]`).val(o1.target)
                let prizeInput = $(`input[name="starlightTreasureConf[${i}][tokenStageConfig][${i1}][prize]"]`)
                prizeInput.val(encodeURI(JSON.stringify(o1['prize'])))
                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o1['prize']))
            })


            if (o['magicPatternConfig']){
                let prizeInput = $(`input[name="starlightTreasureConf[${i}][magicPatternConfig][prize]"]`)
                prizeInput.val(encodeURI(JSON.stringify(o['magicPatternConfig']['prize'])))
                prizeInput.parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(o['magicPatternConfig']['prize']))
            }


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

function changeTypeToStarlightTreasure(type) {
    getActBasicConfig(type).then(()=>{
        initStarlightTreasureWrap()
    })

}

function initStarlightTreasureWrap() {
    starlightTreasureStageCnt = 0
    $('#reward_info').empty()
    let html = `
        <h1 style="color: red">*配置时请先选择配置了魔法阵数量的皮肤</h1>
        <h3 style="color: red">配置方式：菜单-活动图片资源目录配置- 目标皮肤资源-额外配置-添加新配置- key:'magicNum' val:X(魔法阵数量)</h3>
        <div class="form-inline">
            <label>收集物基础值：</label>
            <input class="form-control" name="starlightConfExtra[collect_base_num]" value="0" >
        </div>
        <div class="form-group text-center " id="act-stage-button">
            <div class=" ">
                <button class="btn btn-primary" id="addStarlightTreasureStageWrapBtn" onclick="addStarlightTreasureStages()" type="button">添加分层区间</button>
                <button class="btn btn-white"   id="delStarlightTreasureStageWrapBtn" onclick="delStarlightTreasureStages()" type="button">删除分层区间</button>
            </div>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
    $("#reward_info").append(html);
}

function addStarlightTreasureStages () {
    let theadHtml = ''
    let i = 0
    while(i < 3){
        i++
        theadHtml += `
            <td width="200">任务类型</td>
            <td>任务值</td>
            <td>积分</td>
        `
    }
    let stageConditionTbl = getLvSpinsShushuStageWrapTable('starlightTreasureConf', starlightTreasureStageCnt)
    let prizeOprHtml = getCommonPrizeOprHtml(`starlightTreasureConf[${starlightTreasureStageCnt}][magicPatternConfig][prize]`)
    let html = `
        <div class="form-group starlightTreasureStageWrap " id="starlightTreasureLvWrap_${starlightTreasureStageCnt}" data-idx="${starlightTreasureStageCnt}" style="margin:10px;padding:10px;border:solid 1px lightgray;">
            ${stageConditionTbl}
            
            <hr style="border:solid 1px black;">
            <h3>活动代币收集配置</h3>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>阶段</td>
                        <td>目标</td>
                        <td>代币数</td>
                    </tr>
                </thead>
                <tbody id="starlightTreasureTokenWrap${starlightTreasureStageCnt}"> </tbody>
                <tr>
                    <td colspan="13">
                        <div class="text-center">
                            <botton class="btn btn-primary addSurpriseCollectRewardTrBtn" onclick="addCommonTokenCollectTr('starlightTreasureTokenWrap${starlightTreasureStageCnt}', 'starlightTreasureConf[${starlightTreasureStageCnt}][tokenCollectConfig]')">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delCommonTr('starlightTreasureTokenWrap${starlightTreasureStageCnt}')">删除</botton> 
                        </div>
                    </td>
                </tr>
            
            </table>
            
            <hr style="border:solid 1px black;">
            <h3>关卡配置</h3>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>关卡</td>
                        <td>星星数</td>
                        <td>魔法阵图案</td>
                        <td>奖励</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tbody id="starlightTreasureLevelWrap${starlightTreasureStageCnt}"> </tbody>
                <tr>
                    <td colspan="5">
                        <div class="text-center">
                            <botton class="btn btn-primary addSurpriseCollectRewardTrBtn" onclick="addStarlightTreasureLevelTr(${starlightTreasureStageCnt})">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delCommonTr('starlightTreasureLevelWrap${starlightTreasureStageCnt}')">删除</botton> 
                        </div>
                    </td>
                </tr>
            </table>
            
            <hr style="border:solid 1px black;">
            <h3>星星收集进度条配置</h3>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>阶段</td>
                        <td>目标</td>
                        <td>奖励</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tbody id="starlightTreasureStarCollectWrap${starlightTreasureStageCnt}"> </tbody>
                <tr>
                    <td colspan="5">
                        <div class="text-center">
                            <botton class="btn btn-primary addSurpriseCollectRewardTrBtn" onclick="addCommonPrizeCollectTr('starlightTreasureStarCollectWrap${starlightTreasureStageCnt}', 'starlightTreasureConf[${starlightTreasureStageCnt}][tokenStageConfig]')">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delCommonTr('starlightTreasureStarCollectWrap${starlightTreasureStageCnt}')">删除</botton> 
                        </div>
                    </td>
                </tr>
            </table>
            
            <hr style="border:solid 1px black;">
            <h3>魔法阵收集进度条配置</h3>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>魔法阵数量</td>
                        <td>奖励</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tr class="commonStageRewardTr">
                    <td><input class="form-control magicPatternTarget" name="starlightTreasureConf[${starlightTreasureStageCnt}][magicPatternConfig][target]" value="${magicNum}" readonly></td>
                   ${prizeOprHtml}
                </tr>
            
            </table>
        </div>
`

    starlightTreasureStageCnt++
    $('#act-stage-button').before(html)
    $('.select2').select2({})
}
function delStarlightTreasureStages(){
    if ($('.starlightTreasureStageWrap').length > 0){
        $('.starlightTreasureStageWrap:last').remove()
        starlightTreasureStageCnt--
    }
}

function getMagicNum (){
    console.log(currentSkinConf);
    if (!currentSkinConf.extraConf)return 0;

    if (!currentSkinConf.extraConf.magicNum)return 0;

    let magicNum = parseInt(currentSkinConf.extraConf.magicNum.val)
    if (isNaN(magicNum))return 0;

    return magicNum
}
function getMagicPatterTypeOptions (magicNum){
    let i = 0
    let optionHtml = ''
    while(i < magicNum){
        optionHtml += `<option value="${i+1}">${i+1}</option>`
        i++
    }
    return optionHtml
}
function addStarlightTreasureLevelTr (stageId){
    let patternTypeOptions = getMagicPatterTypeOptions(magicNum)
    let namePrefix = `starlightTreasureConf[${stageId}]`
    let tbodyObj = $(`#starlightTreasureLevelWrap${stageId}`)
    let id = tbodyObj.find('tr').length
    let prizeOprHtml = getCommonPrizeOprHtml(`${namePrefix}[starlightConfig][${id}][prize]`)
    tbodyObj.append(`
        <tr class="commonStageRewardTr">
            <td><input class="form-control" name="${namePrefix}[starlightConfig][${id}][level]" value="${id+1}" readonly></td>
            <td><input class="form-control" name="${namePrefix}[starlightConfig][${id}][target]" value="0" ></td>
            <td><select name="${namePrefix}[starlightConfig][${id}][patternType]" class="form-control patternType">
            ${patternTypeOptions}
            </select></td>
            ${prizeOprHtml}
        </tr>
    `)
}
