define(['zepto', 'medtap'], function($, medtap) {
	var card = {
		cardId: medtap.getRequest('cardId'),
		limit: 10,
		offset: 1,
		hasMore: false,
		firstLoad: false,
		upCount: 0,
		agencyId: '',
		shareId: medtap.getRequest('agencyId'),
		// 贴子审批状态
		status: medtap.getRequest('status')|| '1',
		init: function() {
			card.getShareInfoByToken();
			card.getCardDetail();
			card.getCommentList();
			card.bindEve();
			card.getWechatTicket();
			card.previewImage();
			//滚动加载
			$(window).scroll(function() {
				var scrollTop = $(this).scrollTop(); //滚动条距离顶部的高度
				var scrollHeight = $(document).height(); //当前页面的总高度
				var clientHeight = $(this).height(); //当前可视的页面高度
				if (scrollTop + clientHeight >= scrollHeight) { //距离顶部+当前高度 >=文档总高度 即代表滑动到底部 
					//滚动条到达底部
					if (card.hasMore == true) {
						card.offset += 1;
						card.getCommentList();
					} else {

					}
				} else if (scrollTop <= 0) {
					//滚动条到达顶部
					return
				}
			});
			$('input').on('blur', function() {
				$('body').scrollTop(0);
			});

			$('.btn_do_comment').on('click', function() {
				$('.content_mod').show();
				$('.comment_btn').show();
				$('.reply_input').focus();
				$('.content_mod').on('click', function() {
					$(this).hide();
					$('.comment_btn').hide();
				});
				$('.reply_btn').on('click', function() {
					card.replyPublish();
				})
			});

			//share
			$('.share').on('click', function() {
				$('.share_function').show();
				$('.close_share').on('click', function() {
					$('.share_function').hide();
				})
			});
			window.onload = function(){
				if(localStorage.needReload && localStorage.needReload == 'yes'){
					localStorage.needReload = 'no';
					window.location.reload();
				}
			}
		},
		isShow:function(){
			if(card.status=='1'){
				$('.card_content_header').addClass('card_show')
				$('.card_tools').addClass('card_show')
				$('.comment_warp').addClass('card_show')
				$('.footer_btn').addClass('card_show')
				$('.card_content .fabulous').show()
			}else{
				$('.card_content .fabulous').addClass('card_hide')
			}
		},
		bindEve: function() {
			//为TA加油
			$('.fabulous').on('click', function() {
				var status = card.publishUp(card.cardId);
				if (status == 1) {
					$(this).hide();
					$('.unFabulous').show();
					card.getCardDetail();
					var newNum = parseInt(card.upCount);
					$('.unFabulous').children('.fabulous_desc').html(newNum);
				}
			});
			//取消点赞
			$('.unFabulous').on('click', function() {
				var status = card.publishUp(card.cardId);
				if (status == 0) {
					$(this).hide();
					$('.fabulous').show();
					$('.fabulous').children('.fabulous_desc').html('为TA加油');
				}
			});
			//收藏
			$('.collect').on('click', function() {
				var rs = card.publishCollect();
				var _this = this;
				if (rs == 0) {
					$(this).children('img').attr('src', '../images/post_like_buttn@2x.png');
				} else if (rs == 1) {
					setTimeout(function() {
						medtap.toast({
							message: '收藏成功',
							time: 2000
						});
						$(_this).children('img').attr('src', '../images/post_like red_buttn@2x.png');
					}, 1000)
				}
			})
		},
		getWechatTicket: function() {
			medtap.submitAjax({
				url: 'https://gateway.medtap.cn/wechat/lung/fetchWechatTicket',
				type: 'GET',
				async: false,
				data: {
					url: window.location.href
				},
				success: function(res) {
					var data = res.content;
					wx.config({
						debug: false,
						appId: data.appId, // 必填，公众号的唯一标识
						timestamp: data.timestamp, // 必填，生成签名的时间戳
						nonceStr: data.nonceStr, // 必填，生成签名的随机串
						signature: data.signature, // 必填，签名，见附录1
						jsApiList: [
							'previewImage',
							'onMenuShareTimeline',
							'onMenuShareAppMessage'
						]
					})
					card.share();
				}
			})
		},
		share: function() {
			var desc = $('.cardDetail').text().substring(0,20)
			//微信分享
			wx.ready(function() {
				//分享给朋友
				wx.onMenuShareAppMessage({
					title: '肺癌靶向家园',
					desc: desc+'...', // 分享描述
					link: 'https://gateway.medtap.cn/wechat/lung/oauth_stepEnd?type=BBS_LUNG_SHARE&cardId=' + card.cardId +
						'&agencyId=' + card.shareId,
					// link: window.location.href,
					imgUrl: 'https://review-formal.iplusmed.com/Common/images/bbs_logo_icon/logo_feiai_new.jpg', // 分享图标
					type: 'link', // 分享类型,music、video或link，不填默认为link
					dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
					success: function() {
					
					},
					cancel: function() {
					
					}
				});
				//分享到朋友圈
				wx.onMenuShareTimeline({
					title: '肺癌靶向家园', // 分享标题
					link: 'https://gateway.medtap.cn/wechat/lung/oauth_stepEnd?type=BBS_LUNG_SHARE&cardId=' + card.cardId +
						'&agencyId=' + card.shareId,
					// link: window.location.href,
					imgUrl: 'https://review-formal.iplusmed.com/Common/images/bbs_logo_icon/logo_feiai_new.jpg', // 分享图标
					success: function() {
					
					},
					cancel: function() {
					
					}
				});
			});
		},
		previewImage: function() {
			$('.cardImage').on('click', 'img', function() {
				var imgArray = [];
				var curImageSrc = $(this).attr('src');
				if (curImageSrc) {
					$('.cardImage img').each(function(index, el) {
						var itemSrc = $(this).attr('src');
						imgArray.push(itemSrc);
					});
					wx.previewImage({
						current: curImageSrc,
						urls: imgArray
					});
				}
			});
		},
		getCardDetail: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://gateway.medtap.cn/operation/pubbbs/publish/' + card.cardId,
				type: 'GET',
				async: false,
				data: {

				},
				success: function(res) {
					console.log('帖子详情',res)
					medtap.loading(0);
					if (res.success == true) {
						var detailInfo = res.content.publish;
						$('.user_name').html(detailInfo.nickname);
						$('.card_time').html(detailInfo.updateTime.substring(5, 16));
						$('.user_profile').attr('src', detailInfo.profile);
						$('.cardDetail').html(detailInfo.content);
						$('.seenNum').html(detailInfo.viewCount);
						$('.replyNum').html(detailInfo.commentCount);
						$('.btn_icon_commentNum').html(detailInfo.commentCount)
						card.upCount = detailInfo.upCount;
						if (detailInfo.picUrls.length != 0) {
							var imgHtml = ''
							for (var i = 0; i < detailInfo.picUrls.length; i++) {
								imgHtml += '<img src="' + detailInfo.picUrls[i] + '" >'
							}
							$('.cardImage').html(imgHtml);
						}
						//点赞状态
						if (detailInfo.upStatus == 0) {
							$('.fabulous').show();
							$('.fabulous_desc').html('为TA加油');
							$('.unFabulous').hide();
						} else if (detailInfo.upStatus == 1) {
							$('.fabulous').hide();
							$('.unFabulous').show();
							var newNum = parseInt(card.upCount);
							$('.fabulous_desc').html(newNum);
						}
						//收藏状态
						if (detailInfo.collectStatus == 0) {
							$('.collect').children('img').attr('src', '../images/post_like_buttn@2x.png');
						} else if (detailInfo.collectStatus == 1) {
							$('.collect').children('img').attr('src', '../images/post_like red_buttn@2x.png');
						}
					}else{
						medtap.winPop('该贴已被删除')
					}
				}
			})
		},
		//获取评论列表
		getCommentList: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://gateway.medtap.cn/operation/pubbbs/publishComment',
				type: 'GET',
				async: false,
				data: {
					offset: card.offset,
					limit: card.limit,
					publishId: card.cardId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var replyHtml = '';
						$('.comment_header_num').html(res.content.count + '条');
						var replyList = res.content.list;
						if (replyList.length == 0) {
							card.hasMore = false;
							if (card.offset != 1) {
								setTimeout(function() {
									medtap.toast({
										message: '没有更多了',
										time: 2000
									})
								}, 1000);
							} else {

							}
						} else {
							card.hasMore = true;
							for (var i = 0; i < replyList.length; i++) {
								if (replyList[i].hasOwnProperty('commentList')) {
									var secReplyNum = parseInt(replyList[i].commentList.length);
									var commentList = replyList[i].commentList;
									if (replyList[i].upStatus == 0) {
										replyHtml += '<div class="comment_item hasReply" data-id="' + replyList[i].id + '">' +
											'<div class="comment_item_header clearfix">' +
											'<img src="' + replyList[i].profile + '" >' +
											'<div class="comment_user_name">' +
											replyList[i].nickname +
											'</div>' +
											'<div class="comment_tools_fabulous clearfix">' +
											'<img src="../images/post_unlike_png@2x.png" >' +
											'<span class="fabulous_num">' + replyList[i].upCount + '</span>' +
											'</div>' +
											'</div>' +
											'<div class="comment_content">' +
											replyList[i].content +
											'</div>' +
											'<div class="comment_info">' +
											'<span class="comment_time txt_gray">' + replyList[i].updateTime.substring(5, 16) + '</span>' +
											'<span class="comment_reply_btn txt_blue">回复</span>' +
											'</div>' +
											'<div class="comment_reply"  data-commentId="' + commentList[0].id + '">' +
											'<span class="comment_reply_name">' + commentList[0].nickname + '：</span>' +
											'<span class="comment_reply_content">' + commentList[0].content + '</span>' +
											'<span class="comment_reply_time">' + commentList[0].updateTime.substring(5, 16) + '</span>' +
											'<div class="comment_reply_more">查看全部' +
											secReplyNum +
											'条回复</div>' +
											'</div>' +
											'</div>';
									} else if (replyList[i].upStatus == 1) {
										replyHtml += '<div class="comment_item hasReply" data-id="' + replyList[i].id + '">' +
											'<div class="comment_item_header clearfix">' +
											'<img src="' + replyList[i].profile + '" >' +
											'<div class="comment_user_name">' +
											replyList[i].nickname +
											'</div>' +
											'<div class="comment_tools_fabulous clearfix">' +
											'<img src="../images/post_like_png@2x.png" >' +
											'<span class="fabulous_num">' + replyList[i].upCount + '</span>' +
											'</div>' +
											'</div>' +
											'<div class="comment_content">' +
											replyList[i].content +
											'</div>' +
											'<div class="comment_info">' +
											'<span class="comment_time txt_gray">' + replyList[i].updateTime.substring(5, 16) + '</span>' +
											'<span class="comment_reply_btn txt_blue">回复</span>' +
											'</div>' +
											'<div class="comment_reply"  data-commentId="' + commentList[i].id + '">' +
											'<span class="comment_reply_name">' + commentList[i].nickname + '</span>' +
											'<span class="comment_reply_content">' + commentList[i].content + '</span>' +
											'<span class="comment_reply_time">' + commentList[i].updateTime.substring(5, 16) + '</span>' +
											'<div class="comment_reply_more">查看全部' +
											secReplyNum +
											'条回复</div>' +
											'</div>' +
											'</div>';
									}
								} else {
									if (replyList[i].upStatus == 0) {
										replyHtml += '<div class="comment_item noReply" data-id="' + replyList[i].id + '">' +
											'<div class="comment_item_header clearfix">' +
											'<img src="' + replyList[i].profile + '" >' +
											'<div class="comment_user_name">' +
											replyList[i].nickname +
											'</div>' +
											'<div class="comment_tools_fabulous clearfix">' +
											'<img src="../images/post_unlike_png@2x.png" >' +
											'<span class="fabulous_num">' + replyList[i].upCount + '</span>' +
											'</div>' +
											'</div>' +
											'<div class="comment_content">' +
											replyList[i].content +
											'</div>' +
											'<div class="comment_info">' +
											'<span class="comment_time txt_gray">' + replyList[i].updateTime.substring(5, 16) + '</span>' +
											'<span class="comment_reply_btn txt_blue">回复</span>' +
											'</div>' +
											'</div>';
									} else if (replyList[i].upStatus == 1) {
										replyHtml += '<div class="comment_item noReply" data-id="' + replyList[i].id + '">' +
											'<div class="comment_item_header clearfix">' +
											'<img src="' + replyList[i].profile + '" >' +
											'<div class="comment_user_name">' +
											replyList[i].nickname +
											'</div>' +
											'<div class="comment_tools_fabulous clearfix">' +
											'<img src="../images/post_like_png@2x.png" >' +
											'<span class="fabulous_num">' + replyList[i].upCount + '</span>' +
											'</div>' +
											'</div>' +
											'<div class="comment_content">' +
											replyList[i].content +
											'</div>' +
											'<div class="comment_info">' +
											'<span class="comment_time txt_gray">' + replyList[i].updateTime.substring(5, 16) + '</span>' +
											'<span class="comment_reply_btn txt_blue">回复</span>' +
											'</div>' +
											'</div>';
									}
								}
							}
						}
						if (card.offset > 1) {
							$('.comment_list').append(replyHtml);
						} else {
							$('.comment_list').html(replyHtml);
						}
						//评论点赞
						$('.comment_item .comment_tools_fabulous').on('click', function(event) {
							event.stopPropagation();
							var id = $(this).parents('.comment_item').attr('data-id');
							var num = $(this).children('.fabulous_num').html();
							var commentResult = card.commentUp(id);
							if (commentResult == 0) {
								var newNum = parseInt(num) - 1;
								$(this).children('img').attr('src', '../images/post_unlike_png@2x.png');
								$(this).children('.fabulous_num').html(newNum);
							} else if (commentResult == 1) {
								var newNum = parseInt(num) + 1;
								$(this).children('img').attr('src', '../images/post_like_png@2x.png');
								$(this).children('.fabulous_num').html(newNum);
							}
						});
						//跳转至评论详情
						$('.comment_item').on('click', function() {
							var id = $(this).attr('data-id');
							medtap.pushWindow('replyDetail.html?commentId=' + id);
						})
					}
				}
			})
		},
		//为TA加油
		publishUp: function(cardId) {
			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: cardId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						status = res.content.upStatus;
					}
				}
			})
			return status;
		},
		//评论点赞
		commentUp: function(id) {
			var result;
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://gateway.medtap.cn/operation/pubbbs/publishComment/up',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					commentId: id
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						result = res.content.upStatus;
					}
				}
			});
			return result;
		},
		//收藏帖子
		publishCollect: function() {
			var collectResult;
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://gateway.medtap.cn/operation/pubbbs/publishCollect',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					publishId: card.cardId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						collectResult = res.content.collectStatus;
					}
				}
			})
			return collectResult;
		},
		//回复帖子
		replyPublish: function() {
			if (!$('.reply_input').val()) {
				medtap.toast({
					message: '请输入评论内容',
					time: 2000
				})
				return
			} else {
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://gateway.medtap.cn/operation/pubbbs/publishComment',
					type: 'POST',
					async: false,
					contentType: 'application/json',
					data: {
						content: $('.reply_input').val(),
						publishId: card.cardId
					},
					success: function(res) {
						medtap.loading(0);
						if (res.success == true) {
							setTimeout(function() {
								medtap.toast({
									message: '回复成功~',
									time: 1800
								});
								setTimeout(function() {
									window.location.reload();
								}, 2000)
							}, 2000)
						} else {

						}
					}
				})
			}
		},
		getShareInfoByToken: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://gateway.medtap.cn/common/share/getSharerByToken',
				type: 'post',
				contentType: "application/json",
				async: false,
				data: {

				},
				success: function(res) {
					if (res.success == true) {
						if (res.content.clientType == 'USER') {
							return;
						} else if (res.content.clientType == 'AGENCY') {
							card.agencyId = res.content.clientId;
						} else {
							return;
						}

					}
				}
			})
		}
	}
	card.isShow();
	card.init();
})
