var chooseYouWantShushuArr=[];
var ChooseYouWantSpinsArr=[];
var ChooseYouWantShushuStages=[];
var ChooseYouWantSpinsCnt=0;
function initReward_chooseYouWant() {
    initChooseYouWantWrap();
    
    $('input[name="chooseYouWantBaseConfig[buyCnt]"]').val(extra_reward.buy_times)
    var reward_infoNew =[];
    if(reward_info[0]['start']==undefined){//数据兼容
        var newData =[];
        $.each(reward_info, function(i,o){
            newData[i] = {"shushu":o.shushu,"data":o.data};
        });
        reward_infoNew[0] = {"start":1,"end":300,"data":newData};
    }else{
        reward_infoNew = reward_info;
    }
// console.log("reward_infoNew:",reward_infoNew);
    $.each(reward_infoNew, function(i, o){
        $('.addChooseYouWantSpinsWrapBtn').click();
        $('input[name="chooseYouWantConfig['+i+'][start]"]').val(o.start);
        $('input[name="chooseYouWantConfig['+i+'][end]"]').val(o.end);
        $.each(o.data, function(i1, o1){
            $('#addChooseYouWantShushuWrap'+i).click();
            // console.log("--o1:",o1);
            var container = $('.chooseYouWantSecondWrap:last')
            container.find('.chooseYouWantShushuSelect2').val(o1.shushu);
            container.find('select[name="chooseYouWantConfig['+i+'][data]['+i1+'][data][price]"]').val(o1.data.price)
            container.find('input[name="chooseYouWantConfig['+i+'][data]['+i1+'][data][sku]"]').val(o1.data.sku)
            container.find('input[name="chooseYouWantConfig['+i+'][data]['+i1+'][data][discount]"]').val(o1.data.discount)
                //
            $.each(o1.data.rewards, function(side, prizes){
                $.each(prizes.prizes, function (prizesKey, prize) {
                    var rewardKey = i + '_' + i1;
                    // console.log("prizesKey:"+prizesKey,"rewardKey:"+rewardKey);
                    addChooseYouWantReward(side, rewardKey)
                    var prizesIdx = parseInt(prizesKey+1)
                    var prizeHtml = chestPropsHtml(prize)
                    var prizeArr = [prize]
                    // console.log('.showStagePrize_'+side+'_'+rewardKey+'_'+prizesIdx);
                    // console.log(prizeHtml);
                    $('.showStagePrize_'+side+'_'+rewardKey+'_'+prizesIdx).html(prizeHtml)
                    $('input[name="chooseYouWantRewardConfig['+side+'_'+rewardKey+']['+prizesIdx+'][prize]"]').val(escape(JSON.stringify(prizeArr)))
                })
            })


        });

    });

    $('.select2').select2({})
}

function changeTypeTo_chooseYouWant() {
    initChooseYouWantWrap()
}

function initChooseYouWantWrap(){
    chooseYouWantCnt = 0
    chooseYouWantShushuArr = []
    var html ='';
    html +=''
        +'  <div    id="chooseYouWantWrap">'
        +'  <div class="form-group" >'
        +           '<lable class="col-sm-2" style="text-align: right;">购买次数：</lable>'
        +           '<input type="number" class="col-sm-3"  name="chooseYouWantBaseConfig[buyCnt]">'
        +'</div>'
        // +       '<botton class="btn btn-primary addChooseYouWantShushuWrap" id="addReward" onclick="addChooseYouWantShushuWrap(this)">添加数数分组</botton>'
        // +       '<botton class="btn btn-danger del-tr delChooseYouWantShushuWrap" onclick="delChooseYouWantShushuWrap(this)">删除数数分组</botton>'
        +'  </div>'
    ;
    $('#reward_info').empty()

    html +=ChooseYouWantWrapStageTable();

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

    $('#reward_info').html(html)
}


function addChooseYouWantSpinsWrap(obj){
    // console.log("===addChooseYouWantSpinsWrap===");
    ChooseYouWantShushuStages[ChooseYouWantSpinsCnt] = 0
    var html = ''
        +       '<div class="form-group ChooseYouWantSpinsWrap " id="ChooseYouWantSpinsWrap_'+ChooseYouWantSpinsCnt+'" data-idx="'+ChooseYouWantSpinsCnt+'" 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="chooseYouWantConfig['+ChooseYouWantSpinsCnt+'][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="chooseYouWantConfig['+ChooseYouWantSpinsCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton class="btn btn-primary addChooseYouWantShushuWrap" id="addChooseYouWantShushuWrap'+ChooseYouWantSpinsCnt+'" onclick="addChooseYouWantShushuWrap( this)">添加数数分组层级</botton>'
        +           '<botton class="btn btn-danger del-tr delChooseYouWantShushuWrapBtn" onclick="delChooseYouWantShushuWrap( this)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    ChooseYouWantSpinsCnt++
    $('.select2').select2({})
}
function delChooseYouWantSpinsWrap(obj){
    // var spinsIdx = $(obj).parents('.ChooseYouWantSpinsWrap').data('idx')
    // console.log("ChooseYouWantSpinsCnt:"+ChooseYouWantSpinsCnt)
    if(ChooseYouWantSpinsCnt < 1)return
    ChooseYouWantSpinsCnt--
    $("#ChooseYouWantSpinsWrap_"+ChooseYouWantSpinsCnt).remove();
    // $(obj).parents('.ChooseYouWantSpinsWrapGroup').find('.ChooseYouWantSpinsWrap:last').remove()

}


function addChooseYouWantShushuWrap(obj){
    console.log("==addChooseYouWantShushuWrap==");
    var spinsIdx = $(obj).parents('.ChooseYouWantSpinsWrap').data('idx');

    var rewardKey = spinsIdx + '_' + ChooseYouWantShushuStages[spinsIdx];
    console.log("spinsIdx:"+spinsIdx+"-rewardKey:"+rewardKey);
    chooseYouWantCnt = ChooseYouWantShushuStages[spinsIdx];

    chooseYouWantShushuArr[rewardKey] = {
        'left':0
        ,'right':0
    }
    var html = '';

    // console.log("-----rewardKey----"+rewardKey)
    var html = ''
        +       '<div class="form-group chooseYouWantSecondWrap priceSkuGroup" data-idx="'+chooseYouWantCnt+'" style="margin:10px;padding:10px;border:solid 1px black;">'
        +           '<lable class="col-sm-1">数数分组：</lable>'
        +           '<select class="select2 col-sm-10 chooseYouWantShushuSelect2" name="chooseYouWantConfig['+spinsIdx+'][data]['+chooseYouWantCnt+'][shushu][]" multiple style="min-width:100px;">';
    $.each(shushuGroups, function(i,o){
        html += '<option value="'+i+'">'+o+'</option>'
    });
    html += '</select>'
        +       '<div class="ibox-title" style="border-top: 0;"></div>'
        +           '<lable class="col-sm-1" style="text-align: right;">价格：</lable>'
        +           '<select  class="col-sm-2" name="chooseYouWantConfig['+spinsIdx+'][data]['+chooseYouWantCnt+'][data][price]"  onchange="priceChanged(this)" >';
    html += '<option hidden value="">选择价格</option>'
        $.each(priceSkuMap, function(i,o){
            html += '<option value="'+i+'">'+i+'</option>'
        })
    html += '</select>'
        +           '<lable class="col-sm-1" style="text-align: right;">sku：</lable>'
        +           '<input readonly class="col-sm-3 skuInput"  name="chooseYouWantConfig['+spinsIdx+'][data]['+chooseYouWantCnt+'][data][sku]">'
        +           '<lable class="col-sm-2" style="text-align: right;">折扣：</lable>'
        +           '<input type="number" class="col-sm-3"  name="chooseYouWantConfig['+spinsIdx+'][data]['+chooseYouWantCnt+'][data][discount]">'
        +       '<div class="ibox-title" style="border-top: 0;"></div>'
        +       '<div class="col-sm-6">'
        +           '<table class="table table-bordered chooseYouWantConfigLeft_'+rewardKey+'">'
        +               '<thead>'
        +                   '<tr>'
        +                       '<th>奖品(左边)</th>'
        +                       '<th>奖励配置</th>'
        +                   '</tr>'
        +               '</thead>'
        +               '<tbody class="chooseYouWantConfigTbody">'
        +               '</tbody>'
        +               '<tr>'
        +                   '<td colspan="2">'
        +                       '<div class="text-center">'

        +                       '<botton class="btn btn-primary addChooseYouWantReward " onclick="addChooseYouWantReward(\'left\', \''+rewardKey+'\')">添加</botton>'
        +                       '<botton class="btn btn-danger del-tr" onclick="delChooseYouWantReward(\'left\', \''+rewardKey+'\')">删除</botton>'
        +                       '</div>'
        +                   '</td>'
        +               '</tr>'
        +           '</table>'
        +'      </div>'
        +       '<div class="col-sm-6">'
        +           '<table class="table table-bordered chooseYouWantConfigRight_'+rewardKey+'">'
        +               '<thead>'
        +                   '<tr>'
        +                       '<th>奖品(右边)</th>'
        +                       '<th>奖励配置</th>'
        +                   '</tr>'
        +               '</thead>'
        +               '<tbody class="chooseYouWantConfigTbody">'
        +               '</tbody>'
        +               '<tr>'
        +                   '<td colspan="2">'
        +                       '<div class="text-center">'
        +                       '<botton class="btn btn-primary addChooseYouWantReward " onclick="addChooseYouWantReward(\'right\', \''+rewardKey+'\')">添加</botton>'
        +                       '<botton class="btn btn-danger del-tr" onclick="delChooseYouWantReward(\'right\', \''+rewardKey+'\')">删除</botton>'
        +                       '</div>'
        +                   '</td>'
        +               '</tr>'
        +           '</table>'
        +'      </div>'
        +' </div>'

    $(obj).before(html)
    ChooseYouWantShushuStages[spinsIdx]++;
    $('.select2').select2({})
}

function delChooseYouWantShushuWrap(obj){
    var spinsIdx = $(obj).parents('.ChooseYouWantSpinsWrap').data('idx');
    chooseYouWantCnt = ChooseYouWantShushuStages[spinsIdx];
    if(chooseYouWantCnt <2)return;
    ChooseYouWantShushuStages[spinsIdx]--;
    $(obj).parents('.ChooseYouWantSpinsWrap').find('.chooseYouWantSecondWrap:last').remove()
}

function addChooseYouWantReward(type , idx){
    // console.log("idx:"+idx+"-type:"+type)
    if(chooseYouWantShushuArr[idx][type] == undefined )chooseYouWantShushuArr[idx][type]=0
    chooseYouWantShushuArr[idx][type]++
    var rewardNum = chooseYouWantShushuArr[idx][type]
    var html ='';
    html+='<tr id="rewardInfo_'+type + '_' +idx+'_' +rewardNum+'">';
    html +='<td class="showStagePrize_'+type+'_'+idx+'_'+rewardNum+'"></td>';
    html +='<input type="hidden"  name="chooseYouWantRewardConfig['+type+'_'+idx+']['+rewardNum+'][prize]" value="%5B%5D">';
    html +='<td><button type="button" class="btn btn-outline btn-success" onclick="setPrize(\''+type+'_'+idx+'\',\'chooseYouWant\','+rewardNum+')">奖励配置</button></td>';// data-toggle="modal"
    html +='</tr>';
// console.log("type:"+type+"-idx:"+idx);
    switch (type) {
        case 'left':
            var container = $('.chooseYouWantConfigLeft_' + idx)
            break;
        case 'right':
            var container = $('.chooseYouWantConfigRight_' + idx)
            break;
        default:
            alert('非法操作');
            return;
    }
    container.find('.chooseYouWantConfigTbody').append(html)
}
function delChooseYouWantReward(type , idx){
    switch (type) {
        case 'left':
            var container = $('.chooseYouWantConfigLeft_'+idx)
            break;
        case 'right':
            var container = $('.chooseYouWantConfigRight_'+idx)
            break;
        default:
            alert('非法操作');
            return;
    }
    if(chooseYouWantShushuArr[idx][type] < 1)return;
    chooseYouWantShushuArr[idx][type]--;
    container.find('.chooseYouWantConfigTbody').find('tr:last').remove()
}


function ChooseYouWantWrapStageTable(){
    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="ChooseYouWantSpinsWrapGroup">';
    html+='<button class="btn btn-primary addChooseYouWantSpinsWrapBtn" onclick="addChooseYouWantSpinsWrap(this)" type="button">添加等级层级</button>';
    html+='<button class="btn btn-white delChooseYouWantSpinsWrapBtn"   onclick="delChooseYouWantSpinsWrap()" type="button">删除等级层级</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    return html;
}
