define(['zepto', 'vue', 'Utils'], function($, Vue, Utils) {
	//搜索按钮
	var jzz = $(".load");
	var btn = $("#btn");
	var doctorBg = $(".doctorBg");
	var body = $("body");
	var windowH = $(window).height();
	var cishu = 0;
	var other = true;
	var qudao = Utils.getRequest("linkType");
	var subQudao = Utils.getRequest('sub');
	if (qudao == "wx") {
		other = false;
	}
	if (subQudao == 'dd' || subQudao == 'hz') {
		other = true;
	}
	var chose = {
		doctorName: '',
		doctorList: [],
		max: "LAST",
		hasMore: false,
		noMore: true,
		scrollTopBefore: "0",
		windowH: $(window).height(),
		isScroll: true,
		isScroll01: true,
		scrollTopBefore01: 0,
		doctorListHeight: 0,
		doctorHeightHasChange: true,
		other: other,
		offset: 1,

		renderDoctorList: function(opts) {
			if (chose.doctorName == "") {
				return;
			}
			jzz.addClass("on");
			$.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({
					offset: chose.offset, //当前页
					limit: 10, //每页显示数量
					doctorName: chose.doctorName //医生姓名
				})),
				success: function(data) {
					if (!data.success) {
						Utils.pop(data.resultDesc);
						jzz.removeClass("on");
						return;
					}
					var docLen = data.content.doctorResults.length;
					if (docLen > 0) {
						chose.offset++;
					}
					if (opts.more) {
						chose.doctorList = chose.doctorList.concat(data.content.doctorResults);
					} else {
						chose.doctorList = data.content.doctorResults;
						chose.scrollTopBefore01 = 0;
					}
					chose.hasMore = docLen > 0;
					chose.noMore = docLen <= 0;
					chose.scrollTopBefore = 0;
					chose.doctorHeightHasChange = true;
					chose.isScroll = true;
					chose.isScroll01 = true;
					jzz.removeClass("on");
				},
				error: function() {
					jzz.removeClass("on");
				}
			}, true);

		},
		showImg: function() {

			var scrollTop = $(window).scrollTop();
			var liList = doctorBg.find(".has");
			if (liList.length < 1) {
				return;
			}
			$.each(liList, function() {
				var li = $(this).offset().top;
				if ((chose.windowH + 300) > li - scrollTop && li > scrollTop) {
					var img = $(this).find('img');
					if (!img.attr("src")) {
						var imgUrl = $(this).attr("data_src");
						$(this).removeClass("has");
						img.attr("src", imgUrl);
					}
				}
			});
			chose.isScroll = true;
		},
		eventBind: function() {
			btn.on("click", function() {
				chose.max = "LAST";
				body.scrollTop("0");
				chose.offset = 1;
				chose.renderDoctorList({
					more: false
				});
			});
			doctorBg.on("click", ".more", function() {
				chose.renderDoctorList({
					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&doctorId=" +
						doctorId + '&wechatId=' + Utils.getRequest('wechatId'));
					return;
				}
				window.selectDoctorBean = {
					doctorId: doctorId,
					doctorName: doctorName
				}

			});

			$(window).on("scroll", function() {
				if (chose.isScroll01) {
					chose.isScroll01 = false;
					setTimeout(function() {
						chose.isScroll01 = true;
						var ddd = $(window).scrollTop();
						if (chose.scrollTopBefore01 >= ddd) {
							return;
						}
						chose.scrollTopBefore01 = ddd;
						var docH;
						if (chose.doctorHeightHasChange) {
							docH = doctorBg.height();
							chose.doctorListHeight = docH;
							chose.doctorHeightHasChange = false;
						} else {
							docH = chose.doctorListHeight;

						}
						if (docH - (ddd + windowH) < 200) {
							chose.isScroll01 = false;
							if (!chose.hasMore) {
								return;
							}
							chose.renderDoctorList({
								more: true
							});
						}
					}, 1000);
				}
			});
		},

		init: function() {
			new Vue({
				el: "#app",
				data: chose
			});
			chose.eventBind();
			$("body").css("opacity", "1");

		}
	}
	chose.init();

});
