function changeTypeToMultiRoundTimeLimitedCard(){
    console.log('changeToMultiRoundTimeLimitedCards');
    $("#reward_info").empty();
    initMultiRoundTimeLimitedCardsWrap();
}
function initRewardMultiRoundTimeLimitedCard(){
    console.log('initReward_MultiRoundTimeLimitedCards-',activity_extra_data);
    changeTypeToMultiRoundTimeLimitedCard();
    activity_extra_data  && activity_extra_data.endPreview && $('input[name="timeLimitedCards[endPreview]"]').val((activity_extra_data.endPreview/60).toFixed(2))
    activity_extra_data  && activity_extra_data.themeId && function(){
        $('select[name="timeLimitedCards[themeId]"]').val(activity_extra_data.themeId)
        multiRoundTimeLimitedCardThemeChanged(activity_extra_data.themeId)
        $.each(activity_extra_data.cardSetRewardRatio, (cgid, ratio)=>{
            $(`input[name="timeLimitedCards[cardSetRewardRatio][${cgid}]"]`).val(ratio)
        })
    }()

    activity_extra_data.repeatCardReward && function () {
        $('input[name="timeLimitedCards[repeatCardReward]"]').val(escape(JSON.stringify(activity_extra_data.repeatCardReward)))
        $('input[name="timeLimitedCards[repeatCardReward]"]').parents('.commonStageRewardTr').find('.prizeShow').html(prizeListHtml(activity_extra_data.repeatCardReward))
    }()

    // $('input[name="timeLimitedCards[progressRewardRatio]"]').val(activity_extra_data.progressRewardRatio)

    activity_extra_data && activity_extra_data.progress && $.each(activity_extra_data.progress, function(i,o){
        addMultiRoundTimeLimitedCardRewardTr()
        $(`input[name="timeLimitedCards[progress][${i}][stage]"]`).val(o.stage)
        $(`input[name="timeLimitedCards[progress][${i}][target]"]`).val(o.target)
        $(`input[name="timeLimitedCards[progress][${i}][progressRewardRatio]"]`).val(o.progressRewardRatio)
        $(`input[name="timeLimitedCards[progress][${i}][rewards]"]`).val(escape(JSON.stringify(o.rewards)))
        let propHtml = ''
        $.each(o.rewards, function(ri,reward){
            propHtml+=chestPropsHtml(reward)
        })
        console.log(propHtml);
        $('.prizeShow:last').html(propHtml)
    })
}


function initMultiRoundTimeLimitedCardsWrap(){
    console.log(41)
    MultiRoundTimeLimitedCardStageCnt = 0;
    if(typeof timeLimitedCardsThemes == "undefined" ){
        showLoading()
        $.ajax({
            type: "GET", // 数据提交类型
            url: "activity/getTimeLimitedCardsThemes", // 发送地址
            data: {}, //发送数据
            async: false, // 是否异步
            success: function(data) { // 成功回调
                hideLoading()
                data = JSON.parse(data);
                timeLimitedCardsThemes = data.data.themes
                cgidNameMap = data.data.groups
            }
        });
    }
    var html = ''
    html +='<div class="form-group">';
    html +=     '<label class="col-sm-2 control-label">开放主题:</label>'
    html +=     '<div class="col-sm-5">'
    html +=         '<select   name="timeLimitedCards[themeId]"  class="form-control select2" onchange="multiRoundTimeLimitedCardThemeChanged($(this).val())">'
    html += '<option value="0">选择主题</option>'
    $.each(timeLimitedCardsThemes, function(i,o){
        html += `<option id="theme_id_${o.id}" data-cgids="${o.cgids}" value="${o.id}">${o.theme_res}</option>`
    })
    html +=         '</select>'
    html +=     '</div>';
    html +='</div>';

    html += `
    <div class="form-group">
        <label class="col-sm-2 control-label">活动结束预告(小时):</label>
        <div class="col-sm-5">
            <input name="timeLimitedCards[endPreview]" type="number" class="form-control" value="0" >
        </div>
    </div>
    `
    console.log( $("#reward_info"))
    console.log(html)
    $("#reward_info").append(html);

    initTimelimitCardStageWrap();
}

function initTimelimitCardStageWrap(){

    let prize = escape('[]');
    let html = `
        <div class="timelimitCardStageWrap" id="timelimitCardStageWrap"  style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            卡片重复奖励
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th width="140">奖励设置</th>
                    <th>奖励</th>
                </tr>
                </thead>
                <tbody >
                      <tr class="commonStageRewardTr timeLimitedCardsTr" id="timeLimitedCards" data-idx="${MultiRoundTimeLimitedCardStageCnt}">
                        <td>
                            <input type="hidden" class="prizeInput" name="timeLimitedCards[repeatCardReward]" value="${prize}"> 
                            <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
                        </td>
                        <td class="prizeShow"></td>
                    </tr>          
                </tbody>
            </table>
        </div>  
        <div class="timelimitCardStageWrap" id="timelimitCardStageWrap"  style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            每轮卡组集齐奖励增长系数
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th width="200">卡组</th>
                    <th>奖励增长系数</th>
                </tr>
                </thead>
                <tbody id="cardSetRewardRatio">
                              
                </tbody>
            </table>
        </div>
        <div class="timelimitCardStageWrap" id="timelimitCardStageWrap"  style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            进度奖励
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th width="120">进度条id</th>
                    <th width="140">收集物目标数量</th>
                    <th width="140">每轮进度奖励增长系数</th>
                    <th>奖励</th>
                    <th width="140">奖励设置</th>
                </tr>
                </thead>
                <tbody id="timelimitCardTBody">
                               
                </tbody>
                <tr>
                    <td colspan="8">
                        <div class="text-center">
                            <botton class="btn btn-primary addSerialProgressCollectRewardTrBtn" onclick="addMultiRoundTimeLimitedCardRewardTr(this)">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delMultiRoundTimeLimitedCardRewardTr(this)">删除</botton>
                        </div>
                    </td>
                </tr>
            </table>
        </div>  
    `;

    $("#reward_info").append(html);
}

function addMultiRoundTimeLimitedCardRewardTr() {
    MultiRoundTimeLimitedCardStageCnt++
    var prize = escape('[]');
    var html = `
        <tr class="commonStageRewardTr timeLimitedCardsTr" id="timeLimitedCards" data-idx="${MultiRoundTimeLimitedCardStageCnt}">
            <td>
                <input  name="timeLimitedCards[progress][${MultiRoundTimeLimitedCardStageCnt}][stage]" type="text"   class="form-control"  value="${MultiRoundTimeLimitedCardStageCnt}" readonly >
            </td>
            <td>
                <input  name="timeLimitedCards[progress][${MultiRoundTimeLimitedCardStageCnt}][target]" type="text"   class="form-control"  value="0"  >
            </td>
            <td>
                <input  name="timeLimitedCards[progress][${MultiRoundTimeLimitedCardStageCnt}][progressRewardRatio]" type="text"   class="form-control"  value="0"  >
            </td>
        
            <td class="prizeShow"></td>
            <td>
                <input type="hidden" class="prizeInput" name="timeLimitedCards[progress][${MultiRoundTimeLimitedCardStageCnt}][rewards]" value="${prize}"> 
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'setCommonPrize',1)">奖励配置</button>
            </td>
        </tr>
    `;

    $('#timelimitCardTBody').append(html)
}

function delMultiRoundTimeLimitedCardRewardTr() {
    if(MultiRoundTimeLimitedCardStageCnt > 0){
        MultiRoundTimeLimitedCardStageCnt--
        $('.timeLimitedCardsTr:last').remove()
    }
}


function multiRoundTimeLimitedCardThemeChanged(themeId){
    console.log(themeId);
    let groupIds = $(`#theme_id_${themeId}`).data().cgids
    let html = ''
    groupIds = jsonParse(groupIds)
    console.log(groupIds);
    $.each(groupIds, (i, cgid)=>{
        console.log(cgid, cgidNameMap[cgid]);
        html += `
        <tr class="commonStageRewardTr timeLimitedCardsTr" id="timeLimitedCards" data-idx="${MultiRoundTimeLimitedCardStageCnt}">
                <td class="prizeShow">${cgidNameMap[cgid]}-${cgid}</td>
                <td>
                    <input  class="form-control" name="timeLimitedCards[cardSetRewardRatio][${cgid}]" value="0"> 
                </td>
            </tr>  
`
    })
    $('#cardSetRewardRatio').html(html)
}