define(['zepto', 'vue', 'Utils', 'md5'], function($, Vue, Utils, md5) {

	var selectBg = $('.selectBg');
	var remderList = $("#remderList");
	var doctorBg = $(".doctorBg");
	var windowH = $(window).height();
	var paixu = $(".paixu");
	var shaixuan = $(".shaixuan");
	var qudao = Utils.getRequest("linkType"); //用于判断来源,wx,hz,dd微信,会诊,电刀
	var subQudao = Utils.getRequest("sub");
	var domainIds = [];
	var tjDoctor = false;
	var tuijiancanshu = "";
	if (qudao == "dd" || subQudao == "dd") { //电刀跳过来的处理
		domainIds = ["CT1"]; //电刀过来的医生增加治疗领域:手术的筛选条件
		tjDoctor = true; //点刀默认加载的医生是推荐医生
		tuijiancanshu = "operation";
	}
	if (qudao == "hz" || subQudao == "hz") {
		tjDoctor = true; //会诊默认加载的医生是推荐医生
		tuijiancanshu = "consultation";
	}
	var other = true;
	if (qudao == "wx" || qudao == "app") {
		other = false;
	}
	if (subQudao == "dd" || subQudao == "hz") {
		other = true;
	}
	var chose = {
		//缓存搜索条件
		queryDoctorsRequest: {
			max: "CURRENT",
			localId: "", //地区ID
			diseaseId: '', //疾病ID
			domainIds: domainIds, //领域ID
			hospitalId: '', //医院ID
			hospitalName: '',
			departmentId: '', //科室ID
			sortType: 'default',
			treatWay: [], //治疗方式
			service: [] //开通服务
		},
		other: other,
		doctorHeightHasChange: true,
		doctorListHeight: "",
		isImgLazy: false,
		windowH: $(window).height(),
		scrollTopBefore: '0',
		hasMore: true, //标记是否加载更多医生数据字段
		lshospital: "",
		citySession: '',
		hospitalSession: '',
		diseaseSession: '',
		toHospital: false,
		toCity: false,
		toDisease: false,
		doctorList: [],
		srollLs: 0,
		shaixuanCount: 0, //选中的筛选条件数量
		offset: 1,
		skillTitle: {
			ST1: '主任医师',
			ST2: '副主任医师',
			ST3: '主治医师',
			ST4: '住院医师',
			ST5: '随访医师',
			ST6: '实习医师'
		},
		academicTitle: {
			AT1: '教授',
			AT2: '副教授',
			AT3: '讲师'
		},

		fetchCityData: function() {
			if (!!sessionStorage.cityData) {
				return;
			}

			$.ajax({
				url: "https://testdevgw.medtap.cn/common/listAreaV0",
				contentType: "application/json",
				headers: Utils.getHeaders(),
				data: Utils.setToken(),
				type: 'get',
				async: true,
				dataType: 'json',
				success: function(data) {
					if (data.success == true) {
						sessionStorage.cityData = JSON.stringify(data.content.areas);
					}
				},
				error: function(xhr, textStatus, errorThrow) {
					console.log(xhr.readyState);
				}
			});
		},
		fetchDiseasesData: function() {

			if (sessionStorage.diseasesData) {
				return;
			}

			$.ajax({
				url: "https://testdevgw.medtap.cn/common/listDisease",
				contentType: "application/json",
				headers: Utils.getHeaders(),
				data: Utils.setToken(),
				type: 'get',
				async: true,
				dataType: 'json',
				success: function(data) {
					if (data.success) {
						data = data.content.disease;
						sessionStorage.diseasesData = JSON.stringify(data);
					}
				},
				error: function(data) {

				}
			})


		},
		renderSelectDom: function(opts) {
			selectBg.addClass("on");
			var data, html, i, len;
			html = '';
			if (opts.type == 0) { //代表是渲染地区医院数据
				switch (opts.nextDomType) {
					case "province": //渲染省份数据
						selectBg.find(".one").html("");
						selectBg.find(".two").html("");
						data = JSON.parse(sessionStorage.cityData);
						html += '<ul class="firstList-f">';
						html +=
							'<li type="0" nextDomType="city" provinceId="" cityId="" cityName="" hospitalId="" departmentId="" provinceName="全国">全国</li>';
						for (i = 0, len = data.length; i < len; i++) {
							html += '<li type="0" nextDomType="city" provinceId="' + data[i].provinceId +
								'"cityId="" cityName="" hospitalId="" departmentId="" provinceName="' + data[i].name + '">' + data[i].name +
								'</li>';
						}
						html += '</ul>';
						selectBg.find(".one").html(html);
						break;
					case "city":
						data = JSON.parse(sessionStorage.cityData)
						html += '<ul class="firstList-s">';
						if (opts.provinceId == "") {
							html +=
								'<li type="0" nextDomType="doctor" provinceId="" cityId="" cityName="" hospitalId="" departmentId="" ityName="" provinceName="">全国医生</li>';
						} else {
							for (i = 0, len = data.length; i < len; i++) {
								if (data[i].provinceId == opts.provinceId) {
									data = data[i].city;
									i = 0;
									break;
								}
							}
							for (i = 0, len = data.length; i < len; i++) {
								html += '<li type="0" nextDomType="hospital" provinceId="' + opts.provinceId + '"provinceName="' + opts.provinceName +
									'"cityId="' + data[i].cityId + '"  hospitalId=""  departmentId="" cityName="' + data[i].name + '">' + data[
										i].name + '</li>';
							}
						}

						html += '</ul>';
						selectBg.find(".two").html(html);
						break;
					case "hospital":
						selectBg.find(".two").html("");
						$.ajax({
							url: "https://testdevgw.medtap.cn/common/listHospitalWithDepartment",
							contentType: "application/json",
							type: 'get',
							async: true,
							dataType: 'json',
							data: Utils.setToken({
								cityId: opts.cityId,
								offset: 1,
								limit: 10000
							}),
							success: function(back) {
								if (!back.success) {
									Utils.pop(back.resultCode);
								}
								data = back.content.hospitals;
								html += '<ul class="firstList-f">';
								html += '<li type="0" nextDomType="dep" provinceId="' + opts.provinceId + '" cityId="' + opts.cityId +
									'" hospitalId="" departmentId="">全市</li>';
								for (var i = 0, len = data.length; i < len; i++) {
									html += '<li type="0" nextDomType="dep" provinceId="' + opts.provinceId + '" cityId="' + opts.cityId +
										'" hospitalId="' + data[i].hospitalId + '" departmentId="">' + data[i].hospitalName + '</li>';
								}
								html += "</ul>";

								selectBg.find(".one").html(html);

							},
							error: function() {}
						});
						break;
					case "dep":

						$.ajax({
							url: "https://testdevgw.medtap.cn/common/listDepartByHospitalId",
							contentType: "application/json",
							type: 'get',
							async: true,
							dataType: 'json',
							headers: Utils.getHeaders(),
							data: Utils.setToken({
								hospitalId: opts.hospitalId,
								offset: 1,
								limit: 10000
							}),
							success: function(back) {
								if (!back.success) {
									Utils.pop(back.resultCode);
								}
								data = back.content.departments;
								html += '<ul class="firstList-s">';
								if (opts.hospitalId == "") {
									html += '<li type="0" nextDomType="doctor" provinceId="' + opts.provinceId + '" cityId="' + opts.cityId +
										'" hospitalId="" departmentId="">全市医生</li>';

								} else {
									html += '<li type="0" nextDomType="doctor" provinceId="' + opts.provinceId + '" cityId="' + opts.cityId +
										'" hospitalId="' + opts.hospitalId + '" departmentId="">全院医生</li>';
									for (var i = 0, len = data.length; i < len; i++) {
										html += '<li type="0" nextDomType="doctor" provinceId="' + opts.provinceId + '" cityId="' + opts.cityId +
											'" hospitalId="' + opts.hospitalId + '" departmentId="' + data[i].hospitalDepartmentId + '">' + data[
												i].departmentName + '</li>';
									}
								}

								html += "</ul>";
								selectBg.find(".two").html(html);
							},
							error: function() {
								html += '<ul class="firstList-s">';
								if (opts.hospitalId == "") {
									html += '<li type="0" nextDomType="doctor" provinceId="' + opts.provinceId + '" cityId="' + opts.cityId +
										'" hospitalId="" departmentId="">全市医生</li>';

								}
								html += "</ul>";
								selectBg.find(".two").html(html);
							}
						});
						break;
				}

			}
			if (opts.type == 1) {

				if (opts.nextDomType == "disease") { //渲染疾病列表dom
					selectBg.find(".one").html("");
					selectBg.find(".two").html("");
					data = JSON.parse(sessionStorage.diseasesData);
					html += '<ul class="firstList-f">';
					html += '<li type="1" nextDomType="diseasenext" diseaseAllId="">全部</li>';
					for (i = 0, len = data.length; i < len; i++) {
						html += '<li type="1" nextDomType="diseasenext" diseaseAllId="' + data[i].diseaseId + '">' + data[i].diseaseName +
							'</li>';
					}
					html += '</ul>';
					selectBg.find(".one").html(html);
					return;
				}
				if (opts.nextDomType == "diseasenext") {

					selectBg.find(".two").html("");
					data = JSON.parse(sessionStorage.diseasesData);
					if (opts.diseaseAllId == "") {
						html += '<li type="1" nextDomType="doctor" diseaseAllId="" diseaseId="" >全部疾病</li>';

					} else {
						for (i = 0, len = data.length; i < len; i++) {
							if (data[i].diseaseId == opts.diseaseAllId) {
								data = data[i].diseaseItemList;
								i = 0;
								break;
							}
						}
						html += '<ul class="firstList-s">';
						for (i = 0, len = data.length; i < len; i++) {
							html += '<li type="1" nextDomType="doctor" diseaseAllId="' + opts.diseaseAllId + '" diseaseId="' + data[i].diseaseId +
								'">' + data[i].diseaseName + '</li>';
						}
					}

					html += '</ul>';
					selectBg.find(".two").html(html);
					return;
				}

			}
		},
		renderDoctorList: function(opts) { //from=name是指通过姓名搜索来的,type=more是说明是加载更多数据
			var _this = this;
			var requestUrl = '';
			var requestData;
			requestUrl = '/profile/queryDoctors';
			requestData = {
				max: chose.queryDoctorsRequest.max,
				localId: chose.queryDoctorsRequest.localId,
				diseaseId: chose.queryDoctorsRequest.diseaseId,
				domainIds: chose.queryDoctorsRequest.domainIds,
				hospitalId: chose.queryDoctorsRequest.hospitalId,
				hospitalName: chose.queryDoctorsRequest.hospitalName,
				departmentId: chose.queryDoctorsRequest.departmentId,
				sortType: chose.queryDoctorsRequest.sortType,
				accountId: "",
				globalAppType: '0'
			};
			if (tjDoctor) {
				requestUrl = "/medsrv/queryConsultationOperationDefaultDoctors";
				requestData = {
					max: chose.queryDoctorsRequest.max,
					type: tuijiancanshu,
					globalAppType: '1'
				}
			}
			selectBg.find('.serchTitle div').removeClass("on").attr("isShow", "false");
			selectBg.find(".godiqu").hide();
			$.ajax({
				url: 'https://testdevgw.medtap.cn/doctor/listDoctor',
				contentType: "application/json",
				type: 'post',
				async: true,
				dataType: 'json',
				headers: Utils.getHeaders(),
				data: JSON.stringify(Utils.setToken({
					limit: 10, //每页显示数量
					offset: chose.offset, //当前页
					doctorName: '', //医生姓名
					treatWay: chose.queryDoctorsRequest.treatWay.length > 0 ? chose.queryDoctorsRequest.treatWay : '', //治疗方式
					service: chose.queryDoctorsRequest.service.length > 0 ? chose.queryDoctorsRequest.service : '', //开通服务(预约咨询,预约转诊,私人医生)
					hospitalLocation: chose.queryDoctorsRequest.localId,
					hospitalId: chose.queryDoctorsRequest.hospitalId,
					departmentId: chose.queryDoctorsRequest.departmentId,
					treatDisease: !!chose.queryDoctorsRequest.diseaseId ? chose.queryDoctorsRequest.diseaseId : '',
					sortType: chose.queryDoctorsRequest.sortType //排序(综合,职称,活跃)
				})),
				success: function(data) {
					if (!data.success) {
						Utils.pop(data.resultDesc);
						return;
					}
					var docLen = data.content.doctorResults.length;
					if (docLen > 0) {
						chose.offset++;
					}
					chose.hasMore = docLen > 0;
					if (opts.more) {
						_this.doctorList = _this.doctorList.concat(data.content.doctorResults);
						chose.isSroll = true;
						chose.doctorHeightHasChange = true;
						if (docLen > 0) {
							if (docLen > 0 && docLen < 6) {
								$(".doctorBg .more").trigger("click");
							}
						}
						return;
					}
					_this.doctorList = data.content.doctorResults;
					chose.isSroll = true;
					chose.scrollTopBefore = 0;
					chose.doctorHeightHasChange = true;
					doctorBg.css("position", "relative");
					chose.srollLs = 0;
					if (docLen > 0) {
						if (docLen > 0 && docLen < 6) {

							$(".doctorBg .more").trigger("click");
						}
					}

				},
				error: function() {
					Utils.jzz(0);
				}
			});

		},
		showImg: function() {
			var scrollTop = $(window).scrollTop();
			if (chose.scrollTopBefore > scrollTop) {
				return;
			}
			chose.scrollTopBefore = scrollTop;
			var liList = doctorBg.find("li");
			$.each(liList, function() {
				var li = $(this).offset().top;
				if (chose.windowH > li - scrollTop) {
					var img = $(this).find('img');
					if (!img.attr("src")) {
						var imgUrl = $(this).attr("data_src");
						img.attr("src", imgUrl);
					}
				}
			});
		},
		loadMore: function() {
			this.renderDoctorList({
				form: "list",
				more: true
			});
		},
		eventBind: function() {
			var _this = this;
			selectBg.on('click', '.one li', function() {
				var type = $(this).attr("type");
				$(this).addClass("on").siblings().removeClass("on");
				var nextDomType;
				if (type == 0) {
					var provinceId = $(this).attr("provinceId");
					var cityId = $(this).attr("cityId");
					var hospitalId = $(this).attr("hospitalId");
					var hospitalName = $(this).text();
					var departmentId = $(this).attr("departmentId");
					var provinceName = $(this).attr("provinceName");
					nextDomType = $(this).attr("nextDomType");
					if (nextDomType == "dep") {
						chose.lshospital = $(this).text();
					}
					_this.renderSelectDom({
						type: type,
						nextDomType: nextDomType,
						provinceId: provinceId,
						cityId: cityId,
						hospitalId: hospitalId,
						departmentId: departmentId,
						provinceName: provinceName
					});
					return;
				}
				if (type == 1) {
					var diseaseAllId = $(this).attr("diseaseAllId");
					nextDomType = $(this).attr("nextDomType");
					_this.renderSelectDom({
						type: type,
						nextDomType: nextDomType,
						diseaseAllId: diseaseAllId,
						disease: ''
					});

				}

			});
			selectBg.on('click', ' .two li', function() {
				var type = $(this).attr("type");
				//地区医院选项点击处理
				if (type == 0) {
					var provinceId = $(this).attr("provinceId");
					var cityId = $(this).attr("cityId");
					var hospitalId = $(this).attr("hospitalId");
					var departmentId = $(this).attr("departmentId");
					var nextDomType = $(this).attr("nextDomType");
					var provinceName = $(this).attr("provinceName");
					var cityName = $(this).attr("cityName");
					$(this).addClass("on").siblings().removeClass("on");
					if (nextDomType == "doctor") {
						if (provinceId == "") {
							selectBg.find(".godiqu").html("");

						}
						chose.queryDoctorsRequest.max = "CURRENT";
						chose.queryDoctorsRequest.localId = cityId;
						chose.queryDoctorsRequest.hospitalId = hospitalId;
						chose.queryDoctorsRequest.departmentId = departmentId;
						chose.hospitalSession = selectBg.find("#remderList").html();
						chose.toHospital = true;
						chose.toCity = false;
						tjDoctor = false;
						chose.offset = 1;
						_this.renderDoctorList({
							form: "list",
							more: false
						});
						selectBg.removeClass("on");
						$("#hos").text(chose.lshospital);
						if (provinceId == "") {

							$("#hos").text("全国");
						}
						setTimeout(function() {
							$('body').scrollTop(0);
						}, 0);

						return;
					}
					if (nextDomType == "hospital") {
						selectBg.find(".godiqu").html("地区:" + provinceName + cityName);
						chose.citySession = selectBg.find("#remderList").html();
						chose.toHospital = false;
						chose.toCity = true;
						$("#hos").text("地区/医院");
					}

					_this.renderSelectDom({
						type: type,
						nextDomType: nextDomType,
						provinceId: provinceId,
						cityId: cityId,
						hospitalId: hospitalId,
						departmentId: departmentId
					});

					return;
				}
				//疾病选项点击处理
				if (type == 1) {

					$(this).addClass("on").siblings().removeClass("on");
					var diseaseId = $(this).attr("diseaseId");
					//重置搜索排序
					chose.queryDoctorsRequest.max = "CURRENT";
					//设置搜索条件为当前疾病Id
					chose.queryDoctorsRequest.diseaseId = diseaseId;
					//缓存当前点击操作选项,便于下一次直接调出显示
					chose.diseaseSession = selectBg.find("#remderList").html();
					chose.toDisease = true;
					//渲染医生列表
					tjDoctor = false;
					chose.offset = 1;
					_this.renderDoctorList({
						form: "list",
						more: false
					});
					selectBg.removeClass("on");
					var dis = $(this).text();
					$("#dis").html(dis);
					setTimeout(function() {
						$('body').scrollTop(0);
					}, 0);
					return;
				}

			});
			selectBg.find('.serchTitle').on('click', "div", function() {

				var bg, c;
				var isShow = $(this).attr("isShow");
				var listDomType = $(this).attr("nextdomtype");
				if (isShow == "false") {
					chose.srollLs = $(window).scrollTop();
					doctorBg.css("position", "fixed");

					$(this).attr("isShow", "true").addClass('on').siblings().attr('isShow', 'false').removeClass('on');

					if (listDomType == "province") {
						paixu.hide();
						selectBg.find(".godiqu").show();
						shaixuan.hide();
						if (chose.toHospital) {
							selectBg.find("#remderList").html(chose.hospitalSession);
							selectBg.addClass('on');
							bg = remderList.height();
							c = selectBg.find(".one li.on").offset().top;
							if (c > bg) {
								selectBg.find(".one ul").scrollTop(c - bg);
							}
							return;

						}
						if (chose.toCity) {
							selectBg.find("#remderList").html(chose.citySession);
							selectBg.addClass('on');
							bg = remderList.height();
							c = selectBg.find(".one li.on").offset().top;
							if (c > bg) {
								selectBg.find(".one ul").scrollTop(c - bg);
							}
							return;
						}

						_this.renderSelectDom({
							type: '0',
							nextDomType: 'province',
							provinceId: '',
							cityId: '',
							hospitalId: '',
							departmentId: ''
						});
					}
					if (listDomType == "disease") {
						paixu.hide();
						selectBg.find(".godiqu").hide();
						shaixuan.hide();
						if (chose.toDisease) {
							selectBg.find("#remderList").html(chose.diseaseSession);
							bg = remderList.height();
							c = selectBg.find(".two li.on").offset().top;
							if (c > bg) {
								selectBg.find(".two ul").scrollTop(c - bg);
							}
							selectBg.addClass('on');
							return;
						}

						_this.renderSelectDom({
							type: '1',
							nextDomType: 'disease',
							diseaseAllId: '',
							diseaseId: ''

						});
					}
					if (listDomType == "paixu") {
						paixu.show();
						selectBg.removeClass("on");
						shaixuan.hide();
					}
					if (listDomType == "shaixuan") {
						paixu.hide();
						selectBg.find(".godiqu").hide();
						shaixuan.show();
						selectBg.removeClass("on");
					}
					return;
				}
				$(this).attr("isShow", "false").removeClass("on");
				selectBg.removeClass("on");
				if (listDomType == "paixu") {
					paixu.hide();
				}
				if (listDomType == "shaixuan") {
					shaixuan.hide();
				}
				doctorBg.css("position", "relative");
				$(window).scrollTop(chose.srollLs);

			});
			selectBg.find(".name").on("click", function() {
				Utils.pushNewWindow("choseDoctorByName.html?v=1.0.0&linkType=" + Utils.getRequest("linkType") + '&access=' +
					Utils.getRequest('access') + '&sub=' + Utils.getRequest('sub') + '&sort=' + Utils.getRequest('sort') +
					'&wechatId=' + Utils.getRequest("wechatId") + '&orderType=' + Utils.getRequest("orderType") + '&agencyId=' +
					Utils.getRequest('agencyId') + '&operationType=' + Utils.getRequest('operationType') + '&businessDetail=' +
					Utils.getRequest('businessDetail'));
			});
			selectBg.on("click", '.godiqu', function() {
				selectBg.find("#remderList").html(chose.citySession);
				var bg = remderList.height();
				var c = selectBg.find(".one li.on").offset().top;
				if (c > bg) {
					selectBg.find(".one ul").scrollTop(c - bg);
				}
			});
			paixu.on("click", "li", function() {
				var sortType = $(this).attr("sortType");
				var tit = $(this).attr("tit");
				$(this).addClass("on").siblings().removeClass("on");
				chose.queryDoctorsRequest.sortType = sortType;
				chose.queryDoctorsRequest.max = "CURRENT";
				paixu.hide();
				$("#px").text(tit);
				tjDoctor = false;
				chose.offset = 1;
				chose.renderDoctorList({
					form: "list",
					more: false
				});

			});
			$(".options").on("click", "span", function() {
				!!$(this).hasClass('on') ?
					$(this).removeClass('on') && chose.shaixuanCount-- :
					$(this).addClass('on') && chose.shaixuanCount++;
			});
			$('.options-ok').click(function() {
				var way = [];
				$('.options-1').find('.on').each(function() {
					way.push($(this).attr('type'));
				});
				var service = [];
				$('.options-2').find('.on').each(function() {
					service.push($(this).attr('type'));
				});
				chose.queryDoctorsRequest.treatWay = way;
				chose.queryDoctorsRequest.service = service;
				$('.shaixuan').hide();
				//TODO
				//调用查询接口
				chose.offset = 1;
				chose.renderDoctorList({
					form: "list",
					more: false
				});
			});
			$(".doctorBg").on("click", '.more', function() {
				_this.renderDoctorList({
					form: "list",
					more: true
				});
			});
			$(".doctorBg").on("click", 'li', function() {
				var type = Utils.getRequest("linkType");
				var doctorId = $(this).attr("doctorId");
				var doctorName = $(this).attr("doctorName");
				if (type == "wx") {
					Utils.pushNewWindow(
						"https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_push_doctor/pages/doctorHomePage.html?v=1.0.0&type=wx&doctorId=" +
						doctorId + '&wechatId=' + Utils.getRequest("wechatId"));
					return;
				}

				window.selectDoctorBean = {
					doctorId: doctorId,
					doctorName: doctorName
				}

			});

			$(window).on("scroll", function() {
				if (!chose.isSroll) {
					return;
				}
				var scrollTop = $(window).scrollTop();
				if (chose.scrollTopBefore >= scrollTop) {
					return;
				}
				chose.scrollTopBefore = scrollTop;
				var docH;
				if (chose.doctorHeightHasChange) {
					docH = doctorBg.height();
					chose.doctorListHeight = docH;
					chose.doctorHeightHasChange = false;
				} else {
					docH = chose.doctorListHeight;
				}
				if (docH - (scrollTop + windowH) < 200) {
					chose.isSroll = false;
					if (!chose.hasMore) {
						return;
					}
					chose.loadMore();
				}
			});
		},
		isSroll: true,
		init: function() {
			if (!!localStorage.medtapGlobalData == false) {
				localStorage.medtapGlobalData = JSON.stringify({
					globalAppType: 0
				});
			}
			if (Utils.getRequest("applyReferral") == 1) {
				$('.options.options-2 span[type=customer]').addClass('on');
				chose.queryDoctorsRequest.service.push("customer");
				chose.shaixuanCount = 1;
			}
			chose.fetchCityData();
			chose.fetchDiseasesData();
			new Vue({
				el: "#app",
				data: chose
			});
			this.eventBind();
			$(".doctorBg .more").trigger("click");
			$("body").css("opacity", "1");
		}
	}

	chose.init();

});