var isLeader = false,
    level,
    docCode = '',
    docInfo;
    
var request = getRequest(),
	target_url = request.target_url
$(function(){
    //获取医生基本信息
    getDoctorInfo();
});

function getDoctorInfo(){
    APIService.getDoctorInfo().then(function(res){
        if(res.status == 200){
            docInfo = res.data;
            docCode = docInfo.code;
            isLeader = docInfo.isLeader == '1' ? true : false;
            level = docInfo.level;
            window.localStorage.setItem('docInfo', JSON.stringify(docInfo));
            if(isLeader){
                $("#consultMenu").removeClass("hidden");
            }
            //先获取团队信息显示
            getTeamInfo();
        }else{
            toastr && toastr.warning(res.msg)
        }
    })
}

function getTeamInfo(){
    APIService.getTeamInfo(docCode).then(function(res){
        if(res.status == 200){
            var manageTeams = '',
                orderTeams = '';
            var isLeader2 = false;
            if(level == 3){ //健管师，则续方订单中需要加上额外的两个菜单
                orderTeams = '<li><a class="J_menuItem" href="order-list-jg.html?type=1" data-index="0">需跟踪订单</a></li>'+
                    '<li><a class="J_menuItem" href="order-list-jg.html?type=2" data-index="0">已接收订单</a></li>';
            }
            for(var i=0; i<res.data.length; i++){
                var item = res.data[i];
                if(item.leaderCode == docInfo.code){
                    isLeader2 = true;
                }else{
                    isLeader2 = false;
                }
                console.log(isLeader2);
                manageTeams += '<li><a class="J_menuItem" href="prescription-management.html?id='+item.id+'" data-index="0" data-id="'+item.id+'">'+item.name+'</a></li>';
                orderTeams += '<li><a class="J_menuItem" href="order-list.html?id='+item.id+'&isLeader='+isLeader2+'" data-index="0">'+item.name+'</a></li>';
            }
            $("#manageTeams").empty().append(manageTeams);
            $("#orderTeams").empty().append(orderTeams);
            
            if(target_url) {
            	var url = decodeURIComponent(target_url)
            	$('#content-main .J_iframe').eq(0).attr('src', url)
            } else {
            	var $adminLink = $('#manageTeams a[data-id="'+docInfo.adminTeamCode+'"]');
	            if($adminLink.length) {
	            	$adminLink.trigger('click')
	            } else {
	            	$("#manageTeams li").eq(0).trigger('click')
	            }
            }
            
        }else{
            toastr && toastr.warning(res.msg)
        }
    });
}