//挂号数据装入框
var regDataContent = '<div id="regDataContent"><div id="regDataContainer"></div></div>';

//快速筛选装入框
var quickSearchContent = '<div id="quickSearch"><input type="text" onkeyup="javascript:quickSearch(this.value);" /><span style="color:gray">快速筛选结果，支持拼音首字母</span></div>';

var loadSDptContent = '<div id="loadSDptContent"><span id="cityName"></span>的<select onchange="javascript:selectRealDptBySdpt($(this).val());" id="sDptSelectList">装载科室列表...</select></div>';
//页面加载
$(function(){
	var tmp = $('#indexRegisterCtrl>div').html();
	$('#indexRegisterCtrl').html(tmp);
})

//搜索医生
function searchRegDoctor(){
	regContentClear();
	var doctor_name = $('#searchRegDoctor').val();
	$('#indexRegisterCtrl').append(regDataContent).addClass('ircChecked');
	if(doctor_name == ''){
		$('#regDataContainer').html('请输入医生姓名！');
		return false;
	}
	$('#regDataContainer').show().html('正在 ' + doctor_name + ' 搜索相关数据...');
	$('#indexRegisterCtrl :input').attr('disabled','disabled');
	$.ajax({
		type: "get",
		url: "/index.php?c=get_doc_by_dpt&a=true",
		data: {'sw':doctor_name},
		success: function(xml){
			count = $(xml).find("doctor").attr('total_count');
			$('#regDataContainer').html('<h4>检索关键字“' + doctor_name + '”共 ' + count + ' 条结果</h4>' + quickSearchContent + '<ul></ul>');
			if(count == 0){
				$('#regDataContainer ul').append('没有搜索到相关数据！');
				$('#quickSearch').hide();
				$('#regContentClose').show();
				$('#indexRegisterCtrl :input').removeAttr('disabled');
				return false;
			}
			$(xml).find("doctor_list").each(function(i){
				name = $(this).children('name').text();
				User_ID = $(this).children('User_ID').text();
				doc_url = $(this).children('doc_url').text();
				dpt_name = $(this).children('dpt_name').text();
				dpt_url = $(this).children('dpt_url').text();
				real_section = $(this).children('real_section').text();
				hpt_name = $(this).children('hpt_name').text();
				hpt_url = $(this).children('hpt_url').text();
				real_hospital = $(this).children('real_hospital').text();
				city_id = $(this).children('city_id').text();
				city_name = $(this).children('city_name').text();
				hpt_py = $(this).children('hpt_py').text();
				dpt_py = $(this).children('dpt_py').text();
				py = $(this).children('py').text();
				reg_day = '';
				for(n=1;n<=7;n++){
					reg_day_am = reg_day_pm = '';
					$(this).children('reg_day').each(function(j){
						week = parseInt($(this).children('week').text());
						week = (week == 0) ? 7 : week;
						day = parseInt($(this).children('day').text());
						if(week == n){
							reg_day_am = (day == 0) ? ' class="reg_day_mark"' : '';
							reg_day_pm = (day == 1) ? ' class="reg_day_mark"' : '';
						}
					})
					reg_day += '<li' + reg_day_am + '></li><li' + reg_day_pm + '></li>';
				}
				$('#regDataContainer>ul').append('<li><a onmouseover="javascript:showDoctorDetails(\'' + User_ID + '\',this)" onmouseout="javascript:hideDoctorDetails()" title="点击挂号" href="http://' + doc_url + '/reg.php" target="_blank">' + name + '<span style="display:none;">' + py + '</span></a><span> - <a title="点击浏览该科室所有医生" href="javascript:selectDoctorByRealDpt(' + real_section + ',\'' + dpt_name + '\',' + city_id + ');">' + dpt_name + '<span style="display:none;">' + dpt_py + '</span></a> - <a title="点击浏览该医院所有科室" href="javascript:selectRealDptByHpt(' + real_hospital + ',\'' + hpt_name + '\',' + city_id + ');">' + hpt_name + '<span style="display:none;">' + hpt_py + '</span></a></span><ul class="doctor_reg_time left">' + reg_day + '</ul></li>');
			})
			if(count > 20){
				$('#regDataContainer').append('<div>本页只列出20条显示结果，请尽量精确你的搜索范围！</div>');
			}
			$('#regContentClose').show();
			$('#indexRegisterCtrl :input').removeAttr('disabled');
		},
		error: function(){
			//alert('致命错误！');
		}
	});
}

//通过地区选择医院
function selectHospitalByArea(){
	regContentClear();
	$('#regDataContent').show();
	$('#regContentClose').hide();
	$('#regDataContainer').html('');
    $('#indexRegisterCtrl').append(regDataContent).addClass('ircChecked');
	area_val = $('#areaList').val();
    if(isNaN(area_val) || area_val == ''){
		$('#regDataContainer').html('请选择一个城市！');
		return false;
	}
	area_name = $('#areaList').find('[selected]').text();
	$('#regDataContainer').html('正在获取 ' + area_name + ' 地区医院数据...');
	$('#indexRegisterCtrl :input').attr('disabled','disabled');
	$.ajax({
		type: "get",
		url: "/index.php?c=get_hpt_by_city&a=true",
		data: {'city':area_val},
		success: function(xml){
			count = $(xml).find("hospital").attr('total_count');
			$('#regDataContainer').html(loadSDptContent + '<h4>' + area_name + ' 可挂号医院共 ' + count + ' 家(同级别医院排序不分先后)</h4>' + quickSearchContent + '<ul></ul>');
			loadSDptList();
			$(xml).find("hospital_list").each(function(i){
				name = $(this).children('Name').text();
				level = $(this).children('level').text();
				Hospital_ID = $(this).children('Hospital_ID').text();
				url_id = $(this).children('url_id').text();
				py = $(this).children('py').text();
				$('#regDataContainer ul').append('<li>【'+ level +'】<a title="点击浏览该医院所有科室" href="javascript:selectRealDptByHpt(' + Hospital_ID + ',\'' + name + '\');">' + name + '<span style="display:none;">' + py + '</span></a></li>');
			})
			$('#regContentClose').show();
			$('#indexRegisterCtrl :input').removeAttr('disabled');
		},
		error: function(){
			//alert('致命错误！');
		}
	});
}

//通过医院选择科室
function selectRealDptByHpt(hpt_id,name,city_id){
	regContentClear();
	$('#regDataContent').show();
	$('#regContentClose').hide();
	$('#regDataContainer').html('');
    $('#indexRegisterCtrl').append(regDataContent).addClass('ircChecked');
	$('#regDataContainer').html('正在获取 ' + name + ' 科室数据...');
	$('#areaList option[value=' + city_id + ']').attr('selected','selected');
	$('#indexRegisterCtrl :input').attr('disabled','disabled');
	$.ajax({
		type: "get",
		url: "/index.php?c=get_dpt_by_hpt&a=true",
		data: {'hpt':hpt_id},
		success: function(xml){
			count = $(xml).find("department").attr('total_count');
			$('#regDataContainer').html(loadSDptContent + '<h4>' + name + ' 可挂号科室共 ' + count + ' 个(排序不分先后)</h4>' + quickSearchContent + '<div class="reSelect"><a href="javascript:selectHospitalByArea();">返回</a></div><ul></ul>');
			loadSDptList();
			$(xml).find("department_list").each(function(i){
				name = $(this).children('Name').text();
				Section_ID = $(this).children('Section_ID').text();
				url_id = $(this).children('url_id').text();
				doc_count = $(this).children('doc_count').text();
				py = $(this).children('py').text();
				$('#regDataContainer ul').append('<li title="' + doc_count + '位医生可挂号，点击浏览该科室所有医生"><a href="javascript:selectDoctorByRealDpt(' + Section_ID + ',\'' + name + '\');">' + name + '<span style="display:none;">' + py + '</span></a><span class="list_count">(' + doc_count + ')</span></li>');
			})
			$('#regContentClose').show();
			$('#indexRegisterCtrl :input').removeAttr('disabled');
		},
		error: function(){
			//alert('致命错误！');
		}
	});
}

//通过科室选择医生
function selectDoctorByRealDpt(dpt_id,name,city_id){
	regContentClear();
	$('#regDataContent').show();
	$('#regContentClose').hide();
	$('#regDataContainer').html('');
    $('#indexRegisterCtrl').append(regDataContent).addClass('ircChecked');
	$('#regDataContainer').html('正在获取 ' + name + ' 的医生数据...');
	$('#areaList option[value=' + city_id + ']').attr('selected','selected');
	$('#indexRegisterCtrl :input').attr('disabled','disabled');
	$.ajax({
		type: "get",
		url: "/index.php?c=get_doc_by_dpt&a=true",
		data: {'dpt':dpt_id},
		success: function(xml){
			xml = $(xml).find("doctor_list");
			count = xml.parent().attr('total_count');
			hpt_name = xml.children("hpt_name:eq(0)").text();
			real_hospital = xml.children("real_hospital:eq(0)").text();
			city_id = xml.children("city_id:eq(0)").text();
			$('#regDataContainer').html(loadSDptContent + '<h4>' + hpt_name + ' ' + name + ' 医生共 ' + count + ' 个(排序不分先后)</h4>' + quickSearchContent + '<div class="reSelect"><a href="javascript:selectRealDptByHpt(' + real_hospital + ',\'' + hpt_name + '\',' + city_id + ');">返回</a></div><ul></ul>');
			loadSDptList();
			xml.each(function(i){
				User_ID = $(this).children('User_ID').text();
				name = $(this).children('name').text();
				doc_url = $(this).children('doc_url').text();
				dpt_name = $(this).children('dpt_name').text();
				dpt_url = $(this).children('dpt_url').text();
				hpt_name = $(this).children('hpt_name').text();
				hpt_url = $(this).children('hpt_url').text();
				py = $(this).children('py').text();
				reg_day = '';
				for(n=1;n<=7;n++){
					reg_day_am = reg_day_pm = '';
					$(this).children('reg_day').each(function(j){
						week = parseInt($(this).children('week').text());
						week = (week == 0) ? 7 : week;
						day = parseInt($(this).children('day').text());
						if(week == n){
							reg_day_am = (day == 0) ? ' class="reg_day_mark"' : '';
							reg_day_pm = (day == 1) ? ' class="reg_day_mark"' : '';
						}
					})
					reg_day += '<li' + reg_day_am + '></li><li' + reg_day_pm + '></li>';
				}
				$('#regDataContainer>ul').append('<li><a onmouseover="javascript:showDoctorDetails(\'' + User_ID + '\',this)" onmouseout="javascript:hideDoctorDetails()" title="点击挂号" class="left" href="http://' + doc_url + '/reg.php" target="_blank">' + name + '<span style="display:none;">' + py + '</span></a> <ul class="doctor_reg_time right">' + reg_day + '</ul></li>');
			})
			$('#regContentClose').show();
			$('#indexRegisterCtrl :input').removeAttr('disabled');
		},
		error: function(){
			//alert('致命错误！');
		}
	});
}

//通过标准科室和地区选择真实科室
function selectRealDptBySdpt(s_dpt_id){
	regContentClear();
	$('#regDataContent').show();
	$('#regContentClose').hide();
	$('#regDataContainer').html('');
	city_id = $('#areaList').val();
	city_name = $('#areaList').find('[selected]').text();
    $('#indexRegisterCtrl').append(regDataContent).addClass('ircChecked');
	$('#regDataContainer').html('正在筛选 ' + city_name + ' 的科室...');
	$('#indexRegisterCtrl :input').attr('disabled','disabled');
	$.ajax({
		type: "get",
		url: "/index.php?c=get_dpt_by_hpt&a=true",
		data: {'sd':s_dpt_id,'city':city_id},
		success: function(xml){
			count = $(xml).find("department").attr('total_count');
			$('#regDataContainer').html(loadSDptContent + '<h4>' + city_name + ' 可挂号科室共 ' + count + ' 个(排序不分先后)</h4>' + quickSearchContent + '<ul></ul>');
			loadSDptList(s_dpt_id);
			$(xml).find("department_list").each(function(i){
				name = $(this).children('Name').text();
				Section_ID = $(this).children('Section_ID').text();
				url_id = $(this).children('url_id').text();
				doc_count = $(this).children('doc_count').text();
				Hospital_ID = $(this).children('Hospital_ID').text();
				hpt_name = $(this).children('hpt_name').text();
				hpt_url_id = $(this).children('hpt_url_id').text();
				hpt_py = $(this).children('hpt_py').text();
				py = $(this).children('py').text();
				$('#regDataContainer ul').append('<li title="' + doc_count + '位医生可挂号"><a title="点击浏览该科室所有医生"  href="javascript:selectDoctorByRealDpt(' + Section_ID + ',\'' + name + '\');">' + name + '<span style="display:none;">' + py + '</span></a><span class="list_count">(' + doc_count + ')</span> - <span class="hpt_url"><a title="点击浏览该医院所有科室" href="javascript:selectRealDptByHpt(' + Hospital_ID + ',\'' + hpt_name + '\');">' + hpt_name + '<span style="display:none;">' + hpt_py + '</span></a></span></li>');
			})
			$('#regContentClose').show();
			$('#indexRegisterCtrl :input').removeAttr('disabled');
		},
		error: function(){
			//alert('致命错误！');
		}
	});
}

function regContentClose(){
	$('#regDataContent').hide().remove();
	$('#indexRegisterCtrl').removeClass('ircChecked');
	$('#main_reg_wrap').hide();
}

function regContentClear(){
	$('#regDataContent').remove();
}
//拼音快速检索内容
function quickSearch(s){
	if(s!=''){
		$('#regDataContainer>ul>li').hide().filter(':contains('+ s +')').show();
	}else{
		$('#regDataContainer>ul>li').show();
	}
}

function loadSDptList(s_dpt_id){
	if(s_dpt_id == undefined) s_dpt_id = '';
	city_id = $('#areaList').val();
	city_name = $('#areaList').find('[selected]').text();
	$('#cityName').html(city_name);
	$.ajax({
		type: "get",
		url: "/index.php?c=get_sd&a=true",
		success: function(xml){
			var select_option = '';
			$(xml).find("section_one").each(function(i){
				cate_name = $(this).attr('title');
				dpt_option = '';
				$(this).children('dpt').each(function(j){
					dpt_name = $(this).text();
					dpt_id = $(this).attr('sid');
					selected_s_dpt = (s_dpt_id == dpt_id) ? ' selected' : '';
					dpt_option += '<option value="' + dpt_id + '"' + selected_s_dpt + '>' + dpt_name + '</option>';
				})
				$('#sDptSelectList').append('<optgroup label="' + cate_name + '">' + dpt_option + '</optgroup>').attr('value',s_dpt_id);
			})
		},
		error: function(){
			//alert('致命错误！');
		}
	});
}

