var isInitReward =0;
function changeTypeTo_ChooseOne(){
    console.log('changeToTheme');
    $("#reward_info").empty();
    initChooseOneWrap();
}
function initReward_ChooseOne(){
    isInitReward =1
    console.log('initReward_ChooseOne-reward_info',reward_info);
    changeTypeTo_ChooseOne();

    // 奖励回写
    console.log('三选一:reward_info',reward_info);
    if(reward_info){
        var i=0;
        $.each(reward_info, function (i, o) {

            $('#addChooseOneLvsWrapBtn').click();
            $('input[name="ChooseOneConfig['+i+'][start]"]').val(o.start);
            $('input[name="ChooseOneConfig['+i+'][end]"]').val(o.end);
            $.each(o.data, function(i1  , o1){
                $('.addChooseOneShushuWrapBtn:last').click();
                $('select[name="ChooseOneConfig['+i+'][data]['+i1+'][shushu][]"]').val(o1.shushu);
                o1.data.length > 3 && $('.addChooseOneRewardTrBtn:last').click();
                $.each(o1.data, function(i2, o2){
                    var rewardIdx = i + '_' + i1
                    console.log(i2,o2);
                    console.log('ChooseOneConfig['+i+'][data]['+i1+'][data]['+i2+']');
                    $('select[name="ChooseOneConfig['+i+'][data]['+i1+'][data]['+i2+'][price]"]').val(o2.price);
                    $('input[name="ChooseOneConfig['+i+'][data]['+i1+'][data]['+i2+'][sku]"]').val(o2.sku);
                    $('input[name="ChooseOneConfig['+i+'][data]['+i1+'][data]['+i2+'][discount]"]').val(o2.discount);
                    // $('input[name="ChooseOneConfig['+i+'][data]['+i1+'][data]['+i2+'][gid]"]').val(o2.gid);
                    $('input[name="ChooseOneConfig['+i+'][data]['+i1+'][data]['+i2+'][prizes]"]').val(escape(JSON.stringify(o2.prizes)));
                    var prizeHtml = '';
                    // console.log("prizes;",o2.prizes)
                    $.each(o2.prizes, function (a,prizes){
                        // console.log(prizes)
                        prizeHtml += chestPropsHtml(prizes)
                    });
                     // console.log("prizeHtml;",prizeHtml)
                    $('#ChooseOneShushuWrap_'+rewardIdx).find('.prizeShowStage_'+i2).html(prizeHtml)
                });
            });
        })
    }
    isInitReward=0;
}

function ChooseOneWrapStageTable(){
    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="addChooseOneLvsWrapBtn" onclick="addChooseOneLvsWrap(this)" type="button">添加等级层级</button>';
    html+='<button class="btn btn-white"   id="delChooseOneLvsWrapBtn" onclick="delChooseOneLvsWrap()" type="button">删除等级层级</button>';
    html+='</div></div><hr style=" height:2px;border:none;border-top:2px dotted #0b0;">';
    return html;
}

function addChooseOneLvsWrap(obj){
    ChooseOneLvsCnt++
    ChooseOneShushuStages[ChooseOneLvsCnt] = 0
    var html = ''
        +       '<div class="form-group ChooseOneLvsWrap " id="ChooseOneLvsWrap_'+ChooseOneLvsCnt+'" data-idx="'+ChooseOneLvsCnt+'" 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="ChooseOneConfig['+ChooseOneLvsCnt+'][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="ChooseOneConfig['+ChooseOneLvsCnt+'][end]" value="0">'
        +               '</div>'
        +           '</div>'
        +           '<botton class="btn btn-primary  addChooseOneShushuWrapBtn" onclick="addChooseOneShushuWrap( this,1)">添加数数分组层级</botton>'
        +           '<botton class="btn btn-danger del-tr delChooseOneShushuWrapBtn" onclick="delChooseOneShushuWrap( this,1)">删除数数分组层级</botton>'
        +       ' </div>'

    $(obj).before(html)
    $('.select2').select2({})
}
function delChooseOneLvsWrap(){
    if(ChooseOneLvsCnt < 1){
        return;
    }
    $('#ChooseOneLvsWrap_'+ChooseOneLvsCnt).remove();
    ChooseOneLvsCnt--;
}

function addChooseOneShushuWrap(obj,c=0){
    var lvsIdx = $(obj).parents('.ChooseOneLvsWrap').data('idx')
    console.log(lvsIdx)
    ChooseOneShushuStages[lvsIdx]++
    var rewardIdx = lvsIdx + '_' + ChooseOneShushuStages[lvsIdx]
    ChooseOneRewardStages[rewardIdx] = 0
    var html = '';
    html +='<table class="table table-bordered ChooseOneShushuWrap" id="ChooseOneShushuWrap_'+rewardIdx+'" data-idx="'+ChooseOneShushuStages[lvsIdx]+'">';
    html +='<thead><tr><td colspan="5">';
    html +='<div class="form-group"><label class="col-sm-1 control-label m-b">数数分组 </label><div class="col-sm-10 input-group">';
    html +='<select class=" select2" name="ChooseOneConfig['+lvsIdx+'][data]['+ChooseOneShushuStages[lvsIdx]+'][shushu][]"  multiple  style="min-width: 100%">';
    $.each(shushuGroups, function(i,o){
        html += '<option value="'+i+'">'+o+'</option>'
    });
    html +='</select>';
    html +='</div></div>';
    html +='</td></tr>';
    html +='<tr><th>价格</th>';
    html +=     '<th>SKU</th>';
    html +=     '<th>折扣</th>';
    html +=     '<th>奖品</th>';
    html +=    '<th>奖励设置</th></tr></thead>' ;
    html +='<tbody id="ChooseOneRewardBody_'+rewardIdx+'">';

    html +='</tbody>' +
        '<tr >' +
        '<td colspan="5">' +
        '<div class="text-center">' +
        '<botton class="btn btn-primary addChooseOneRewardTrBtn" onclick="addChooseOneRewardTr(this)">添加购买全部选项</botton>' +
        '<botton class="btn btn-danger del-tr delChooseOneRewardTr" onclick="delChooseOneRewardTr(this)">删除购买全部选项</botton>' +
        '</div>' +
        '</td>' +
        '</tr>' +
        '</table>';
    $(obj).before(html)
    while(ChooseOneRewardStages[rewardIdx] < 3){
        $(obj).prev('#ChooseOneShushuWrap_'+rewardIdx).find('.addChooseOneRewardTrBtn').click()
    }
    $('.select2').select2({})
}
function delChooseOneShushuWrap(obj){
    var lvsIdx = $(obj).parents('.ChooseOneLvsWrap').data('idx')
    // console.log(lvsIdx)
    if(ChooseOneShushuStages[lvsIdx] < 1)return
    $(obj).parents('.ChooseOneLvsWrap').find('.ChooseOneShushuWrap:last').remove()
    ChooseOneShushuStages[lvsIdx]--
}

function addChooseOneRewardTr(obj){
    var lvsIdx = $(obj).parents('.ChooseOneLvsWrap').data('idx')
    var shushuIdx = $(obj).parents('.ChooseOneShushuWrap').data('idx')
    var rewardIdx = lvsIdx + '_' + shushuIdx
    var rewardKey = lvsIdx + '_' + shushuIdx+ '_' + ChooseOneRewardStages[rewardIdx]
    var prizes = '[]';
    var prize_show_arr = JSON.parse(prizes);
    var discountType = 'text'
    if(ChooseOneRewardStages[rewardIdx] < 3)discountType = 'hidden'
    prizes = escape(prizes);
    var html ='';
    html+='<tr id="ChooseOneRewardInfo_'+rewardKey+'" data-idx="'+ChooseOneRewardStages[rewardIdx]+'" class="commonStageRewardTr priceSkuGroup">';
    html +='<td><select onchange="priceChanged(this)"   class="form-control"  name="ChooseOneConfig['+lvsIdx+'][data]['+shushuIdx+'][data]['+ChooseOneRewardStages[rewardIdx]+'][price]" >' ;
    $.each(goodSku, function (k, v) {
        html += '<option value="' + v["price"] + '" >' + v["price"] + '</option>';
    });
    html +='</select></td>';
    html +='<td>' ;
    html +=     '<input type="text" class="form-control skuInput"  name="ChooseOneConfig['+lvsIdx+'][data]['+shushuIdx+'][data]['+ChooseOneRewardStages[rewardIdx]+'][sku]" value="" readonly>' ;
    html +='</td>';
    html +='<td>' ;
    html +=    '<input type="'+discountType+'" class="form-control"  name="ChooseOneConfig['+lvsIdx+'][data]['+shushuIdx+'][data]['+ChooseOneRewardStages[rewardIdx]+'][discount]" value="0">';
    html +='</td>';
    // html +='<td>' ;
    // html +=     '<input type="hidden" class="form-control"  name="ChooseOneConfig['+lvsIdx+'][data]['+shushuIdx+'][data]['+ChooseOneRewardStages[rewardIdx]+'][discount]" value="0">';
    // html +='</td>';
    html +='<td class="prizeShow prizeShowStage_'+ChooseOneRewardStages[rewardIdx]+'"></td>';
    html +='<td>' ;
    html +=    '<input type="hidden" class="form-control"  name="ChooseOneConfig['+lvsIdx+'][data]['+shushuIdx+'][data]['+ChooseOneRewardStages[rewardIdx]+'][gid]" value="'+(ChooseOneRewardStages[rewardIdx]+130000+1)+'">';
    html +=    '<input type="hidden" class="prizeInput" name="ChooseOneConfig['+lvsIdx+'][data]['+shushuIdx+'][data]['+ChooseOneRewardStages[rewardIdx]+'][prizes]" value="'+prizes+'">' ;
    html +=    '<button type="button" class="btn btn-outline btn-success" onclick="setPrize(this,\'setCommonPrize\',1)">奖励配置</button>' ;
    html +='</td>';// data-toggle="modal"
    html +='</tr>';
    $('#ChooseOneRewardBody_'+rewardIdx).append(html)
    ChooseOneRewardStages[rewardIdx]++
    if(ChooseOneRewardStages[rewardIdx] >= 4){
        $(obj).parent().find('.delChooseOneRewardTr').show()
        $(obj).hide()
    }else{
        $(obj).parent().find('.delChooseOneRewardTr').hide()
    }
}
function delChooseOneRewardTr(obj){
    var lvsIdx = $(obj).parents('.ChooseOneLvsWrap').data('idx')
    var shushuIdx = $(obj).parents('.ChooseOneShushuWrap').data('idx')
    var rewardIdx = lvsIdx + '_' + shushuIdx
    if(ChooseOneRewardStages[rewardIdx] < 1)return;
    ChooseOneRewardStages[rewardIdx]--
    var rewardKey = lvsIdx + '_' + shushuIdx+ '_' + ChooseOneRewardStages[rewardIdx]
    console.log(rewardKey);
    $('#ChooseOneRewardInfo_'+rewardKey).remove()
    if(ChooseOneRewardStages[rewardIdx] < 4){
        $(obj).parent().find('.addChooseOneRewardTrBtn').show()
        $(obj).hide()
    }
}





function initChooseOneWrap(){
    ChooseOneLvsCnt = 0;
    ChooseOneShushuStages = {}
    ChooseOneRewardStages = {}
    var html = ''
    html +=ChooseOneWrapStageTable();

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