define(
	[ 'zepto', 'medtap' /* ,'https://review-formal.iplusmed.com/Common/javaScript/beforeSubmitForLung.js?v=2.0' */ ],
	function($, medtap /* ,beforeSubmit */) {
		var index = {
			wechatId: medtap.getRequest('wechatId'),
			groupId: 0,
			limit: 10,
			offset: 1,
			hasMore: false,
			firstLoad: false,
			init: function() {
				index.getUserToken();
			},
			bindEve: function() {
				// 浮动菜单切换
				$('.more').on('click', function() {
					$(this).hide();
					$('.my-text').show();
					$('.release').show();
					$('.close').show();
				});
				$('.close').on('click', function() {
					$(this).hide();
					$('.my-text').hide();
					$('.release').hide();
					$('.more').show();
				});
				// 浮动菜单点击跳转
				// 点击我的图标跳转到我的页面
				$('.my-text').on('click', function() {
					medtap.pushWindow('pages/myCardList.html');
				});
				// 点击发布图标跳转到发布页面
				$('.release').on('click', function() {
					medtap.pushWindow("pages/newCard.html?groupId="+ index.groupId +"")
				});
				$('.user_nickName').on('click', function() {
					medtap.pushWindow('pages/myCardList.html');
				});

				//取消
				$('.change_name_warp').on('click', '.change_btn_cancle', function() {
					$('.content_mod').hide();
					$('.change_name_warp').hide();
					$('.new_nick').val('');
				});

				//确定
				$('.change_name_warp').on('click', '.change_btn_confirm', function() {
					var newNick = $('.new_nick').val();
					index.upDateUserInfo(newNick);
				});

				//tab分类查询帖子列表
				$('.silder_tap_item').on('click', function() {
					$(this).addClass('on');
					$(this).siblings().removeClass('on');
					var type = '';
					var id = $(this).attr('id');
					if (id == 'recommend') {
						$('.list_warp').html('');
						index.firstLoad = false;
						index.offset = 1;
						type = 1;
					} else if (id == 'newest') {
						$('.list_warp').html('');
						index.firstLoad = false;
						index.offset = 1;
						type = '';
					}
					index.getForumList(type);
				});

				//add

				//滚动加载
				$(window).scroll(function() {
					var scrollTop = $(this).scrollTop(); //滚动条距离顶部的高度
					var scrollHeight = $(document).height(); //当前页面的总高度
					var clientHeight = $(this).height(); //当前可视的页面高度
					if (scrollTop + clientHeight >= scrollHeight) {
						//距离顶部+当前高度 >=文档总高度 即代表滑动到底部
						//滚动条到达底部
						if (index.hasMore == true) {
							var type = '';
							if ($('#recommend').hasClass('on')) {
								index.firstLoad = true;
								index.offset += 1;
								type = 1;
							} else if ($('#newest').hasClass('on')) {
								index.firstLoad = true;
								index.offset += 1;
								type = '';
							}
							index.getForumList(type);
						} else {
						}
					} else if (scrollTop <= 0) {
						//滚动条到达顶部
						return;
					}
				});
			},
			//换取微信token
			getUserToken: function() {
				localStorage.wxLungToken = '';
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/user/auth/token',
					type: 'POST',
					async: false,
					contentType: 'application/json',
					data: {
						wechatId: index.wechatId
					},
					success: function(res) {
						medtap.loading(0);
						if (res.success == true) {
							var token = res.content.token;
							localStorage.setItem('wxLungToken', token);
							index.getForumInfo();
							index.getUserInfo();
						} else {
						}
					},
					error: function(res) {
						console.log(res.resultDesc);
					}
				});
			},
			//获取用户信息
			getUserInfo: function() {
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/user/getUserDetail',
					type: 'GET',
					async: false,
					data: {},
					success: function(res) {
						medtap.loading(0);
						var userInfo = res.content.userInfo;
						$('.user_nickName').html(userInfo.nickname);
						$('.change_name_nickname').html(userInfo.nickname);
					}
				});
			},
			//更新用户信息
			upDateUserInfo: function(name) {
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/user/saveUserInfo',
					type: 'POST',
					async: false,
					contentType: 'application/json',
					data: {
						nickname: name
					},
					success: function(res) {
						medtap.loading(0);
						setTimeout(function() {
							medtap.toast({
								message: '修改成功',
								time: 2000
							});
							index.getUserInfo();
							$('.content_mod').hide();
							$('.change_name_warp').hide();
							$('.new_nick').val('');
						}, 1000);
					}
				});
			},
			//获取论坛信息
			getForumInfo: function() {
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/operation/pubbbs/getGruop',
					type: 'POST',
					async: false,
					contentType: 'application/json',
					data: {
						codeVal: 'LUNG'
					},
					success: function(res) {
						medtap.loading(0);
						if (res.success == true) {
							var forumInfo = res.content.publishGroup;
							index.groupId = forumInfo.id;
							$('.bbs_title').html(forumInfo.name); //社区名
							$('.bbs_profile_img').attr('src', forumInfo.icon); //社区icon
							$('.bbs_attentionNum').html(forumInfo.userCount); //社区关注人数
							$('.bbs_cardNum').html(forumInfo.publishCount); //社区帖子数量
							index.getForumList(1);
							$('.add').unbind().bind('click', function() {
								medtap.pushWindow('pages/newCard.html?groupId=' + index.groupId);
							});
							index.joinPublish();
						}
					}
				});
			},
			//根据分类获取论坛信息
			getForumList: function(listType) {
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/operation/pubbbs/publish',
					type: 'GET',
					async: false,
					data: {
						offset: index.offset,
						limit: index.limit,
						good: listType,
						groupId: index.groupId
					},
					success: function(res) {
						medtap.loading(0);
						if (res.success == true) {
							var forumList = res.content.list;
							if (forumList.length == 0) {
								index.hasMore = false;
								setTimeout(function() {
									medtap.toast({
										message: '没有更多了',
										time: 2000
									});
								}, 1000);
							} else {
								index.hasMore = true;
								//dealData
								var str = '';
								for (var i = 0; i < forumList.length; i++) {
									//当前帖子用户未点赞
									if (forumList[i].upStatus == 0) {
										var picUrl = forumList[i].picUrls || '';
										//单张图片
										if (picUrl.length != 0 && picUrl.length < 2) {
											str +=
												'<div class="list_item" data-id="' +
												forumList[i].id +
												'">' +
												'<div class="list_item_header clearfix">' +
												'<img src="' +
												forumList[i].profile +
												'" class="user_profile">' +
												'<div class="user_info">' +
												'<p class="user_name">' +
												forumList[i].nickname +
												'</p>' +
												'<p class="txt_gray post_card_time">' +
												forumList[i].updateTime.substring(5, 16) +
												'</p>' +
												'</div>' +
												'</div>' +
												'<div class="list_item_content">' +
												'<div class="list_item_content_desc">' +
												forumList[i].content +
												'</div>' +
												'<div class="list_item_content_img">' +
												'<img src="' +
												picUrl +
												'">' +
												'</div>' +
												'</div>' +
												'<div class="list_item_tools clearfix">' +
												'<div class="tools clearfix seen">' +
												'<img src="images/post_seen_png@2x.png">' +
												'<span class="seenNum">' +
												forumList[i].viewCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix reply">' +
												'<img src="images/post_reply_png@2x.png">' +
												'<span class="replyNum">' +
												forumList[i].commentCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix like">' +
												'<img src="images/post_unlike_png@2x.png">' +
												'<span class="likeNum">' +
												forumList[i].upCount +
												'</span>' +
												'</div>' +
												'</div>' +
												'</div>';
										} else if (picUrl.length > 1) {
											//多张图片
											var imgStr = '';
											for (var j = 0; j < picUrl.length; j++) {
												imgStr += '<img src="' + picUrl[j] + '">';
											}
											str +=
												'<div class="list_item" data-id="' +
												forumList[i].id +
												'">' +
												'<div class="list_item_header clearfix">' +
												'<img src="' +
												forumList[i].profile +
												'" class="user_profile">' +
												'<div class="user_info">' +
												'<p class="user_name">' +
												forumList[i].nickname +
												'</p>' +
												'<p class="txt_gray post_card_time">' +
												forumList[i].updateTime.substring(5, 16) +
												'</p>' +
												'</div>' +
												'</div>' +
												'<div class="list_item_content">' +
												'<div class="list_item_content_desc">' +
												forumList[i].content +
												'</div>' +
												'<div class="list_item_content_imgMore clearfix">' +
												imgStr +
												'</div>' +
												'<div class="list_item_tools clearfix">' +
												'<div class="tools clearfix seen">' +
												'<img src="images/post_seen_png@2x.png">' +
												'<span class="seenNum">' +
												forumList[i].viewCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix reply">' +
												'<img src="images/post_reply_png@2x.png">' +
												'<span class="replyNum">' +
												forumList[i].commentCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix like">' +
												'<img src="images/post_unlike_png@2x.png">' +
												'<span class="likeNum">' +
												forumList[i].upCount +
												'</span>' +
												'</div>' +
												'</div>' +
												'</div>' +
												'</div>';
										} else {
											str +=
												'<div class="list_item" data-id="' +
												forumList[i].id +
												'">' +
												'<div class="list_item_header clearfix">' +
												'<img src="' +
												forumList[i].profile +
												'" class="user_profile">' +
												'<div class="user_info">' +
												'<p class="user_name">' +
												forumList[i].nickname +
												'</p>' +
												'<p class="txt_gray post_card_time">' +
												forumList[i].updateTime.substring(5, 16) +
												'</p>' +
												'</div>' +
												'</div>' +
												'<div class="list_item_content">' +
												'<div class="list_item_content_desc">' +
												forumList[i].content +
												'</div>' +
												'</div>' +
												'<div class="list_item_tools clearfix">' +
												'<div class="tools clearfix seen">' +
												'<img src="images/post_seen_png@2x.png">' +
												'<span class="seenNum">' +
												forumList[i].viewCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix reply">' +
												'<img src="images/post_reply_png@2x.png">' +
												'<span class="replyNum">' +
												forumList[i].commentCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix like">' +
												'<img src="images/post_unlike_png@2x.png">' +
												'<span class="likeNum">' +
												forumList[i].upCount +
												'</span>' +
												'</div>' +
												'</div>' +
												'</div>';
										}
									} else if (forumList[i].upStatus == 1) {
										var picUrl = forumList[i].picUrls || '';
										//单张图片
										if (picUrl.length != 0 && picUrl.length < 2) {
											str +=
												'<div class="list_item" data-id="' +
												forumList[i].id +
												'">' +
												'<div class="list_item_header clearfix">' +
												'<img src="' +
												forumList[i].profile +
												'" class="user_profile">' +
												'<div class="user_info">' +
												'<p class="user_name">' +
												forumList[i].nickname +
												'</p>' +
												'<p class="txt_gray post_card_time">' +
												forumList[i].updateTime.substring(5, 16) +
												'</p>' +
												'</div>' +
												'</div>' +
												'<div class="list_item_content">' +
												'<div class="list_item_content_desc">' +
												forumList[i].content +
												'</div>' +
												'<div class="list_item_content_img">' +
												'<img src="' +
												picUrl +
												'">' +
												'</div>' +
												'</div>' +
												'<div class="list_item_tools clearfix">' +
												'<div class="tools clearfix seen">' +
												'<img src="images/post_seen_png@2x.png">' +
												'<span class="seenNum">' +
												forumList[i].viewCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix reply">' +
												'<img src="images/post_reply_png@2x.png">' +
												'<span class="replyNum">' +
												forumList[i].commentCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix like">' +
												'<img src="images/post_like_png@2x.png">' +
												'<span class="likeNum">' +
												forumList[i].upCount +
												'</span>' +
												'</div>' +
												'</div>' +
												'</div>';
										} else if (picUrl.length > 1) {
											//多张图片
											var imgStr = '';
											for (var j = 0; j < picUrl.length; j++) {
												imgStr += '<img src="' + picUrl[j] + '">';
											}
											str +=
												'<div class="list_item" data-id="' +
												forumList[i].id +
												'">' +
												'<div class="list_item_header clearfix">' +
												'<img src="' +
												forumList[i].profile +
												'" class="user_profile">' +
												'<div class="user_info">' +
												'<p class="user_name">' +
												forumList[i].nickname +
												'</p>' +
												'<p class="txt_gray post_card_time">' +
												forumList[i].updateTime.substring(5, 16) +
												'</p>' +
												'</div>' +
												'</div>' +
												'<div class="list_item_content">' +
												'<div class="list_item_content_desc">' +
												forumList[i].content +
												'</div>' +
												'<div class="list_item_content_imgMore clearfix">' +
												imgStr +
												'</div>' +
												'<div class="list_item_tools clearfix">' +
												'<div class="tools clearfix seen">' +
												'<img src="images/post_seen_png@2x.png">' +
												'<span class="seenNum">' +
												forumList[i].viewCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix reply">' +
												'<img src="images/post_reply_png@2x.png">' +
												'<span class="replyNum">' +
												forumList[i].commentCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix like">' +
												'<img src="images/post_like_png@2x.png">' +
												'<span class="likeNum">' +
												forumList[i].upCount +
												'</span>' +
												'</div>' +
												'</div>' +
												'</div>' +
												'</div>';
										} else {
											str +=
												'<div class="list_item" data-id="' +
												forumList[i].id +
												'">' +
												'<div class="list_item_header clearfix">' +
												'<img src="' +
												forumList[i].profile +
												'" class="user_profile">' +
												'<div class="user_info">' +
												'<p class="user_name">' +
												forumList[i].nickname +
												'</p>' +
												'<p class="txt_gray post_card_time">' +
												forumList[i].updateTime.substring(5, 16) +
												'</p>' +
												'</div>' +
												'</div>' +
												'<div class="list_item_content">' +
												'<div class="list_item_content_desc">' +
												forumList[i].content +
												'</div>' +
												'</div>' +
												'<div class="list_item_tools clearfix">' +
												'<div class="tools clearfix seen">' +
												'<img src="images/post_seen_png@2x.png">' +
												'<span class="seenNum">' +
												forumList[i].viewCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix reply">' +
												'<img src="images/post_reply_png@2x.png">' +
												'<span class="replyNum">' +
												forumList[i].commentCount +
												'</span>' +
												'</div>' +
												'<div class="tools clearfix like">' +
												'<img src="images/post_like_png@2x.png">' +
												'<span class="likeNum">' +
												forumList[i].upCount +
												'</span>' +
												'</div>' +
												'</div>' +
												'</div>';
										}
									}
								}
								if (index.offset > 1) {
									$('.list_warp').append(str);
								} else {
									$('.list_warp').html(str);
								}
								$('.like').on('click', function(event) {
									event.stopPropagation();
									var id = $(this).parents('.list_item').attr('data-id');
									var num = $(this).children('.likeNum').html();
									var status = index.publishUp(id);
									if (status == 0) {
										var newNum = parseInt(num) - 1;
										$(this).children('img').attr('src', 'images/post_unlike_png@2x.png');
										$(this).children('.likeNum').html(newNum);
									} else if (status == 1) {
										var newNum = parseInt(num) + 1;
										$(this).children('img').attr('src', 'images/post_like_png@2x.png');
										$(this).children('.likeNum').html(newNum);
									}
								});

								$('.list_item').on('click', function() {
									var id = $(this).attr('data-id');
									medtap.pushWindow(
										'pages/cardDetail.html?cardId=' + id + '&wechatId=' + index.wechatId
									);
								});
							}
						}
					}
				});
			},
			//点赞
			publishUp: function(publishId) {
				var status;
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/operation/pubbbs/publish/up',
					type: 'POST',
					async: false,
					contentType: 'application/json',
					data: {
						publishId: publishId
					},
					success: function(res) {
						medtap.loading(0);
						if (res.success == true) {
							status = res.content.upStatus;
						}
					}
				});
				return status;
			},
			//加入社区(便于后台统计人数)
			joinPublish: function() {
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/operation/pubbbs/groupUser',
					type: 'POST',
					async: false,
					contentType: 'application/json',
					data: {
						groupId: index.groupId
					},
					success: function(res) {
						medtap.loading(0);
					}
				});
			}
		};
		index.init();
		index.bindEve();
	}
);
