// $(function(){
// 	// 评论区
// 	$("#comment").on("focus",".textarea",function(){
// 		$(this).parents(".input-wrap").addClass("active_focus");
// 	})
// 	$("#comment").on("focus",".bui-box .textarea",function(){
// 		$('.c-item .J_inputBox .inputBox,.c-item .Jz_inputBox .inputBox').remove();
// 	})
// 	var html='<div class="inputBox">';
// 		html+='<div class="input-wrap">';
// 		html+='<div class="c-textarea"><textarea class="textarea" name="comment" placeholder="写下您的回复..."></textarea></div>';
// 		html+='<div class="c-action"><button type="button" class="am-btn am-btn-secondary am-btn-xs">回复</button></div>';
// 		html+='</div>';
// 		html+='</div>';
// 	// 点击一级回复
// 	$("#comment").on("click",".c-item .c-reply",function(){
// 		$(".bui-box>.input-wrap").removeClass("active_focus");
// 		var c_item=$(this).parents(".c-item");
// 		var J_inputBox=c_item.find(".J_inputBox");
// 		J_inputBox.html(html);

// 		c_item.siblings().find('.J_inputBox .inputBox').remove();
// 		$(".Jz_inputBox .inputBox").remove();
// 	})
 	// 点击二级回复
 	// $("#comment").on("click",".c-item .cz-reply",function(){
 	// 	$(".bui-box>.input-wrap").removeClass("active_focus");
 	// 	var c_item=$(this).parents(".c-reply-comment");
 	// 	var Jz_inputBox=c_item.find(".Jz_inputBox");
 	// 	Jz_inputBox.html(html);
 	// 	c_item.find('.J_inputBox .inputBox').remove();
 	// 	c_item.siblings().find('.Jz_inputBox .inputBox').remove();
 	// })

// 	// 评论的内容
// 	$("#comment").on("click",".bui-box .c-action",function(){
// 		var own_img=$(".bui-box .avatar-wrap img").attr("src");
// 		var own_textarea=$(".bui-box .textarea").val();
// 		var html_item='<li class="c-item clearfix">';
// 		html_item+='<a class="avatar-wrap" href="#" target="_blank"><img src="'+own_img+'"></a>';
// 		html_item+='<div class="c-content">';
// 		html_item+='<div class="c-user-info clearfix">';
// 		html_item+='<a href="#" class="c-user-name">admin</a><span class="c-create-time">5小时前</span>';
// 		html_item+='</div>';
// 		html_item+='<div class="c-comment-content">'+own_textarea+'</div>';
// 		html_item+='<div class="c-footer-action clearfix">';
// 		html_item+='<span class="c-reply">回复</span>';
// 		html_item+='<span class="c-digg">123&nbsp;<i class="glyphicon glyphicon-thumbs-up"></i></span>';
// 		html_item+='</div>';
// 		html_item+='<div class="J_input_1 J_inputBox"></div>';
// 		html_item+='</div>';
// 		html_item+='</li>';
// 		if(own_textarea!=""){
// 			$(".c-item-ul").prepend(html_item);
// 		}else{
// 			confirm('评论内容不能为空');
// 		}
// 		$(".bui-box>.input-wrap").removeClass("active_focus");
// 		$(".bui-box .textarea").val("");
// 	})
// 	// 一级回复
// 	$("#comment").on("click",".c-item .J_inputBox .c-action",function(){
// 		var own_img=$(".bui-box .avatar-wrap img").attr("src");
// 		var own_textarea=$(this).parents(".J_inputBox").find(".textarea").val();
// 		var html_reply_comment='<div class="c-reply-comment" style="display: block;">';
// 		html_reply_comment+='<a class="avatar-wrap" href="#" target="_blank"><img src="'+own_img+'" alt=""></a>';
// 		html_reply_comment+='<div class="c-content">';
// 		html_reply_comment+='<div class="c-user-info clearfix">';
// 		html_reply_comment+='<a href="#" class="c-user-name">admin</a><span class="c-create-time">5小时前</span>';
// 		html_reply_comment+='</div>';
// 		html_reply_comment+='<div class="c-comment-content">'+own_textarea+'</div>';
// 		html_reply_comment+='<div class="c-footer-action clearfix active-footer-action">';
// 		html_reply_comment+='<span class="cz-reply">回复</span>';
// 		html_reply_comment+='<span class="c-digg">123&nbsp;<i class="glyphicon glyphicon-thumbs-up"></i></span>';
// 		html_reply_comment+='</div>';
// 		html_reply_comment+='<div class="J_input_1 Jz_inputBox"></div>';
// 		html_reply_comment+='</div>';
// 		html_reply_comment+='</div>';
// 		if(own_textarea!=""){
// 			$(this).parents(".c-content").find(".J_inputBox").after(html_reply_comment);
// 		}else{
// 			confirm('回复内容不能为空');
// 		}
// 		$(this).parents(".c-content").find(".J_inputBox .inputBox").remove();
// 		$(this).parents(".c-content").find(".J_inputBox .textarea").val("");
// 	})
// 	// 二级回复
// 	$("#comment").on("click",".c-item .Jz_inputBox .c-action",function(){
// 		var own_img=$(".bui-box .avatar-wrap img").attr("src");
// 		var own_textarea=$(this).parents(".Jz_inputBox").find(".textarea").val();
// 		var own_textareas=$(this).parents(".c-reply-comment").find(".c-comment-content").text().split("//@admin:")[0];
// 		var html_reply_comments='<div class="c-reply-comment">';
// 		html_reply_comments+='<a class="avatar-wrap" href="#" target="_blank"><img src="'+own_img+'" alt=""></a>';
// 		html_reply_comments+='<div class="c-content">';
// 		html_reply_comments+='	<div class="c-user-info clearfix">';
// 		html_reply_comments+='	<a href="#" class="c-user-name">admin</a><span class="c-create-time">5小时前</span>';
// 		html_reply_comments+='</div>';
// 		html_reply_comments+='	<div class="c-comment-content">'+own_textarea+'//@admin：'+own_textareas+'</div>';
// 		html_reply_comments+='<div class="c-footer-action clearfix active-footer-action">';
// 		html_reply_comments+='	<span class="cz-reply">回复</span>';
// 		html_reply_comments+='	<span class="c-digg">123&nbsp;<i class="glyphicon glyphicon-thumbs-up"></i></span>';
// 		html_reply_comments+='	</div>';
// 		html_reply_comments+='<div class="J_input_1 Jz_inputBox"></div>';
// 		html_reply_comments+='	</div>';
// 		html_reply_comments+='</div>';
// 		if(own_textarea!=""){
// 			$(this).parents(".c-reply-comment").after(html_reply_comments);
// 		}else{
// 			confirm('回复内容不能为空');
// 		}
// 		$(this).parents(".c-content").find(".Jz_inputBox .inputBox").remove();
// 		$(this).parents(".c-content").find(".Jz_inputBox .textarea").val("");
// 	})
// 	// 一级回复折叠
// 	$(".c-item").each(function(index,item){
// 		if($(item).find(".c-reply-comment").length>=1){
// 			$(item).find(".c-reply-comment").hide();
// 			// 判断是否有回复,有显示回复几条以及回复下拉,反之不显示
// 			var text=$(item).find(".c-reply-count .hf").text();
// 			$(item).find(".c-reply-count").show();
// 			$(item).find(".c-reply-count .glyphicon").html('<i class="glyphicon glyphicon-menu-down" style="font-size: 14px; color: rgb(64, 101, 153);"></i>');
// 			// $(item).find(".c-reply-count").show().html('&nbsp;⋅&nbsp;'+length_+'条回复<i class="glyphicon glyphicon-menu-down" style="font-size: 14px; color: rgb(64, 101, 153);"></i>');
// 			$("#comment").on("click",".c-item .c-reply-count",function(){
// 				$(item).find(".c-reply-comment").hide();
// 				$(this).parents(".c-content").find(".c-reply-comment").toggleClass("active_content");
// 				$(this).toggleClass("active_reply-count");
// 				var flag=$(this).is(".active_reply-count");
// 				var text_=flag?"收起回复":text;
// 				var class_=flag?"glyphicon-menu-up":"glyphicon-menu-down";
// 				$(item).find(".c-reply-count").show();
// 				$(item).find(".c-reply-count .hf").text(text_);
// 				$(item).find(".c-reply-count .glyphicon").html('<i class="glyphicon '+class_+'" style="font-size: 14px; color: rgb(64, 101, 153);"></i>');
// 				// $(item).find(".c-reply-count").show().html('&nbsp;⋅&nbsp;'+text_+'<i class="glyphicon '+class_+'" style="font-size: 14px; color: rgb(64, 101, 153);"></i>');
// 			})
// 		}else{
// 			$(item).find(".c-reply-count").hide();
// 		}
// 	})
// })




$(function(){
	// 评论区
	$("#comment").on("focus",".textarea",function(){
		$(this).parents(".input-wrap").addClass("active_focus");
		$(this).css("height","110");
		$(this).parents(".input-wrap").find(".c-action").css({"position": "relative","background-color": "#f7f7f7"})
	})
	// 除文本框之外
	$('body').click(function(e){
		if(($(e.target).attr('class')!='textarea')){
			$(".textarea").css("height","30");
			$(".textarea").parents(".input-wrap").find(".c-action").css({"position": "absolute","background-color": "rgba(0,0,0,0)"})
		}
	});
	$("#comment").on("focus",".bui-box .textarea",function(){
		$('.c-item .J_inputBox .inputBox,.c-item .Jz_inputBox .inputBox').remove();
	})
	var html='<div class="inputBox">';
		html+='<div class="input-wrap">';
		html+='<div class="c-textarea"><textarea class="textarea" name="comment" placeholder="写下您的回复..."></textarea></div>';
		html+='<div class="c-action"><button type="button" class="am-btn am-btn-secondary am-btn-xs comment-submit">回复</button></div>';
		html+='</div>';
		html+='</div>';
	// 点击一级回复
	$("#comment").on("click",".c-item .c-reply",function(){
		$(".bui-box>.input-wrap").removeClass("active_focus");
		var c_item=$(this).parents(".c-item");
		var J_inputBox=c_item.find(".J_inputBox");
		J_inputBox.html(html);
		c_item.siblings().find('.J_inputBox .inputBox').remove();
		$(".Jz_inputBox .inputBox").remove();
	})
	// 点击二级回复
	$("#comment").on("click",".c-item .cz-reply",function(){
		$(".bui-box>.input-wrap").removeClass("active_focus");
		var c_item=$(this).parents(".c-reply-comment");
		var Jz_inputBox=c_item.find(".Jz_inputBox");
		Jz_inputBox.html(html);
		$(".c-item").find('.J_inputBox .inputBox').remove();
		c_item.siblings().find('.Jz_inputBox .inputBox').remove();
	})

	// 一级回复折叠
	$('#comment').on('click', '.c-reply-count',function () {
		var comment_li = $(this).parents('li')
		if($(this).find('em').length){
			$(this).html('&nbsp;⋅ <i class="hf">收起回复</i><i class="glyphicon" style="font-size: 14px; color: rgb(64, 101, 153);"><i class="glyphicon glyphicon-menu-up" style="font-size: 14px; color: rgb(64, 101, 153);"></i></i>')
			var formdata = {"theme": Number($(this).attr('theme')), 'page': 1, 'action': 'getcomment'}
			$.post('', formdata, function (data) {
				if(data.status != 0){
					alert(data.msg)
				}else{
					if(data.page == data.total_page){
						comment_li.find('.get-next-page').hide().attr('next-page', 0)
					}else{
						comment_li.find('.get-next-page').show().attr('next-page', 2)
					}
					var html = ''
					for(var i=0; i<data.comment.length; i++){
						var active_class = ''
						if(data.comment[i].already_agree == 1){
							active_class = 'active-footer-action'
						}
						var comment_html = '<div class="c-reply-comment" style="display: block;">\n' +
							'<a class="avatar-wrap" href="#" target="_blank">\n' +
							'<img src="' + data.comment[i].image +'" alt=""></a>\n' +
							'<div class="c-content">\n' +
							'<div class="c-user-info clearfix">\n' +
							'<a href="#" class="c-user-name">' + data.comment[i].nickname + '</a>\n' +
							'<span class="c-create-time">' + data.comment[i].interval + '</span>\n' +
							'</div>\n' +
							'<div class="c-comment-content"><em>' + data.comment[i].content + '</em>' + data.comment[i].touser + '</div>\n' +
							'<div class="c-footer-action clearfix '+ active_class +'">\n' +
							'<span class="cz-reply">回复</span>\n' +
							'<span class="c-digg" comment-id="' + data.comment[i].id + '">' + data.comment[i].agree + '&nbsp;<i class="glyphicon glyphicon-thumbs-up"></i></span>\n' +
							'</div>\n' +
							'<div class="J_input_1 Jz_inputBox comment-param" quote-id="' + data.comment[i].id + '" theme-id="' + data.comment[i].theme + '" level="2">\n' +
							'</div>\n' +
							'</div>\n' +
							'</div>'
						html += comment_html
					}
					comment_li.find('.comment-div-box').append(html)
				}
			}, 'json')
		}else{
			var count = Number($(this).attr('comment-count'))
			$(this).html('&nbsp;⋅ <i class="hf"><em>' + count + '</em>条回复</i><i class="glyphicon" style="font-size: 14px; color: rgb(64, 101, 153);"><i class="glyphicon glyphicon-menu-down" style="font-size: 14px; color: rgb(64, 101, 153);"></i></i>')
			comment_li.find('.get-next-page').hide()
			comment_li.find('.comment-div-box').empty()
		}
	})

	$("#comment").on('click', '.get-next-page', function () {
		var more_button = $(this)
		var comment_page = Number($(this).attr('next-page'))
		var theme_id = Number($(this).parents('li').find('.comment-param').attr('theme-id'))
		if(comment_page > 0){
			var formdata = {"theme": theme_id, 'page': comment_page, 'action': 'getcomment'}
			$.post('', formdata, function (data) {
				if(data.status != 0){
					alert(data.msg)
				}else{
					if(data.page == data.total_page){
						more_button.hide().attr('next-page', 0)
					}else{
						more_button.show().attr('next-page', comment_page + 1)
					}
					var html = ''
					for(var i=0; i<data.comment.length; i++){
						var active_class = ''
						if(data.comment[i].already_agree == 1){
							active_class = 'active-footer-action'
						}
						var comment_html = '<div class="c-reply-comment" style="display: block;">\n' +
							'<a class="avatar-wrap" href="#" target="_blank">\n' +
							'<img src="' + data.comment[i].image +'" alt=""></a>\n' +
							'<div class="c-content">\n' +
							'<div class="c-user-info clearfix">\n' +
							'<a href="#" class="c-user-name">' + data.comment[i].nickname + '</a>\n' +
							'<span class="c-create-time">' + data.comment[i].interval + '</span>\n' +
							'</div>\n' +
							'<div class="c-comment-content"><em>' + data.comment[i].content + '</em>' + data.comment[i].touser + '</div>\n' +
							'<div class="c-footer-action clearfix '+ active_class +'">\n' +
							'<span class="cz-reply">回复</span>\n' +
							'<span class="c-digg" comment-id="' + data.comment[i].id + '">' + data.comment[i].agree + '&nbsp;<i class="glyphicon glyphicon-thumbs-up"></i></span>\n' +
							'</div>\n' +
							'<div class="J_input_1 Jz_inputBox comment-param" quote-id="' + data.comment[i].id + '" theme-id="' + data.comment[i].theme + '" level="2">\n' +
							'</div>\n' +
							'</div>\n' +
							'</div>'
						html += comment_html
					}
					more_button.parents('li').find('.comment-div-box').append(html)
				}
			}, 'json')
		}
	})


	$('#comment').on('click', '.comment-submit', function () {
		var level = Number($(this).parents('.comment-param').attr('level'))
		var quote_id = $(this).parents('.comment-param').attr('quote-id')
		var theme_id = $(this).parents('.comment-param').attr('theme-id')
		var content = $(this).parent().parent().find('.textarea').val()
		var comment_submit = $(this)
		var formData = new FormData();
        formData.append("quote", quote_id);
        formData.append("theme", theme_id);
        formData.append("action", 'comment');
        formData.append("comment", content);
		$.ajax({
            scriptCharset: 'UTF-8',
            type: 'post',
            url: '',
            cache: false,
			data: formData,
            processData: false,
            contentType: false,
            dataType : "json",
            success: function(data){
                if(data.status == 0 || data.msg){
                    alert(data.msg)
                }else{
                	var image = data.comment['image']
					var nickname = data.comment[0]['nickname']
					var nickname = data.comment[0]['nickname']
					var content = data.comment[0]['content']
					var quote = data.comment[0]['quote']
					var theme = data.comment[0]['theme']
					if(level == 0){
						var html = '<li class="c-item clearfix">\n' +
							'<a class="avatar-wrap" href="#" target="_blank"><img src="'+ image +'" alt=""></a>\n' +
							'<div class="c-content">\n' +
							'<div class="c-user-info clearfix">\n' +
							'<a href="#" class="c-user-name">' + nickname +'</a><span class="c-create-time">刚刚</span>\n' +
							'</div>\n' +
							'<div class="c-comment-content">' + content +'</div>\n' +
							'<div class="c-footer-action clearfix">\n' +
							'<span class="c-reply">回复</span>\n' +
							'<span class="c-reply-count" theme="' + data.comment[0]['id'] + '" comment-count="0"></span>\n' +
							'<span class="c-digg">0&nbsp;<i class="glyphicon glyphicon-thumbs-up"></i></span>\n' +
							'</div>\n' +
							'<div class="J_input_1 J_inputBox comment-param" quote-id="' + data.comment[0]['id'] +'" theme-id="'+ data.comment[0]['id'] +'" level="1" add-comment="0">\n' +
							'</div>\n' +
							'<div class="comment-div-box">\n' +
							'</div>\n' +
							'<div><a href="javascript:void(0);" class="c-load-more get-next-page" next-page="2" style="display: none">查看更多评论</a></div>\n' +
							'</div>\n' +
							'</li>'
						$('#level-0 li').eq(0).before(html)
					}
					var to_user = ''
					if(level == 2){
						var quote_comment = comment_submit.parents('.c-reply-comment').find('.c-comment-content em').text()
						var quote_nickname = comment_submit.parents('.c-reply-comment').find('.c-user-name').text()
						to_user = '//@'+ quote_nickname +':' + quote_comment
					}
					var html = '<div class="c-reply-comment" style="display: block;">\n' +
						'<a class="avatar-wrap" href="#" target="_blank">\n' +
						'<img src="'+ image + '" alt=""></a>\n' +
						'<div class="c-content">\n' +
						'<div class="c-user-info clearfix">\n' +
						'<a href="#" class="c-user-name">' + nickname + '</a>\n' +
						'<span class="c-create-time">刚刚</span>\n' +
						'</div>\n' +
						'<div class="c-comment-content"><em>' + content + '</em>' + to_user + '</div>\n' +
						'<div class="c-footer-action clearfix">\n' +
						'<span class="cz-reply">回复</span>\n' +
						'<span class="c-digg" comment-id="' + data.comment[0]['id'] + '">0&nbsp;<i class="glyphicon glyphicon-thumbs-up"></i></span>\n' +
						'</div>\n' +
						'<div class="J_input_1 Jz_inputBox comment-param" quote-id="' + data.comment[0]['id'] + '" theme-id="' + theme +'" level="2">\n' +
						'</div>\n' +
						'</div>\n' +
						'</div>'
					if(level == 1 || level ==2){
						if(level == 1){
							console.log(comment_submit.parents('li').find('.c-reply-comment').length)
							if(comment_submit.parents('li').find('.c-reply-comment').length > 0){
								comment_submit.parents('li').find('.c-reply-comment').eq(0).before(html)
							}else{
								comment_submit.parents('li').find('.comment-div-box').append(html)
							}
						}
						if(level == 2){
							comment_submit.parents('.c-reply-comment').after(html)
						}
						var parents_li = comment_submit.parents('li')
						comment_submit.parents('.inputBox').remove()
						// 修改评论数量
						var c_count = Number(parents_li.find('.c-reply-count').attr('comment-count'))
						parents_li.find('.c-reply-count').attr('comment-count', c_count + 1)
						if(c_count > 0){
							var c_count = Number(parents_li.find('.c-reply-count').attr('comment-count'))
						}else{
							parents_li.find('.c-reply-count').html('&nbsp;⋅ <i class="hf">收起回复</i><i class="glyphicon" style="font-size: 14px; color: rgb(64, 101, 153);"><i class="glyphicon glyphicon-menu-up" style="font-size: 14px; color: rgb(64, 101, 153);"></i></i>')
							parents_li.find('.c-reply-count').addClass('active_reply-count')
						}
					}
                }
            }
        })
	})


	$('#comment').on('click', '.c-digg', function () {
	    var id = $(this).attr('comment-id')
	    var set_agree = $(this)
        $.ajax({
            scriptCharset: 'UTF-8',
            type: 'get',
            url: '/comment?action=agree&id=' + id,
            cache: false,
            processData: false,
            contentType: false,
            dataType : "json",
            success: function(data){
                if(data.status == 0){
                    alert(data.msg)
                }else{
                	set_agree.html(data.agree + '&nbsp;<i class="glyphicon glyphicon-thumbs-up">')
                	if(data.action == 'agree'){
						set_agree.parent().addClass('active-footer-action')
					}else{
						set_agree.parent().removeClass('active-footer-action')
					}

                }
            }
        })
	})


})
