let plusN = 1 // 默认1+1
let gtype = 0 // 默认1+1
let gidBegin = 0 // 默认1+1
let OnePlusLvCnt = 0;
let onePlusShushuStages = {}
let OnePlusRewardStages = {}
function changeTypeTo_OnePlus(type){
    console.log('changeToOnePlus');
    getActBasicConfig(type).then(( )=>{
        initOnePlusWrap(type);
    })
}
function initReward_OnePlus(type){

    console.log('initReward_OnePlus-reward_info',reward_info);
    getActBasicConfig(type).then((  )=>{
        initOnePlusWrap(type);
        // 最大购买次数回写
        if(reward_conf && reward_conf.maxNum){
            $('input[name=maxNum]').val(reward_conf.maxNum)
        }
        var reward_infoNew = reward_info
        // 奖励回写

        console.log('1+'+plusN+':reward_info',reward_infoNew);
        if(reward_infoNew){
            $.each(reward_infoNew, function (i, o) {
                $('#addOnePlusLvWrapBtn').click();
                $('input[name="onePlusConfig['+i+'][start]"]').val(o.start);
                $('input[name="onePlusConfig['+i+'][end]"]').val(o.end);
                $.each(o.stages, function(i1, o1){
                    $('.addOnePlusShushuWrapBtn:last').click();
                    console.log( o);
                    $('select[name="onePlusConfig['+i+'][stages]['+i1+'][shushu][]"]').val(o1.shushu);
                    $.each(o1.stages, function(i2, o2){
                        $('select[name="onePlusConfig['+i+'][stages]['+i1+'][stages]['+i2+'][gprice]"]').val(o2.gprice);
                        $('input[name="onePlusConfig['+i+'][stages]['+i1+'][stages]['+i2+'][gsku]"]').val(o2.gsku);
                        $('input[name="onePlusConfig['+i+'][stages]['+i1+'][stages]['+i2+'][gdiscount]"]').val(1);
                        $('input[name="onePlusConfig['+i+'][stages]['+i1+'][stages]['+i2+'][gid]"]').val(o2.gid);
                        $('input[name="onePlusConfig['+i+'][stages]['+i1+'][stages]['+i2+'][prizes]"]').val(escape(JSON.stringify(o2.prizes)));
                        $('input[name="onePlusConfig['+i+'][stages]['+i1+'][stages]['+i2+'][gid]"]').next('.prizeShow').html(prizeListHtml(o2.prizes))
                    });
                    console.log($('select[name="onePlusConfig[' + i + '][stages][' + i1 + '][shushu][]"]').val());
                });
            })
        }
        console.log('select2');
        $('.select2').select2({})
    })
}

// 初始化1+n礼包
function initOnePlusWrap(type){
    console.warn("typeof actBasicConf:",typeof actBasicConf);
    switch (type){
        case 'one_plus_gift':
            plusN = 1;
            gtype = actBasicConf[type].gtype
            break;
        case 'one_plus_two':
            plusN = 2;
            gtype = actBasicConf[type].gtype
            break;
        case 'one_plus_three':
            plusN = 3;
            gtype = actBasicConf[type].gtype
            break;
        case 'one_plus_six':
            plusN = 6;
            gtype = actBasicConf[type].gtype
            break;
        default:
            alert("未知活动："+type)
            return;
    }
    gidBegin = gtype * gidGtypeCe
    OnePlusLvCnt = 0;
    onePlusShushuStages = {}
    OnePlusRewardStages = {}
    var html = ''
    html +='<div class="form-group">';
    html +='<label class="col-xs-1 control-label">最大购买次数:</label><div class="col-sm-1"><input type="text"  name="maxNum" value="1" class="form-control"></div>';
    html +='</div>';
    html +=onePlusWrapStageTable();

    $("#reward_info").empty();
    $("#reward_info").append(html);
    initLayDate();
}
function onePlusWrapStageTable(){
    var html ='';
    html+='<hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    html +='<div class="" style="top:10px">';
    var str ='分层级配置';
    html +='<div class="ibox-title" style="border-top: 0;"><h5 style="font-weight: bold">'+str+'</h5></div>';

    html+='</div>';
    html+='<div class="form-group"><div class=" ">';
    html+='<button class="btn btn-primary" id="addOnePlusLvWrapBtn" onclick="addOnePlusLvWrap(this)" type="button">添加等级层级</button>';
    html+='<button class="btn btn-white"   id="delOnePlusLvWrapBtn" onclick="delOnePlusLvWrap()" type="button">删除等级层级</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    return html;
}

function addOnePlusLvWrap(obj){
    OnePlusLvCnt++
    onePlusShushuStages[OnePlusLvCnt] = 0
    var html = ''
        +       '<div class="form-group onePlusLvWrap " id="OnePlusLvWrap_'+OnePlusLvCnt+'" data-idx="'+OnePlusLvCnt+'" style="margin:10px;padding:10px;border:solid 1px black;">'
        +           '<div class="row">'
        +               '<div class="col-sm-2"><lable class="form-control">等级区间：</lable></div>'
        +               '<div class="col-sm-3">'
        +                  '<input name="onePlusConfig['+OnePlusLvCnt+'][start]" value="0">'
        +               '</div>'
        +               '<div class="col-sm-2"><lable class="form-control" style="text-align: center">-</lable></div>'
        +               '<div class="col-sm-3">'
        +                  '<input name="onePlusConfig['+OnePlusLvCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           `<botton class="btn btn-primary  addOnePlusShushuWrapBtn" onclick="addOnePlusShushuWrap( this)">添加数数分组层级</botton>`
        +           '<botton class="btn btn-danger del-tr delOnePlusShushuWrapBtn" onclick="delOnePlusShushuWrap( this,1)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    $('.select2').select2({})
}
function delOnePlusLvWrap(){
    if(OnePlusLvCnt < 1){
        return;
    }
    $('#OnePlusLvWrap_'+OnePlusLvCnt).remove();
    OnePlusLvCnt--;
}

function addOnePlusShushuWrap(obj){
    let lvIdx = $(obj).parents('.onePlusLvWrap').data('idx')
    let ssIdx = ++onePlusShushuStages[lvIdx]

    let rewardKey = `${lvIdx}_${ssIdx}`;//lvIdx + '_' + ssIdx
    OnePlusRewardStages[rewardKey] = 0
    let ssOptions = getShushuSelectorOptions()
    let html = '';
    html += `<table class="table table-bordered onePlusShushuWrap" id="OnePlusShushuWrap_${rewardKey}" data-idx="'+ ssIdx + '">
                <thead>
                    <tr>
                        <td colspan="5">
                            <div class="form-group"><label class="col-sm-1 control-label m-b">数数分组 </label><div class="col-sm-10 input-group">
                            <select class=" select2" name="onePlusConfig[${lvIdx}][stages][${ssIdx}][shushu][]"  multiple  style="min-width: 100%">
                                ${ssOptions}
                            </select>
                            </div></div>
                        </td>
                    </tr>
                    <tr>
                        <th>价格</th>
                        <th>SKU</th>
                        <th>奖品</th>
                        <th>奖励设置</th>
                    </tr>
                </thead>
                <tbody id="OnePlusRewardBody_${rewardKey}">
                </tbody>
            </table>`;
    $(obj).before(html)
    let i = 0
    while(i <= plusN){
        addOnePlusRewardTr(lvIdx, ssIdx, i)
        i++
    }
    $('.select2').select2({})
}
function delOnePlusShushuWrap(obj){
    var lvIdx = $(obj).parents('.onePlusLvWrap').data('idx')
    // console.log(lvIdx)
    if(onePlusShushuStages[lvIdx] < 1)return
    $(obj).parents('.onePlusLvWrap').find('.onePlusShushuWrap:last').remove()
    onePlusShushuStages[lvIdx]--
}

function addOnePlusRewardTr(lvIdx, ssIdx, rIdx){
    let gid = gidBegin + rIdx;
    let rewardKey = `${lvIdx}_${ssIdx}`;//lvIdx + '_' + ssIdx
    let showHtml = '';
    if(rIdx > 0)showHtml = 'style="display:none"';
    let prizes = '[]';prizes = escape(prizes);
    let html ='';
    html+=`<tr  data-idx="${rIdx}" class="commonStageRewardTr priceSkuGroup">
                <td>
                    <select ${showHtml} onchange="priceChanged(this)"   class="form-control"  name="onePlusConfig[${lvIdx}][stages][${ssIdx}][stages][${rIdx}][gprice]" >` ;
                        $.each(goodSku, function (k, v) {
                            html += `<option value="${v["price"]}" >${v["price"]}</option>`;
                        });
    html +=`        </select>
                </td>
                <td>
                    <input type="text"  ${showHtml}  class="form-control skuInput"  name="onePlusConfig[${lvIdx}][stages][${ssIdx}][stages][${rIdx}][gsku]" value="" readonly>
                </td>
                <input type="hidden" class="form-control"  name="onePlusConfig[${lvIdx}][stages][${ssIdx}][stages][${rIdx}][gdiscount]" value="1">
                <input type="hidden" class="form-control"  name="onePlusConfig[${lvIdx}][stages][${ssIdx}][stages][${rIdx}][gid]" value="${gid}">
                <td class="prizeShow"></td>
                <td>
                    <input type="hidden" class="prizeInput" name="onePlusConfig[${lvIdx}][stages][${ssIdx}][stages][${rIdx}][prizes]" value="${prizes}">
                    <button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,\'setCommonPrize\',1)">奖励配置</button>
                </td>
            </tr>`;
    $('#OnePlusRewardBody_'+rewardKey).append(html)
}




