define([ 'zepto', 'medtap','mui'], function($, medtap,mui) {
	var chat = {
		doctorId: medtap.getRequest('doctorId'),
		relationId: '',
		userId: '',
		doctorName: '',
		doctorProfile: '',
		helperName: '',
		helperProfile: '',
		userName: '',
		userProfile: '',
		hasMore: false,
		limit: 8,
		offset: 1,
		// 刷新的起始页数
		offsetRefresh:1,
		minId: 0,
		maxId: 0,
		data: null,
		localId: '',
		localIds: [],
		serverId: '',
		imgList: [],
		// 消息列表
		record:[],
		// 发送是传递的类型编号 0 文字 1 图片 2 ...
		num: 0,
		// 图片的ossKey
		imgKey: '',
		// 上传图片后的url地址
		imgUrl: '',
		// 价格横线
		throwClass:'',
		// 显示的图文价格
		pic_price:0,
		// 显示的私人医生价格
		private_price: 0,
		// 没消息后请求开关
		noTalkList:true,
		init: function() {
			chat.getDoctorDetail();
			chat.bindEve();
			chat.talkRelation();
			chat.getWechatTicket();
			chat.getAttention();
			// var getTalk = setInterval(function() {
			// 	chat.getTalkRecord();
			// 	window.scrollTo(0, document.documentElement.clientHeight);
			// }, 10000)
			// $('input').on('focus',function(){
			// 	//$('body').scrollTop(0);
			// 	clearInterval(getTalk);
			// });
			// $('input').on('blur',function(){
			// 	//$('body').scrollTop(0);
			// 	var getTalk = setInterval(function() {
			// 		chat.getTalkRecord();
			// 		window.scrollTo(0, document.documentElement.clientHeight);
			// 	}, 5000)
			// });
			// 监听页面滚动到底部的方法

		// 	document.querySelector('body').addEventListener('touchmove', function (e) {
		// 		if (!document.querySelector('#content').contains(e.target)) {
		// 			e.preventDefault();
		// 	}
		//  }, {passive: false}); 
		// 新方法
		var overscroll = function(el) {
			  el.addEventListener('touchstart', function() {    var top = el.scrollTop
			      , totalScroll = el.scrollHeight
			      , currentScroll = top + el.offsetHeight;    
			    if(top === 0) {
			      el.scrollTop = 1;
			    } else if(currentScroll === totalScroll) {
			      el.scrollTop = top - 1;
			    }
			  });
			  el.addEventListener('touchmove', function(evt) {   
			    if(el.offsetHeight < el.scrollHeight)
			      evt._isScroller = true;
			  });
			}
			overscroll(document.querySelector('.chat_list'));
			document.body.addEventListener('touchmove', function(evt) {  
			  if(!evt._isScroller) {
			    evt.preventDefault();
			  }
			});
			  //1.获取到列表的dom，刷新显示部分的dom，列表父容器的dom
				var container = document.querySelector('.chat_list');
				var refreshText = document.querySelector('.refreshText');
				var parent = document.querySelector('.container');
				//2.定义一些需要常用的变量
				var startY = 0;//手指触摸最开始的Y坐标
				var endY = 0;//手指结束触摸时的Y坐标
				//3.给列表dom监听touchstart事件,得到起始位置的Y坐标
				parent.addEventListener('touchstart',function(e){
						startY = e.touches[0].pageY;
						container.style.position = 'relative';
						container.style.transition = 'transform 0s';
				});
				// container.ontouchmove = function (e) {
				// 	var point = e.touches[0],
				// 		eleTop = container.scrollTop,
				// 		eleScrollHeight = container.scrollHeight,
				// 		eleOffsetHeight = container.offsetHeight,
				// 		eleTouchBottom = eleScrollHeight - eleOffsetHeight;
				// 	if (eleTop === 0) {
				// 		if (point.clientY > startY) {
				// 			e.preventDefault();
				// 		}
				// 	}
				// 	else if (eleTop === eleTouchBottom) {
				// 		if (point.clientY < startY) {
				// 			e.preventDefault()
				// 		}
				// 	}
				// };
				//4.给列表dom监听touchmove事件，当移动到一定程度需要显示上面的文字
				parent.addEventListener('touchmove',function (e) { 
					
						if(isTop() && (e.touches[0].pageY-startY) > 0){
								refreshText.style.height = "50px";
								parent.style.transform = "translateY(50px)";
								parent.style.transition = "all ease 0.3s";
								refreshText.innerHTML = "获取更多聊天记录...";	
						}
				});
				//5.给列表dom监听touchend事件，此时说明用户已经松开了手指，应该进行异步操作了
				parent.addEventListener('touchend',function (e) { 
						if(isTop()){
								refreshText.innerHTML = "正在刷新...";
								setTimeout(function(){
										parent.style.transform = "translateY(0)";
										console.log('成功刷新');
								},800)
						}
						medtap.loading(1);
						chat.refreshRecord(1)
						return;
				})
				function isTop(){
						var t = document.documentElement.scrollTop||document.body.scrollTop;
						return t === 0 ? true : false;
				}
		},
		bindEve: function() {
			$('.close_info_btn').unbind().bind('click', function() {
				$('.chat_info').hide();
				$(this).hide();
				$('.chat_list').css('margin-top', '1rem;');
				$('.open_info_btn').show();
			});

			$('.open_info_btn').unbind().bind('click', function() {
				$('.chat_info').show();
				$(this).hide();
				$('.chat_list').css('margin-top', '3.6rem;');
				$('.close_info_btn').show();
			});

			$('.send').unbind().bind('click', function() {
				if (!$('#sendMsg').val()) {
					medtap.toast({
						message: '请输入留言内容',
						time: 1500
					});
				} else {
					chat.sendMsg(chat.num);
				}
			});

			
			//跳转至医生主页
			$('.doctor_profile').unbind().bind('click',function(){
				medtap.pushWindow('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_push_doctor/pages/doctorHomePage.html?doctorId=' + chat.doctorId + '&wechatId=' + medtap.getRequest('wechatId'));
			});
		},
		//绑定，获取聊天关系
		talkRelation: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/talk/talkRelation',
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					doctorId: parseInt(chat.doctorId)
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						chat.relationId = res.content.talkRealtion.id;
						chat.userId = res.content.talkRealtion.userId;
						chat.doctorName = res.content.talkRealtion.doctorName;
						chat.doctorProfile = res.content.talkRealtion.doctorProfile;
						chat.userName = res.content.talkRealtion.username;
						chat.userProfile = res.content.talkRealtion.userProfile;
						chat.helperName = res.content.talkRealtion.helperName;
						chat.helperProfile = res.content.talkRealtion.helperProfile;
						$('.doctor_profile').attr('src', chat.doctorProfile);
						$('.doctor_name').html(chat.doctorName);
						$('.doctor_title').html(
							res.content.talkRealtion.hasOwnProperty('technologyProfessional')
								? res.content.talkRealtion.technologyProfessional.value
								: ''
						);
						$('.assistant_name').html(chat.helperName);
						$('.assistant_profile').attr('src', res.content.talkRealtion.helperProfile);
						chat.getTalkRecord();
					} else {
						chat.getToken();
					}
				}
			});
		},
		// 获取门票
		getWechatTicket: function() {
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/wechat/lung/fetchWechatTicket',
				type: 'GET',
				async: false,
				data: {
					url: window.location.href
				},
				success: function(res) {
					chat.data = res.content;
					wx.config({
						debug: false,
						appId: chat.data.appId, // 必填，公众号的唯一标识
						timestamp: chat.data.timestamp, // 必填，生成签名的时间戳
						nonceStr: chat.data.nonceStr, // 必填，生成签名的随机串
						signature: chat.data.signature, // 必填，签名，见附录1
						jsApiList: [ 'chooseImage', 'previewImage', 'uploadImage' ]
					});
				}
			});
		},
		//选择图片
		choseImage: function() {
			$('.footer_addPic').on('click', function() {
				console.log('点击了添加图片');
				var html = '';
				wx.ready(function() {
					var syncUpload = function(localIds) {
						console.log(localIds);
						chat.localId = localIds[0];
						localIds = localIds.length > 1 ? localIds.slice(1) : localIds[0];
						wx.uploadImage({
							localId: chat.localId,
							isShowProgressTips: 1,
							success: function(res) {
								var serverId = res.serverId; // 返回图片的服务器端ID
								//其他对serverId做处理的代码
								chat.upLoadImg(serverId, chat.localId);
							}
						});
					};
					wx.chooseImage({
						count: 1,
						sizeType: [ 'original', 'compressed' ],
						sourceType: [ 'album', 'camera' ],
						success: function(res) {
							chat.localIds = res.localIds;
							//所有需要上传到图片预览
							syncUpload(chat.localIds);
						}
					});
				});
			});
		},
		// // 点击图片信息预览图片
		previewImg: function() {
			$('.chat_list').on('click', '.chat_nopadding img', function(e) {
				console.log(e);
				var curImageSrc = $(this).attr('src');
				var imgURls = [];
				if (curImageSrc) {
					imgURls.push(curImageSrc);
					wx.previewImage({
						current: curImageSrc, // 当前显示图片的http链接
						urls: imgURls // 需要预览的图片http链接列表
					});
				}
			});
		},
		//上传图片
		upLoadImg: function(serverId, localId) {
			$.ajax({
				url: 'https://testdevgw.medtap.cn/common/uploadWechatFile',
				type: 'POST',
				async: true,
				data: {
					bizType: '3',
					folder: 'bbs',
					mediaId: serverId,
					ext: 'jpg'
				},
				headers: {
					'apptype': 'wechat_lung',
					'user-os': 'wechat'
				},
				success: function(data) {
					if (data.success == true) {
						var res = data.content;
						var imgKey = res.ossKey;
						var imgUrl = res.url;
						chat.sendMsg(1, res.url);
					}
				}
			});
		},
		sendMsg: function(num,content) {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/talk/talk',
				type: 'post',
				async: false,
				contentType: 'application/json',
				data: {
					contentType: num,
					content:  content ? content : $('#sendMsg').val(),
					orderSn: '',
					relationId: chat.relationId
				},
				success: function(res) {
					// 发送玩消息滚动到最底部
					$('.chat_list').scrollTop( $('.chat_list')[0].scrollHeight);
					medtap.loading(0);
					if (res.success == true) {
						//window.location.reload();
						chat.getTalkRecord();
						$('#sendMsg').val('');
						//window.scrollTo(0, document.documentElement.clientHeight);
						//$(document).scrollTop($(document).height());
						if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
							setTimeout(function() {
								document.documentElement.scrollTop = document.documentElement.scrollHeight;
							});
						}
					} else {
						medtap.toast({
							message: res.resultDesc
						});
					}
				}
			});
		},
		//获取消息列表
		getTalkRecord: function() {
			//medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/talk/talkRecord',
				type: 'get',
				async: false,
				data: {
					offset: chat.offset,
					limit: chat.limit,
					relationId: chat.relationId,
					orderSn: ''
				},
				success: function(res) {
					console.log('消息列表',res)
					medtap.loading(0);
				chat.record = res.content.list;
					var html = '';
					if (chat.record.length == 0) {
						chat.hasMore = false;
						/* medtap.toast({
							message: '没有更多了'
						}) */
					} else {
						chat.hasMore = true;
						chat.minId = chat.record[0].id;
						chat.maxId = chat.record[chat.record.length - 1].id;
						//console.log(chat.minId + ':' + chat.maxId)
						for (var i = 0; i < chat.record.length; i++) {
							var type = chat.record[i].contentType;
							if (type == 0) {
								if (chat.record[i].senderId == chat.userId) {
									html +=
										'<div class="chat_item chat_item_right">' +
										'<p class="message_time">' +
										chat.record[i].createTime.substring(5, 16) +
										'</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' +
										chat.userProfile +
										'" class="chat_profile send_profile">' +
										'<div class="chat_message_warp">' +
										'<div class="chat_message chat_message_send">' +
										chat.record[i].content +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								} else if (chat.record[i].senderId == chat.doctorId) {
									html +=
										'<div class="chat_item chat_item_left">' +
										'<p class="message_time">' +
										chat.record[i].createTime.substring(5, 16) +
										'</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' +
										chat.doctorProfile +
										'" class="chat_profile from_profile">' +
										'<div class="chat_message_warp">' +
										'<p class="chat_from_name">' +
										chat.doctorName +
										'医生</p>' +
										'<div class="chat_message chat_message_from">' +
										chat.record[i].content +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								} else {
									html +=
										'<div class="chat_item chat_item_left">' +
										'<p class="message_time">' +
										chat.record[i].createTime.substring(5, 16) +
										'</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' +
										chat.helperProfile +
										'" class="chat_profile from_profile" style="margin-top:0.2rem">' +
										'<div class="chat_message_warp">' +
										'<p class="chat_from_name">' +
										chat.helperName +
										'</p>' +
										'<div class="chat_message chat_message_from">' +
										chat.record[i].content +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								}
							} else if (type == 10) {
								var content = JSON.parse(chat.record[i].content);
								chat.throwClass = content.payFee==0? 'service_price_throw':'';
								chat.pic_price = content.payFee==0? content.totalFee : content.payFee;
								html +=
									'<div class="service_message img_consult clearfix" style="margin-bottom:0.2rem;" data-id="' +
									content.orderSn +
									'">' +
									'<div class="service_left_box sevice_img_consult">' +
									'<img src="../images/consult_order_twzx_png@2x.png" >' +
									'<p class="service_title">图文咨询</p>' +
									'<p class="service_price '+ chat.throwClass +'">' +
									chat.pic_price + '元' +
									'</p>' +
									'</div>' +
									'<div class="service_right_box service_message_detail">' +
									'<p class="detail_patient_name">' +
									content.texts[0] +
									'</p>' +
									'</div>' +
									'</div>';
							} else if (type == 3) {
								var content = chat.record[i].content;
								content.replace(/\n/g, '<br>');
								html += '<p class="system_msg">' + content.replace(/\n/g, '<br>') + '</p>';
							} else if (type == 11) {
								var content = JSON.parse(chat.record[i].content);
								chat.throwClass = content.payFee==0? 'service_price_throw':'';
								chat.private_price = content.payFee==0? content.totalFee : content.payFee;
								html +=
									'<div class="service_message consult_box clearfix" data-id="' +
									content.orderSn +
									'">' +
									'<div class="service_left_box sevice_consult">' +
									'<img src="../images/consult_order_twzx_png@2x.png" >' +
									'<p class="service_title">私人医生</p>' +
									'<p class="service_price '+ chat.throwClass +'">' +
									chat.private_price + '元' +
									'</p>' +
									'</div>' +
									'<div class="service_right_box service_message_detail">' +
									/* '<p class="detail_patient_name">患者：张天问</p>'+ */
									'<p class="detail_question">' +
									content.texts[1] +
									'</p>' +
									'</div>' +
									'</div>';
							} else if (type == 1) {
								if (chat.record[i].senderId == chat.userId) {
									html +=
										'<div class="chat_item chat_item_right">' +
										'<p class="message_time">' +
										chat.record[i].createTime.substring(5, 16) +
										'</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' +
										chat.userProfile +
										'" class="chat_profile send_profile">' +
										'<div class="chat_message_warp">' +
										'<div class="chat_message chat_message_send chat_nopadding">' +
										// 图片消息
										'<img src="' +
										chat.record[i].contentPic[0] +
										'" />' +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								} else if (chat.record[i].senderId == chat.doctorId) {
									html +=
										'<div class="chat_item chat_item_left">' +
										'<p class="message_time">' +
										chat.record[i].createTime.substring(5, 16) +
										'</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' +
										chat.doctorProfile +
										'" class="chat_profile from_profile">' +
										'<div class="chat_message_warp">' +
										/* '<p class="chat_from_name">' +
										chat.doctorName +
										'医生</p>' + */
										'<div class="chat_message chat_message_from chat_nopadding">' +
										// 图片消息
										'<img src="' +
										chat.record[i].contentPic[0] +
										'" />' +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								}
							}
						}
					}
					//$('.chat_list').html(html);
					if (chat.offset == 1) {
						$('.chat_list .container').html(html);
						if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
							setTimeout(function() {
								document.documentElement.scrollTop = document.documentElement.scrollHeight;
							});
						}
					} else {
						$('.chat_list .container').prepend(html);
						if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
							setTimeout(function() {
								document.documentElement.scrollTop = document.documentElement.scrollHeight;
							});
						}
					}
					// 页面滚动到底部
					mui.scrollTo($('.chat_list').height(),50);
					$('.consult_box').unbind().bind('click', function() {
						var id = $(this).attr('data-id');
						medtap.pushWindow(
							'../../wechat_lung_prvivateDoctor/pages/pmdDetail.html?id=' +
								id +
								'&wechatId=' +
								medtap.getRequest('wechatId')
						);
					});

					$('.img_consult').unbind().bind('click', function() {
						var id = $(this).attr('data-id');
						medtap.pushWindow(
							'../../wechat_lung_imgConsult/pages/imgConsultDetail.html?id=' +
								id +
								'&wechatId=' +
								medtap.getRequest('wechatId')
						);
					});
				
				}
			});
		},
		//刷新消息列表
		refreshRecord: function(operateType) {
			$('.refreshText').html('正在刷新...')
			var id = 0;
			if (operateType == 0) {
				id = chat.maxId;
			} else {
				id = chat.minId;
			}
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/talk/talkRecord/refresh',
				type: 'get',
				async: false,
				data: {
					offset: chat.offsetRefresh,
					limit: 6,
					relationId: chat.relationId,
					orderSn: '',
					operateType: operateType,
					startId: id
				},
				success: function(res) {
					$('.refreshText').html('')
					console.log('刷新的消息',res)
					medtap.loading(0);
					chat.offsetRefresh += 1;
					var record = res.content.list
					// chat.getTalkRecord()
					var html = '';
					if (record.length == 0) {
						chat.hasMore = false;
						 medtap.toast({
							message: '没有更多了'
						}) 
					} else {
						chat.hasMore = true;
						if (operateType == 0) {
							chat.maxId = record[record.length - 1].id;
							for (var i = 0; i < record.length; i++) {
								var type = record[i].contentType;
								if (type == 0) {
									if (record[i].senderId == chat.userId) {
										html +=
											'<div class="chat_item chat_item_right">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.userProfile +
											'" class="chat_profile send_profile">' +
											'<div class="chat_message_warp">' +
											'<div class="chat_message chat_message_send">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									} else if (chat.record[i].senderId == chat.doctorId) {
										html +=
											'<div class="chat_item chat_item_left">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.doctorProfile +
											'" class="chat_profile from_profile">' +
											'<div class="chat_message_warp">' +
											'<p class="chat_from_name">' +
											chat.doctorName +
											'医生</p>' +
											'<div class="chat_message chat_message_from">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									} else {
										html +=
											'<div class="chat_item chat_item_left">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.helperProfile +
											'" class="chat_profile from_profile" style="margin-top:0.2rem">' +
											'<div class="chat_message_warp">' +
											'<p class="chat_from_name">' +
											chat.helperName +
											'</p>' +
											'<div class="chat_message chat_message_from">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									}
								} else if (type == 10) {
									var content = JSON.parse(chat.record[i].content);
									chat.throwClass = content.payFee==0? 'service_price_throw':'';
									chat.pic_price = content.payFee==0? content.totalFee : content.payFee;
									html +=
										'<div class="service_message img_consult clearfix" style="margin-bottom:0.2rem;" data-id="' +
										content.orderSn +
										'">' +
										'<div class="service_left_box sevice_img_consult">' +
										'<img src="../images/consult_order_twzx_png@2x.png" >' +
										'<p class="service_title">图文咨询</p>' +
										'<p class="service_price '+ chat.throwClass +'">' +
										chat.pic_price + '元' +
										'</p>' +
										'</div>' +
										'<div class="service_right_box service_message_detail">' +
										'<p class="detail_patient_name">' +
										content.texts[0] +
										'</p>' +
										'</div>' +
										'</div>';
								} else if (type == 3) {
									var content = record[i].content;
									content.replace(/\n/g, '<br>');
									html += '<p class="system_msg">' + content.replace(/\n/g, '<br>') + '</p>';
								} else if (type == 11) {
									var content = JSON.parse(chat.record[i].content);
									chat.throwClass = content.payFee==0? 'service_price_throw':'';
									chat.private_price = content.payFee==0? content.totalFee : content.payFee;
									html +=
										'<div class="service_message consult_box clearfix" data-id="' +
										content.orderSn +
										'">' +
										'<div class="service_left_box sevice_consult">' +
										'<img src="../images/consult_order_twzx_png@2x.png" >' +
										'<p class="service_title">私人医生</p>' +
										'<p class="service_price '+ chat.throwClass +'">' +
										chat.private_price + '元' +
										'</p>' +
										'</div>' +
										'<div class="service_right_box service_message_detail">' +
										/* '<p class="detail_patient_name">患者：张天问</p>'+ */
										'<p class="detail_question">' +
										content.texts[1] +
										'</p>' +
										'</div>' +
										'</div>';
								} else if (type == 1) {
									if (record[i].senderId == chat.userId) {
										html +=
											'<div class="chat_item chat_item_right">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.userProfile +
											'" class="chat_profile send_profile">' +
											'<div class="chat_message_warp">' +
											'<div class="chat_message chat_message_send chat_nopadding">' +
											// 图片消息
											'<img src="' +
											record[i].contentPic[0] +
											'" />' +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									} else if (record[i].senderId == chat.doctorId) {
										html +=
											'<div class="chat_item chat_item_left">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.doctorProfile +
											'" class="chat_profile from_profile">' +
											'<div class="chat_message_warp">' +
											/* '<p class="chat_from_name">' +
											chat.doctorName +
											'医生</p>' + */
											'<div class="chat_message chat_message_from chat_nopadding">' +
											// 图片消息
											'<img src="' +
											record[i].contentPic[0] +
											'" />' +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									}
								}
							}
							$('.chat_list .container').append(html);
						} else {
							chat.minId = record[0].id;
							for (var i = 0; i < record.length; i++) {
								var type = record[i].contentType;
								if (type == 0) {
									if (record[i].senderId == chat.userId) {
										html +=
											'<div class="chat_item chat_item_right">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.userProfile +
											'" class="chat_profile send_profile">' +
											'<div class="chat_message_warp">' +
											'<div class="chat_message chat_message_send">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									} else if (record[i].senderId == chat.doctorId) {
										html +=
											'<div class="chat_item chat_item_left">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.doctorProfile +
											'" class="chat_profile from_profile">' +
											'<div class="chat_message_warp">' +
											'<p class="chat_from_name">' +
											chat.doctorName +
											'医生</p>' +
											'<div class="chat_message chat_message_from">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									} else {
										html +=
											'<div class="chat_item chat_item_left">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.helperProfile +
											'" class="chat_profile from_profile" style="margin-top:0.2rem">' +
											'<div class="chat_message_warp">' +
											'<p class="chat_from_name">' +
											chat.helperName +
											'</p>' +
											'<div class="chat_message chat_message_from">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									}
								} else if (type == 10) {
									var content = JSON.parse(chat.record[i].content);
									chat.throwClass = content.payFee==0? 'service_price_throw':'';
									chat.pic_price = content.payFee==0? content.totalFee : content.payFee;
									html +=
										'<div class="service_message img_consult clearfix" style="margin-bottom:0.2rem;" data-id="' +
										content.orderSn +
										'">' +
										'<div class="service_left_box sevice_img_consult">' +
										'<img src="../images/consult_order_twzx_png@2x.png" >' +
										'<p class="service_title">图文咨询</p>' +
										'<p class="service_price '+ chat.throwClass +'">' +
										chat.pic_price + '元' +
										'</p>' +
										'</div>' +
										'<div class="service_right_box service_message_detail">' +
										'<p class="detail_patient_name">' +
										content.texts[0] +
										'</p>' +
										'</div>' +
										'</div>';
								} else if (type == 3) {
									var content = record[i].content;
									content.replace(/\n/g, '<br>');
									html += '<p class="system_msg">' + content.replace(/\n/g, '<br>') + '</p>';
								} else if (type == 11) {
									var content = JSON.parse(chat.record[i].content);
									chat.throwClass = content.payFee==0? 'service_price_throw':'';
									chat.private_price = content.payFee==0? content.totalFee : content.payFee;
									html +=
										'<div class="service_message consult_box clearfix" data-id="' +
										content.orderSn +
										'">' +
										'<div class="service_left_box sevice_consult">' +
										'<img src="../images/consult_order_twzx_png@2x.png" >' +
										'<p class="service_title">私人医生</p>' +
										'<p class="service_price '+ chat.throwClass +'">' +
										chat.private_price + '元' +
										'</p>' +
										'</div>' +
										'<div class="service_right_box service_message_detail">' +
										/* '<p class="detail_patient_name">患者：张天问</p>'+ */
										'<p class="detail_question">' +
										content.texts[1] +
										'</p>' +
										'</div>' +
										'</div>';
								} else if (type == 1) {
									if (record[i].senderId == chat.userId) {
										html +=
											'<div class="chat_item chat_item_right">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.userProfile +
											'" class="chat_profile send_profile">' +
											'<div class="chat_message_warp">' +
											'<div class="chat_message chat_message_send chat_nopadding">' +
											// 图片消息
											'<img src="' +
											record[i].contentPic[0] +
											'" />' +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									} else if (record[i].senderId == chat.doctorId) {
										html +=
											'<div class="chat_item chat_item_left">' +
											'<p class="message_time">' +
											record[i].createTime.substring(5, 16) +
											'</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' +
											chat.doctorProfile +
											'" class="chat_profile from_profile">' +
											'<div class="chat_message_warp">' +
											/* '<p class="chat_from_name">' +
											chat.doctorName +
											'医生</p>' + */
											'<div class="chat_message chat_message_from chat_nopadding">' +
											// 图片消息
											'<img src="' +
											record[i].contentPic[0] +
											'" />' +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									}
								}
							}
							$('.chat_list .container').prepend(html);
						}
					}
					
				}
			});
		},
		getAttention: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/friend/getAttention',
				type: 'get',
				async: false,
				data: {
					doctorId: parseInt(chat.doctorId)
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var status = res.content.doctorUserFriendType;
						if (status != 'FOLLOW_TYPE_FRIEND') {
							$('.no_follow').show();
						} else {
							$('.no_follow').hide();
						}
					}
				}
			});
		},
		getToken: function() {
			localStorage.wxLungToken = '';
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/auth/token',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					wechatId: medtap.getRequest('wechatId')
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var token = res.content.token;
						localStorage.setItem('wxLungToken', token);
						//main.checkUser();
						window.location.reload();
					} else {
					}
				},
				error: function(res) {
					console.log(res.resultDesc);
				}
			});
		},
		getDoctorDetail:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/doctor/getDoctorDetail',
				type: 'get',
				async: false,
				data: {
					doctorId: chat.doctorId
				},
				success:function(res){
					medtap.loading(0);
					var detail = res.content.doctorItem;
					//电话咨询
					if (detail.openConsult == false) {
						$('.to_consult').addClass('grayBtn');
					} else {
						$('.to_consult').removeClass('grayBtn');
						chat.getConsultNumber();
					}
					//图文咨询
					if (detail.isOpenImageConsult == false) {
						$('.to_img_consult').addClass('grayBtn');
					} else {
						$('.to_img_consult').removeClass('grayBtn');
						//查找与当前医生相关的图文咨询订单信息
						chat.getImgConsultNumber();
					}
					//私人医生
					if (detail.isOpenPmd == false) {
						$('.to_pmd').addClass('grayBtn');
					} else {
						$('.to_pmd').removeClass('grayBtn');
						chat.getPmdConsultNumber();
					}
				}
			})
		},
		//查询图文咨询订单数量
		getImgConsultNumber:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/trade/imageConsultOrder/countOrder',
				type:'post',
				async: false,
				contentType: 'application/json',
				data:{
					condition:{
						serviceStatusList:['3','10'],
						doctorId:parseInt(chat.doctorId)
					}
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var count = res.content.totalCount;
						if(count != 0){
							//medtap.pushWindow('myOrder.html?orderType=TWZX');//跳转至图文咨询订单列表
							$('.to_img_consult').unbind().bind('click',function(){
								medtap.pushWindow('myOrder.html?orderType=TWZX&wechatId=' + medtap.getRequest('wechatId'));
							})
						}else{
							$('.to_img_consult').unbind().bind('click',function(){
								medtap.pushWindow('../../wechat_lung_imgConsult/index.html?doctorId=' + chat.doctorId + '&pmdOrderSn=' + medtap.getRequest('pmdSn') + '&wechatId=' + medtap.getRequest('wechatId'));
							})
						}
					}
				}
			})
		},
		//查询电话咨询订单数量
		getConsultNumber:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/trade/consult/countOrder',
				type:'post',
				async: false,
				contentType: 'application/json',
				data:{
					condition:{
						serviceStatusList:['3','10'],
						doctorId:parseInt(chat.doctorId)
					}
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var count = res.content.totalCount;
						if(count != 0){
							//跳转至电话咨询订单列表
							$('.to_consult').unbind().bind('click',function(){
								medtap.pushWindow('myOrder.html?orderType=DHZX&wechatId=' + medtap.getRequest('wechatId'));
							})
						}else{
							$('.to_consult').unbind().bind('click',function(){
								medtap.pushWindow('../../wechat_lung_consult/rukou.html?doctorId=' + chat.doctorId + '&pmdOrderSn=' + medtap.getRequest('pmdSn') + '&doctorname=' + chat.doctorName + '&wechatId=' + medtap.getRequest('wechatId'))
							})
						}
					}
				}
			})
		},
		//查询私人医生订单数量
		getPmdConsultNumber:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/trade/pmdConsultOrder/countOrder',
				type:'post',
				async: false,
				contentType: 'application/json',
				data:{
					condition:{
						serviceStatusList:['3','10'],
						doctorId:parseInt(chat.doctorId)
					}
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var count = res.content.totalCount;
						if(count != 0){
							//跳转至私人医生订单列表
							$('.to_pmd').unbind().bind('click',function(){
								medtap.pushWindow('myOrder.html?orderType=PMD&wechatId=' + medtap.getRequest('wechatId'));
							})
						}else{
							$('.to_pmd').unbind().bind('click',function(){
								medtap.pushWindow('../../wechat_lung_prvivateDoctor/index.html?doctorId=' + chat.doctorId + '&wechatId=' + medtap.getRequest('wechatId'));
							})
						}
					}
				}
			})
		}
	};
	chat.init();
	chat.choseImage();
	chat.previewImg();	
});
