function changeTypeTo_superSerialProgressPack(type) {
    console.log('changeTo ' + type)
    $("#reward_info").empty();
    initSerialProgressWrap();
}

function initReward_superSerialProgressPack(type) {
    console.log('init' + type)
    changeTypeTo_superSerialProgressPack(type)
    // 奖励回写
    console.log('extra_reward', JSON.stringify(extra_reward))
    if (extra_reward) {
        $.each(extra_reward, function (i, o) {
            $('#addSerialProgressLvWrapBtn').click()
            $(`input[name="superSerialProgressConfig[${i}][start]"]`).val(o.start)
            $(`input[name="superSerialProgressConfig[${i}][end]"]`).val(o.end)
            $.each(o.stages, function (i1, o1) {
                $('.addSerialProgressShushuWrapBtn:last').click()
                $('select[name="superSerialProgressConfig['+i+'][stages]['+i1+'][shushu][]"]').val(o1.shushu)

                var stage = `${i}_${i1}`
                $.ajax({
                    type: "GET", // 数据提交类型
                    url: "activity/getActStageReward/" + $asyncId + '/' + stage, // 发送地址
                    data: {}, //发送数据
                    async: false, // 是否异步
                    success: function (data) { // 成功回调
                        console.log(stage, 'finish')
                        data = JSON.parse(data);
                        o1.list = data.data
                    }
                });
                console.log(stage, 'complete')
                $.each(o1.list.stages, function (i2, o2) {
                    console.log(i2, o2)
                    $('.addSerialProgressRewardTrBtn:last').click()
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][stage]"]`).val(o2.stage)
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][name]"]`).val(o2.name)
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][gMax]"]`).val(o2.gMax)
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][collect]"]`).val(o2.collect)
                    $(`select[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][mtype]"]`).val(o2.mtype)
                    $('.priceSkuGroup:last').html(getSerialProgressRewardPayInfoByType(o2.mtype, i, i1, i2))
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][bet]"]`).val(o2.bet)
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][sku]"]`).val(o2.sku)
                    if (o2.mtype == 1) {
                        $(`select[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][coin]"]`).val(o2.coin)
                    } else {
                        $(`select[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][price]"]`).val(o2.price)
                    }
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][stages][${i2}][prize]"]`).val(escape(JSON.stringify(o2.prize)))
                    var prizeHtml = ''
                    $.each(o2.prize, function (i, prize) {
                        prizeHtml += chestPropsHtml(prize)
                    })
                    $('.prizeShow:last').html(prizeHtml)
                })
                $.each(o1.list.progress, function (i2, o2) {
                    console.log(i2, o2)
                    $('.addSerialProgressCollectRewardTrBtn:last').click()
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][progress][${i2}][progressBarId]"]`).val(o2.progressBarId)
                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][progress][${i2}][stepNum]"]`).val(o2.stepNum)

                    $(`input[name="superSerialProgressConfig[${i}][stages][${i1}][list][progress][${i2}][prize]"]`).val(escape(JSON.stringify(o2.prize)))
                    var prizeHtml = ''
                    $.each(o2.prize, function (i, prize) {
                        prizeHtml += chestPropsHtml(prize)
                    })
                    $('.prizeShow:last').html(prizeHtml)
                })
            })
        })
    }
}

function initSerialProgressWrap() {
    serialProgressLvCnt = 0;
    serialProgressShushuStages = {}
    serialProgressRewardStages = {}
    serialProgressCollectRewardStages = {}

    $('#reward_info').empty()

    var html = serialProgressWrapStageTable();
    $("#reward_info").append(html);
}

function serialProgressWrapStageTable() {
    return `
        <div class="form-group text-center">
            <div class=" ">
                <button class="btn btn-primary" id="addSerialProgressLvWrapBtn" onclick="addSerialProgressLvWrap(this)" type="button">添加等级区间</button>
                <button class="btn btn-white"   id="delSerialProgressLvWrapBtn" onclick="delSerialProgressLvWrap()" type="button">删除等级区间</button>
            </div>
        </div>
        <hr style=" height:2px;border:none;border-top:2px dotted #0b0;">
    `;
}

function addSerialProgressLvWrap(obj) {
    serialProgressLvCnt++
    serialProgressShushuStages[serialProgressLvCnt] = 0
    var html = `
        <div class="form-group serialProgressLvWrap " id="serialProgressLvWrap_${serialProgressLvCnt}" data-idx="${serialProgressLvCnt}" 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="number" style="text-align: center" class="form-control" name="superSerialProgressConfig[${serialProgressLvCnt}][start]" value="0">
                    <span class="input-group-addon">-</span>
                    <input type="number" style="text-align: center" class="form-control" name="superSerialProgressConfig[${serialProgressLvCnt}][end]" value="0">
                </div>
            </div>
            <hr style="border:solid 1px black;">
            <botton class="btn btn-primary  addSerialProgressShushuWrapBtn" onclick="addSerialProgressShushuWrap( this)">添加数数分组层级</botton>
            <botton class="btn btn-danger del-tr delSerialProgressShushuWrapBtn" onclick="delSerialProgressShushuWrap( this)">删除数数分组层级</botton>
        </div>
`

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

function delSerialProgressLvWrap() {
    if (serialProgressLvCnt < 1) {
        return;
    }
    $('#serialProgressLvWrap_' + serialProgressLvCnt).remove();
    serialProgressLvCnt--;
}

function addSerialProgressShushuWrap(obj) {
    var lvIdx = $(obj).parents('.serialProgressLvWrap').data('idx')
    var serialProgressSSCnt = ++serialProgressShushuStages[lvIdx]
    var rewardIdx = `${lvIdx}_${serialProgressSSCnt}`
    serialProgressRewardStages[rewardIdx] = 0
    serialProgressCollectRewardStages[rewardIdx] = 0

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

    var html = `
        <div class="serialProgressShushuWrap" id="serialProgressShushuWrap_${rewardIdx}" data-idx="${serialProgressSSCnt}" style="margin:10px;padding:5px;border:solid 1px goldenrod;">
            <table class="table table-bordered">
                <thead>
                    <tr>
                       <td colspan="8"> 
                            <div class="form-group">
                                <label class="col-sm-1 control-label m-b">数数分组 </label>
                                <div class="col-sm-5 input-group"> 
                                    <select class="select2" name="superSerialProgressConfig[${lvIdx}][stages][${serialProgressSSCnt}][shushu][]"  multiple  style="min-width: 100%"> 
                                        ${ssOptionsHtml}
                                    </select> 
                                </div>
                            </div> 
                        </td>
                    </tr>
                    <tr>
                        <th width="120">阶段</th>
                        <th width="140">名称</th>
                        <th width="140">限量</th>
                        <th width="140">收集物个数</th>
                        <th width="140">购买货币</th>
                        <th width="220">价格</th>
                        <th>奖励</th>
                        <th width="140">奖励设置</th>
                    </tr>
                </thead>
                <tbody id="serialProgressRewardBody_${rewardIdx}"></tbody>
                <tr>
                    <td colspan="8">
                        <div class="text-center">
                            <botton class="btn btn-primary addSerialProgressRewardTrBtn" onclick="addSerialProgressRewardTr(this)">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delSerialProgressRewardTr(this)">删除</botton>
                        </div>
                    </td>
                </tr>
            </table>
        
            <hr style="border:solid 1px goldenrod;">

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th width="120">进度条id</th>
                    <th width="140">收集物目标数量</th>
                    <th>奖励</th>
                    <th width="140">奖励设置</th>
                </tr>
                </thead>
                <tbody id="serialProgressCollectRewardBody_${rewardIdx}"></tbody>
                <tr>
                    <td colspan="8">
                        <div class="text-center">
                            <botton class="btn btn-primary addSerialProgressCollectRewardTrBtn" onclick="addSerialProgressCollectRewardTr(this)">添加</botton>
                            <botton class="btn btn-danger del-tr" onclick="delSerialProgressCollectRewardTr(this)">删除</botton>
                        </div>
                    </td>
                </tr>
            </table>
        </div>  
     
    `;

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

function delSerialProgressShushuWrap(obj) {
    var lvIdx = $(obj).parents('.serialProgressLvWrap').data('idx')
    if (serialProgressShushuStages[lvIdx] < 1) return
    $(obj).parents('.serialProgressLvWrap').find('.serialProgressShushuWrap:last').remove()
    serialProgressShushuStages[lvIdx]--
}

function addSerialProgressRewardTr(obj) {
    var lvIdx = $(obj).parents('.serialProgressLvWrap').data('idx')
    var shushuIdx = $(obj).parents('.serialProgressShushuWrap').data('idx')
    var rewardIdx = `${lvIdx}_${shushuIdx}`
    var rIdx = ++serialProgressRewardStages[rewardIdx]

    var rewardKey = `${lvIdx}_${shushuIdx}_${rIdx}`
    var prize = escape('[]');
    var html = `
        <tr class="serialProgressRewardInfoTr" id="serialProgressRewardInfo_${rewardKey}" data-idx="${rIdx}">
            <td>
                <input  name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${rIdx}][stage]" type="text"   class="form-control"  value="${rIdx}" readonly >
            </td>
            <td>
                <input  name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${rIdx}][name]" type="text"   class="form-control" value="serialPack-${rIdx}" readonly>
            </td>
            <td>
                <input  name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${rIdx}][gMax]" type="number"   class="form-control" value="0">
            </td>
            <td>
                <input  name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${rIdx}][collect]" type="number"   class="form-control" value="0">
            </td>
            <td>
                <select name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${rIdx}][mtype]"  onchange="serialProgressPayTypeChange(this)"  class="form-control"  > 
                    <option value="1">金币</option>
                    <option value="2">美元</option> 
                </select>
            </td>
        
            <td class=" priceSkuGroup">${getSerialProgressRewardPayInfoByType(1, lvIdx, shushuIdx, rIdx)}</td>
        
            <td class="prizeShow"></td>
            <td>
                <input type="hidden" class="prizeInput" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${rIdx}][prize]" value="${prize}"> 
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'superSerialProgress',1)">奖励配置</button>
            </td>
        </tr>
    `;

    $('#serialProgressRewardBody_' + rewardIdx).append(html)
}

function delSerialProgressRewardTr(obj) {
    var lvIdx = $(obj).parents('.serialProgressLvWrap').data('idx')
    var shushuIdx = $(obj).parents('.serialProgressShushuWrap').data('idx')
    var rewardIdx = `${lvIdx}_${shushuIdx}`
    var rewardKey = `${lvIdx}_${shushuIdx}_${serialProgressRewardStages[rewardIdx]}`

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

function addSerialProgressCollectRewardTr(obj) {
    var lvIdx = $(obj).parents('.serialProgressLvWrap').data('idx')
    var shushuIdx = $(obj).parents('.serialProgressShushuWrap').data('idx')
    var rewardIdx = `${lvIdx}_${shushuIdx}`
    console.log(rewardIdx)
    var rIdx = ++serialProgressCollectRewardStages[rewardIdx]

    var rewardKey = `${lvIdx}_${shushuIdx}_${rIdx}`
    var prize = escape('[]');
    var html = `
        <tr class="serialProgressCollectRewardInfoTr" id="serialProgressCollectRewardInfo_${rewardKey}" data-idx="${rIdx}">
            <td>
                <input  name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][progress][${rIdx}][progressBarId]" type="text"   class="form-control"  value="${rIdx}" readonly >
            </td>
            <td>
                <input  name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][progress][${rIdx}][stepNum]" type="number"   class="form-control" value="0">
            </td>
        
            <td class="prizeShow"></td>
            <td>
                <input type="hidden" class="prizeInput" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][progress][${rIdx}][prize]" value="${prize}"> 
                <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,'superSerialProgress',1)">奖励配置</button>
            </td>
        </tr>
    `;

    $('#serialProgressCollectRewardBody_' + rewardIdx).append(html)
}

function delSerialProgressCollectRewardTr(obj) {
    var lvIdx = $(obj).parents('.serialProgressLvWrap').data('idx')
    var shushuIdx = $(obj).parents('.serialProgressShushuWrap').data('idx')
    var rewardIdx = `${lvIdx}_${shushuIdx}`
    var rewardKey = `${lvIdx}_${shushuIdx}_${serialProgressCollectRewardStages[rewardIdx]}`

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

function serialProgressPayTypeChange(obj) {
    var payType = $(obj).val();
    var lvIdx = $(obj).parents('.serialProgressLvWrap').data('idx')
    var shushuIdx = $(obj).parents('.serialProgressShushuWrap').data('idx')
    var trIdx = $(obj).parents('.serialProgressRewardInfoTr').data('idx')
    console.log(payType, lvIdx, shushuIdx);
    $(obj).parents('.serialProgressRewardInfoTr').find('.priceSkuGroup').html(getSerialProgressRewardPayInfoByType(payType, lvIdx, shushuIdx, trIdx))
}

function getSerialProgressRewardPayInfoByType(payType, lvIdx, shushuIdx, trIdx) {
    payType = parseInt(payType)
    switch (payType) {
        case 1://金币

            var optionHtml = '';
            for (var i = 0; i < coins_list.length; i++) {
                optionHtml += `<option value="${coins_list[i]}">${coins_list[i]}</option>`;
            }

            return `
                价格:<select class="form-control m-b" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${trIdx}][coin]">
                        ${optionHtml}
                    </select>
                倍数:<input class="form-control" type="text" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${trIdx}][bet]" value="0">
                    <input type="hidden" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${trIdx}][sku]" value="0">
                    <input type="hidden" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${trIdx}][price]" value="0">
            `
        case 2://美元
            var optionHtml = '';
            $.each(goodSku, function (k, v) {
                optionHtml += `<option value="${v['price']}">${v['price']}</option>`;
            });

            return `
                价格:<select class="form-control m-b selPrice" onchange="priceChanged(this)" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${trIdx}][price]">
                        ${optionHtml}
                    </select>
             商品sku:<input class="form-control t_sku skuInput" readonly type="text" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${trIdx}][sku]" value="">
                    <input type="hidden" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${trIdx}][coin]" value="0">
                    <input type="hidden" name="superSerialProgressConfig[${lvIdx}][stages][${shushuIdx}][list][stages][${trIdx}][bet]" value="0">
            `
    }
}
