surpriseCard = 'surpriseCard'
function initRewardSurpriseCard() {
    console.log('initRewardSurpriseCard')
    changeTypeToSurpriseCard()
    $(`input[name="surpriseCardBasic[limitConfig]"]`).val(activity_extra_data.limitConfig)
    $(`input[name="surpriseCardBasic[collectConfig][oneHit]"]`).val(activity_extra_data.collectConfig[0])
    $(`input[name="surpriseCardBasic[collectConfig][twoHit]"]`).val(activity_extra_data.collectConfig[1])
    $(`input[name="surpriseCardBasic[collectConfig][threeHit]"]`).val(activity_extra_data.collectConfig[2])

    reward_conf && $.each(reward_conf, function(i, o){
        addSurpriseCardLvWrap()
        $(`input[name="surpriseCard[${i}][lvStart]"]`).val(o.lvStart)
        $(`input[name="surpriseCard[${i}][lvEnd]"]`).val(o.lvEnd)

        o.stages &&  $.each(o.stages, function(i1, o1){
            addSurpriseCardSpinsWrap(i)
            $(`input[name="surpriseCard[${i}][stages][${i1}][spinsStart]"]`).val(o1.spinsStart)
            $(`input[name="surpriseCard[${i}][stages][${i1}][spinsEnd]"]`).val(o1.spinsEnd)

            o1.stages &&  $.each(o1.stages, function(i2, o2){
                console.log(o2);
                addSurpriseCardShushuWrap(i, i1)
                $(`select[name="surpriseCard[${i}][stages][${i1}][stages][${i2}][shushu][]"]`).val(o2.shushu)
                $(`input[name="surpriseCard[${i}][stages][${i1}][stages][${i2}][endStage][target]"]`).val(o2.endStage.target)
                $(`input[name="surpriseCard[${i}][stages][${i1}][stages][${i2}][endStage][cards][0][weight]"]`).val(o2.endStage.cards[0].weight)
                $(`input[name="surpriseCard[${i}][stages][${i1}][stages][${i2}][endStage][cards][0][prize]"]`).val(o2.endStage.cards[0].prize)
                let endPrize = JSON.parse(unescape(o2.endStage.cards[0].prize))
                $(`#surpriseCardEndStageRewardInfo_${i}_${i1}_${i2}`).find('.prizeShow').html(prizeListHtml(endPrize))

                console.log();
                let len = Object.keys(o2.stageConfig).length
                for(let ir = 0; ir < len-1; ir++){
                    let row = o2.stageConfig[ir]
                    addSurpriseCardRewardTr(i, i1, i2)
                    $(`input[name="surpriseCard[${i}][stages][${i1}][stages][${i2}][stageConfig][${ir}][target]"]`).val(row.target)

                    $.each(row.cards, function(ic, oc){
                        console.log(oc);
                        // oc.prize = JSON.parse(unescape(oc.prize))
                        $(`input[name="surpriseCard[${i}][stages][${i1}][stages][${i2}][stageConfig][${ir}][cards][${ic}][weight]"]`).val(oc.weight)
                        $(`input[name="surpriseCard[${i}][stages][${i1}][stages][${i2}][stageConfig][${ir}][cards][${ic}][prize]"]`).val(escape(JSON.stringify(oc.prize)))
                        $($(`.surpriseCardRewardTr_${i}_${i1}_${i2}_${ir}`)[ic]).find('.prizeShow').html(prizeListHtml(oc.prize))
                        console.log(row);
                    })

                }
            })

        })
    })
}

function changeTypeToSurpriseCard() {
    console.log('changeTypeToSurpriseCard')
    $("#reward_info").empty();

    surpriseCardLvCnt = 0;
    surpriseCardSpinsStages = {}
    surpriseCardShushuStages = {}
    surpriseCardRewardStages = {}
    let html = ``

    html += surpriseCardBasicConf();
    html += surpriseCardWrapStageTable();

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

}
function surpriseCardBasicConf(){
    let html='';
    html +='<div class="form-group">';
    html +=`<label class="col-xs-1 control-label">门槛积分:</label><div class="col-sm-1"><input type="text" name="${surpriseCard}Basic[limitConfig]"   value="0" class="form-control"></div>`;
    html +=`<label class="col-xs-1 control-label">一连击:</label><div class="col-sm-1"><input type="text"   name="${surpriseCard}Basic[collectConfig][oneHit]"   value="0" class="form-control"></div>`;
    html +=`<label class="col-xs-1 control-label">二连击:</label><div class="col-sm-1"><input type="text"   name="${surpriseCard}Basic[collectConfig][twoHit]"   value="0" class="form-control"></div>`;
    html +=`<label class="col-xs-1 control-label">三连击:</label><div class="col-sm-1"><input type="text"   name="${surpriseCard}Basic[collectConfig][threeHit]" value="0" class="form-control"></div>`;
    html +='</div>';
    return html;
}

function surpriseCardWrapStageTable() {
    return `
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
        <div class="" id="surpriseCardWrap" style="top:10px">
            <div class="ibox-title" style="border-top: 0;">
                <h5 style="font-weight: bold">分层级配置</h5>
            </div>
        </div>
        <div class="form-group">
            <div class=" ">
                <button class="btn btn-primary" id="addSurpriseCardLvWrapBtn" onclick="addSurpriseCardLvWrap()" type="button">添加等级层级</button>
                <button class="btn btn-white"   id="delSurpriseCardLvWrapBtn" onclick="delSurpriseCardLvWrap()" type="button">删除等级层级</button>
            </div>
      </div>
      <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

// 等级区间
function addSurpriseCardLvWrap() {
    surpriseCardLvCnt++
    surpriseCardSpinsStages[surpriseCardLvCnt] = 0

    let html = `
        <div class="form-group surpriseCardLvWrap " id="surpriseCardLvWrap_${surpriseCardLvCnt}" data-idx="${surpriseCardLvCnt}" 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="surpriseCard[${surpriseCardLvCnt}][lvStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="surpriseCard[${surpriseCardLvCnt}][lvEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addSurpriseCardSpinsWrapBtn" onclick="addSurpriseCardSpinsWrap(${surpriseCardLvCnt})">添加体力层级</botton>
            <botton class="btn btn-danger del-tr delSurpriseCardSpinsWrapBtn" onclick="delSurpriseCardSpinsWrap(${surpriseCardLvCnt})">删除体力层级</botton>
        </div>
`
    $('#surpriseCardWrap').append(html)
    $('.select2').select2({})
}
function delSurpriseCardLvWrap() {
    if (surpriseCardLvCnt < 1)return;
    $('#surpriseCardLvWrap_' + surpriseCardLvCnt).remove();
    surpriseCardLvCnt--;
}

// 体力区间
function addSurpriseCardSpinsWrap(lvIdx) {
    let spinsIdx = ++surpriseCardSpinsStages[lvIdx]
    let ssKey = `${lvIdx}_${spinsIdx}`
    surpriseCardShushuStages[ssKey] = 0

    let html = `
        <div class="form-group surpriseCardSpinsWrap " id="surpriseCardSpinsWrap_${ssKey}"  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="surpriseCard[${lvIdx}][stages][${spinsIdx}][spinsStart]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="text" style="text-align: center" class="form-control" name="surpriseCard[${lvIdx}][stages][${spinsIdx}][spinsEnd]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary addSurpriseCardShushuWrapBtn" onclick="addSurpriseCardShushuWrap(${lvIdx}, ${spinsIdx})">添加数数分组层级</botton>
            <botton class="btn btn-danger del-tr delSurpriseCardShushuWrapBtn" onclick="delSurpriseCardShushuWrap(${lvIdx}, ${spinsIdx})">删除数数分组层级</botton>
        </div>
`
    $(`#surpriseCardLvWrap_${lvIdx}`).find('.addSurpriseCardSpinsWrapBtn').before(html)
    $('.select2').select2({})
}
function delSurpriseCardSpinsWrap(lvIdx) {
    if (surpriseCardSpinsStages[lvIdx] < 1) return
    $(`#surpriseCardLvWrap_${lvIdx}`).find('.surpriseCardSpinsWrap:last').remove()
    surpriseCardSpinsStages[lvIdx]--
}

// 数数区间
function addSurpriseCardShushuWrap(lvIdx, spinsIdx) {
    let ssKey = `${lvIdx}_${spinsIdx}`
    let ssIdx = ++surpriseCardShushuStages[ssKey]

    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}`
    surpriseCardRewardStages[rewardKey] = -1

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

    let prize = '[]';
    prize = escape(prize);
    let html = `
        <div class="surpriseCardShushuWrap" id="surpriseCardShushuWrap_${rewardKey}"   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="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][shushu][]"  multiple  style="min-width: 100%"> 
                                    ${optionsHtml}
                                </select> 
                            </div>
                        </div> 
                    </td>
                </tr> 
                <tr>
                    <th width="120">阶段</th> 
                    <th>阶段值</th> 
                    <th>权重</th> 
                    <th>奖品</th> 
                    <th>奖励设置</th>
                </tr>
            </thead> 
            <tbody id="surpriseCardRewardBody_${rewardKey}"> </tbody>
            <tbody id="surpriseCardRewardBody_${rewardKey}"> 
                <tr  id="surpriseCardEndStageRewardInfo_${rewardKey}"   class="commonStageRewardTr "> 
                    <td rowspan="3">
                        <input type="text" class="form-control" readonly name="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][endStage][stage]" value="最终阶段">
                    </td> 
                    <td rowspan="3">
                        <input type="text" class="form-control"  name="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][endStage][target]" value="0">
                    </td> 
                   <td>
                        <input type="text" class="form-control"  name="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][endStage][cards][0][weight]" value="0">
                    </td> 
                    <td class="prizeShow"></td> 
                    <td>
                        <input type="hidden" class="prizeInput" name="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][endStage][cards][0][prize]" value="${prize}">
                        <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'superCollect',1)">奖励配置</button>
                    </td>
                </tr>
            </tbody>
            <tr>
                <td colspan="5">
                    <div class="text-center">
                        <botton class="btn btn-primary addSurpriseCardRewardTrBtn" onclick="addSurpriseCardRewardTr(${lvIdx}, ${spinsIdx}, ${ssIdx})">添加</botton>
                        <botton class="btn btn-danger del-tr" onclick="delSurpriseCardRewardTr(${lvIdx}, ${spinsIdx}, ${ssIdx})">删除</botton> 
                    </div>
                </td>
            </tr>
        </table>
        </div>`;

    $(`#surpriseCardSpinsWrap_${ssKey}`).find(`.addSurpriseCardShushuWrapBtn`).before(html)
    $('.select2').select2({})
}
function delSurpriseCardShushuWrap(lvIdx, spinsIdx) {
    let ssKey = `${lvIdx}_${spinsIdx}`
    if (surpriseCardShushuStages[ssKey] < 1) return
    $(`#surpriseCardSpinsWrap_${ssKey}`).find('.surpriseCardShushuWrap:last').remove()
    surpriseCardShushuStages[ssKey]--
}

// 行级
function addSurpriseCardRewardTr(lvIdx, spinsIdx, ssIdx) {

    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}`
    let rewardIdx = ++surpriseCardRewardStages[rewardKey]

    let prize = '[]';
    prize = escape(prize);
    let html = ``;
    let i = 0;
    while(i < 3){
        html += `<tr  id="surpriseCardRewardInfo_${rewardKey}" data-idx="${rewardIdx}" class="commonStageRewardTr surpriseCardRewardTr_${rewardKey}_${rewardIdx}"> `
        if(i == 0){
            html +=
                `
                <td rowspan="3">
                    <input type="text" class="form-control" readonly name="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stageConfig][${rewardIdx}][stage]" value="${rewardIdx+1}">
                </td> 
                <td rowspan="3">
                    <input type="text" class="form-control"  name="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stageConfig][${rewardIdx}][target]" value="0">
                </td> 
            `
        }
        html +=
            `
                <td>
                    <input type="text" class="form-control"  name="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stageConfig][${rewardIdx}][cards][${i}][weight]" value="0">
                </td> 
                <td class="prizeShow"></td> 
                <td>
                    <input type="hidden" class="prizeInput" name="surpriseCard[${lvIdx}][stages][${spinsIdx}][stages][${ssIdx}][stageConfig][${rewardIdx}][cards][${i}][prize]" value="${prize}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'superCollect',1)">奖励配置</button>
                </td> 
            `

        html += `</tr> `
        i++
    }

    $(`#surpriseCardRewardBody_${rewardKey}`).append(html)
}
function delSurpriseCardRewardTr(lvIdx, spinsIdx, ssIdx) {
    let rewardKey = `${lvIdx}_${spinsIdx}_${ssIdx}`
    if (surpriseCardRewardStages[rewardKey] < 1) return;
    console.log($(`.surpriseCardRewardTr_${rewardKey}_${surpriseCardRewardStages[rewardKey]}`));
    $(`.surpriseCardRewardTr_${rewardKey}_${surpriseCardRewardStages[rewardKey]}`).remove()
    surpriseCardRewardStages[rewardKey]--
}


