define(['zepto', 'medtap'], function($, medtap) {
	var reply = {
		commentId: medtap.getRequest('commentId'),
		publishId:'',
		init: function() {
			reply.getDetail();
			$('input').on('blur',function(){
				$('body').scrollTop(0);
			});
			$('.reply_btn').on('click',function(){
				reply.replyThisComment();
			})
		},
		getDetail: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://gateway.medtap.cn/operation/pubbbs/publishComment/' + reply.commentId,
				type: 'GET',
				async: false,
				data: {

				},
				success: function(res) {
					medtap.loading(0);
					var replyDetail = res.content.publishComment;
					reply.publishId = replyDetail.publishId;
					//reply.commentId = replyDetail.commentId;
					$('.user_profile').attr('src', replyDetail.profile);
					$('.reply_content_username').html(replyDetail.nickname)
					$('.reply_content_time').html(replyDetail.updateTime.substring(5, 16));
					$('.reply_like_num').html(replyDetail.upCount);
					$('.reply_content_desc').html(replyDetail.content);
					if (replyDetail.upStatus == 0) {
						$('.reply_content_btn img').attr('src', '../images/post_unlike_png@2x.png');
					} else if (replyDetail.upStatus == 1) {
						$('.reply_content_btn img').attr('src', '../images/post_like_png@2x.png');
					}
					$('.like').on('click', function() {
						var rs = reply.likeThisReply();
						if (rs == 0) {
							$('.reply_content_btn img').attr('src', '../images/post_unlike_png@2x.png');
							$('.reply_like_num').html(parseInt(replyDetail.upCount));
						} else if (rs == 1) {
							$('.reply_like_num').html(parseInt(replyDetail.upCount) + 1);
							$('.reply_content_btn img').attr('src', '../images/post_like_png@2x.png');
						}
					});
					if(replyDetail.hasOwnProperty('commentList')){
						var commentList = replyDetail.commentList;
						$('.reply_list_header_num').html(commentList.length + '条')
						var commentStr = '';
						for(var i = 0;i < commentList.length;i++){
							commentStr += '<div class="reply_list_item">'+
											'<div class="reply_list_item_header clearfix">'+
												'<img src="'+commentList[i].profile+'" class="reply_user_profile">'+
												'<div class="reply_user_name">'+
													commentList[i].nickname+
												'</div>'+
											'</div>'+
											'<div class="reply_list_item_content">'+
												commentList[i].content+
											'</div>'+
											'<div class="reply_list_item_time">'+
												commentList[i].updateTime.substring(5,16)+
											'</div>'+
										'</div>';
						}
					}else{
						$('.reply_list_header_num').html('0条')
					}
					$('.reply_list').append(commentStr);
				}
			})
		},
		//点赞当前回复
		likeThisReply: function() {
			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: reply.commentId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						result = res.content.upStatus;
					}
				}
			});
			return result;
		},
		replyThisComment:function(){
			localStorage.needReload = '';
			if(!$('.reply_input').val()){
				medtap.toast({
					message:'请输入回复内容',
					time:1500
				})
				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:reply.publishId,
						commentId:reply.commentId
					},
					success:function(res){
						medtap.loading(0);
						if(res.success == true){
							setTimeout(function(){
								medtap.toast({
									message:'回复成功~',
									time:1500
								})
								setTimeout(function(){
									localStorage.setItem('needReload','yes');
									window.location.reload();
								},1500)
							},1500)
						}
					}
				})
			}
		}
	}

	reply.init();
})
