let taskExpressStageCnt = 0
let taskExpressStagesMap = {}
let taskExpressTaskMap = {}
function initRewardTaskExpress(type) {
    console.log('init ' + type)
    getActBasicConfig(type).then(()=>{
        initTaskExpressWrap()

        // 奖励回写
        $.each(reward_conf, function (i, o) {
            addTaskExpressStages()
            $(`input[name="taskExpressConfig[${i}][lv_start]"]`).val(o['lv_start'])
            $(`input[name="taskExpressConfig[${i}][lv_end]"]`).val(o['lv_end'])
            $(`input[name="taskExpressConfig[${i}][spins_start]"]`).val(o['spins_start'])
            $(`input[name="taskExpressConfig[${i}][spins_end]"]`).val(o['spins_end'])
            $(`select[name="taskExpressConfig[${i}][shushu][]"]`).val(o['shushu'])
            $(`select[name="taskExpressConfig[${i}][price]"]`).val(o['price'])
            $.each(o.tasks, function (i1, o1) {
                addTaskExpressTaskTr(i)
                $(`input[name="taskExpressConfig[${i}][tasks][${i1}][day]"]`).val(o1['day'])
                $(`input[name="taskExpressConfig[${i}][tasks][${i1}][id]"]`).val(o1['id'])
                for(let taskId of [1,2,3]){
                    $(`select[name="taskExpressConfig[${i}][tasks][${i1}][task${taskId}][task_type]"]`).val(o1[`task${taskId}`].task_type)
                    $(`input[name="taskExpressConfig[${i}][tasks][${i1}][task${taskId}][target]"]`).val(o1[`task${taskId}`].target)
                    $(`input[name="taskExpressConfig[${i}][tasks][${i1}][task${taskId}][score]"]`).val(o1[`task${taskId}`].score)

                }
            })
            $.each(o.rewards, function (i1, o1) {
                addTaskExpressRewardTr(i)
                let freePrizeInput = $(`input[name="taskExpressConfig[${i}][rewards][${i1}][free_prize]"]`)
                let paymentPrizeInput = $(`input[name="taskExpressConfig[${i}][rewards][${i1}][payment_prize]"]`)
                $(`input[name="taskExpressConfig[${i}][rewards][${i1}][score]"]`).val(o1['score'])
                freePrizeInput.val(encodeURI(JSON.stringify(o1['free_prize'])))
                paymentPrizeInput.val(encodeURI(JSON.stringify(o1['payment_prize'])))
                freePrizeInput.parents('.commonStageRewardTr').find('.free').html(prizeListHtml(o1['free_prize']))
                freePrizeInput.parents('.commonStageRewardTr').find('.payment').html(prizeListHtml(o1['payment_prize']))
            })
        })
        $('.select2').select2({})
    })
}

function changeTypeToTaskExpress(type) {
    getActBasicConfig(type).then(()=>{
        initTaskExpressWrap()
    })

}

function initTaskExpressWrap() {
    taskExpressStageCnt = 0
    taskExpressStagesMap = {}
    taskExpressTaskMap = {}
    $('#reward_info').empty()
    let html = `
        <div class="form-group text-center " id="act-stage-button">
            <div class=" ">
                <button class="btn btn-primary" id="addTaskExpressStageWrapBtn" onclick="addTaskExpressStages()" type="button">添加分层区间</button>
                <button class="btn btn-white"   id="delTaskExpressStageWrapBtn" onclick="delTaskExpressStages()" type="button">删除分层区间</button>
            </div>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
    $("#reward_info").append(html);
}

function addTaskExpressStages() {
    taskExpressStagesMap[taskExpressStageCnt] = 0
    taskExpressTaskMap[taskExpressStageCnt] = 0
    let theadHtml = ''
    let i = 0
    while(i < 3){
        i++
        theadHtml += `
            <td width="200">任务类型</td>
            <td>任务值</td>
            <td>积分</td>
        `
    }
    let priceOptHtml = priceSelectorHtml(`taskExpressConfig[${taskExpressStageCnt}][price]`)
    let stageConditionTbl = getLvSpinsShushuStageWrapTable('taskExpressConfig', taskExpressStageCnt)
    let html = `
        <div class="form-group taskExpressStageWrap " id="taskExpressLvWrap_${taskExpressStageCnt}" data-idx="${taskExpressStageCnt}" style="margin:10px;padding:10px;border:solid 1px lightgray;">
            ${stageConditionTbl}
               
            <hr style="border:solid 1px black;">
            <h3>激活价格</h3>
            ${priceOptHtml}
            
            <hr style="border:solid 1px black;">
            <h3>任务配置</h3>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>-</td>
                        <td>-</td>
                        <td colspan="3" style="text-align: center;background-color: #FFFFFF">任务1</td>
                        <td colspan="3" style="text-align: center;background-color: #EEEEEE">任务2</td>
                        <td colspan="3" style="text-align: center;background-color: #FFFFFF">任务3</td>
                    </tr>
                    <tr>
                        <td>所在天</td>
                        <td>任务ID</td>
                        ${theadHtml}
                    </tr>   
                </thead>
                <tbody id="taskExpressTaskWrap${taskExpressStageCnt}"> </tbody>
                <tr>
                    <td colspan="13">
                        <div class="text-center">
                            <botton class="btn btn-primary addSurpriseCollectRewardTrBtn" onclick="addTaskExpressTaskTr(${taskExpressStageCnt})">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delTaskExpressTaskTr(${taskExpressStageCnt})">删除</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="taskExpressRewardsWrap${taskExpressStageCnt}"> </tbody>
                
                <tr>
                    <td colspan="5">
                        <div class="text-center">
                            <botton class="btn btn-primary addSurpriseCollectRewardTrBtn" onclick="addTaskExpressRewardTr(${taskExpressStageCnt})">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delTaskExpressRewardTr(${taskExpressStageCnt})">删除</botton> 
                        </div>
                    </td>
                </tr>
            
            </table>
        </div>
`

    taskExpressStageCnt++
    $('#act-stage-button').before(html)
    $('.select2').select2({})
}
delTaskExpressStages = ()=>{
    if ($('.taskExpressStageWrap').length > 0){
        $('.taskExpressStageWrap:last').remove()
        taskExpressStageCnt--
    }
}
// 任务配置
addTaskExpressTaskTr = (stageIdx)=>{
    let taskIdx = taskExpressTaskMap[stageIdx]++
    let optionsHtml = getOptionsHtml(actBasicConf[act_type]['taskMap'])
    let trHtml = `
        <tr>
            <td><input class="form-control" name="taskExpressConfig[${stageIdx}][tasks][${taskIdx}][day]"></td>
            <td><input class="form-control" name="taskExpressConfig[${stageIdx}][tasks][${taskIdx}][id]"></td>

`
    let i = 0
    while(i < 3){
        i++
        trHtml += `
            <td>
                <select class="form-control" name="taskExpressConfig[${stageIdx}][tasks][${taskIdx}][task${i}][task_type]">
                    ${optionsHtml}    
                </select>
            </td>
            <td><input class="form-control" name="taskExpressConfig[${stageIdx}][tasks][${taskIdx}][task${i}][target]"></td>
            <td><input class="form-control" name="taskExpressConfig[${stageIdx}][tasks][${taskIdx}][task${i}][score]"></td>
        `
    }
    trHtml += '</tr>'
    $(`#taskExpressTaskWrap${stageIdx}`).append(trHtml)
}
delTaskExpressTaskTr = (stageIdx)=>{
    let taskWrap = $(`#taskExpressTaskWrap${stageIdx}`)
    if (taskWrap.find('tr').length >0){
        taskWrap.find('tr:last').remove()
        taskExpressTaskMap[stageIdx]--
    }
}


// 奖励配置
addTaskExpressRewardTr = (stageIdx) => {
    let rewardIdx = taskExpressStagesMap[stageIdx]++
    let rewardDft = encodeURI( '[]');
    let trHtml = `
            <tr class="commonStageRewardTr">
                <td><input class="form-control" name="taskExpressConfig[${stageIdx}][rewards][${rewardIdx}][lv]" value="${rewardIdx+1}" readonly></td>
                <td><input class="form-control" name="taskExpressConfig[${stageIdx}][rewards][${rewardIdx}][score]"></td>
                <td class="free"></td>
                <td class="payment"></td>
                <td>
                    <input type="hidden" class="freeInput" name="taskExpressConfig[${stageIdx}][rewards][${rewardIdx}][free_prize]" value="${rewardDft}">
                    <input type="hidden" class="paymentInput" name="taskExpressConfig[${stageIdx}][rewards][${rewardIdx}][payment_prize]" value="${rewardDft}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize2',1,'free')">免费奖励配置</button>
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize2',1,'payment')">令牌奖励配置</button>
                </td>
            </tr>
        `
    $(`#taskExpressRewardsWrap${stageIdx}`).append(trHtml)
}


delTaskExpressRewardTr = (stageIdx) => {
    let rewardWrap = $(`#taskExpressRewardsWrap${stageIdx}`)
    if (rewardWrap.find('tr').length >0){
        rewardWrap.find('tr:last').remove()
        taskExpressStagesMap[stageIdx]--
    }

}