(function($){
	var publish_version = false;
	var agentName = "wlyyAgent";
	var server, userAgent, loginUrl;
	
 
	$.support.cors = true;
	if(publish_version) { // 生产环境配置
		
	} else { // 测试环境配置
		server = "http://172.19.103.88:9092/wlyy/"
		loginUrl = "http://172.19.103.88:9092/wlyy/auth/login.html"
	}
	
	
	userAgent = localStorage.getItem(agentName)
	
	if(userAgent) {
		try{
			userAgent = JSON.parse(userAgent)
		}catch(e){
			location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
		}
	} else {
		location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
	}
	
//  var server = "http://172.19.103.88:9092/wlyy/",
//      userAgent = {"id":4800,"uid":"zbqD201703150222","imei":"864394010176834","token":"fcf8310ee6d911c3f25cdecd28b9bcdd","platform":2,"hospital":"3502050100","appVersion":"1.3.6"};
//  userAgent = JSON.stringify(userAgent);
	
	function httpGet(url,options) {
		//发送ajax请求
		return new Promise(function(resolve, reject) {
			$.ajax(server + url, 
				$.extend({},{
				type: 'GET',
				dataType: 'JSON',
				beforeSend: function(request) {
                    request.setRequestHeader("userAgent", userAgent);
				},
				error: function(res) {
					reject(res)
				},
				success: function(res) {
					failCodeHandle(res)
					resolve(res)
				}
			},options));
		})
	}
	
	function httpPost(url,options) {
		//发送ajax请求
		return new Promise(function(resolve, reject) {
			$.ajax(server + url, 
				$.extend({},{
				type: 'POST',
				dataType: 'JSON',
				beforeSend: function(request) {
                    request.setRequestHeader("userAgent", userAgent);
				},
				error: function(res) {
					reject(res)
				},
				success: function(res) {
					failCodeHandle(res)
					resolve(res)
				}
			},options));
		})
	}
	
	function failCodeHandle(res) {
		var tip = "";
		if(res.status == 999) {
			tip = "此账号已在别处登录，请重新登录";
		} else if(res.status == 998) {
			tip = "登录超时，请重新登录";
		} else if(res.status == 997) {
			tip = "此账号未登录，请先登录"
		}
		if(tip) {
			toastr && toastr.warning(tip)
			location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
		}
	}
	
	APIService = {
        server: server,
        httpGet: httpGet,
        httpPost: httpPost,
        getTeamInfo: function(doctor,data){
            return httpGet('/doctor/admin-teams/team/'+doctor+'/teams', data);
        }
	}
	
	window.APIService = APIService;
})(jQuery)
