<div class="comment-view">
	{% if comment_text %}
	<div class="comment-header">{{ comment_text }}</div>
	{% endif %}
	{% if not comment_list %}
	<div class="no-comment">
		<p class="text-muted small">{{ _("No comments yet. Start a new discussion.") }}</p>
	</div>
	{% endif %}

	<div itemscope itemtype="http://schema.org/UserComments" id="comment-list">
		{% for comment in comment_list %}
		{% include "templates/includes/comments/comment.html" %}
		{% endfor %}
	</div>
</div>

{% if not is_communication %}
<div class="add-comment-section">
	<div class="text-muted hidden login-required">
		<a href="/login?redirect-to={{ pathname }}">{{ _("Login to comment") }}</a>
	</div>

	<div class="comment-form-wrapper">
		<a class="add-comment btn btn-default btn-xs">{{ _("Add Comment") }}</a>
			<div style="display: none;" id="comment-form">
			<p>{{ _("Leave a Comment") }}</p>
			<div class="alert" style="display:none;"></div>
			<form>
				<fieldset>
					<div class="row {% if _login_required %}hide{% endif %}"
						style="margin-bottom: 15px;">
						<div class="col-sm-6">
							<input class="form-control" name="comment_by_fullname"
								placeholder="{{ _("Your Name") }}" type="text">
						</div>
						<div class="col-sm-6">
							<input class="form-control" name="comment_by"
								placeholder="{{ _("Your Email Address") }}" type="email">
						</div>
					</div>
					<p><textarea class="form-control" name="comment" rows=10
						placeholder="{{ _("Comment") }}"></textarea></p>
                        <button class="btn btn-primary btn-sm" id="submit-comment" style="margin-top:10px">
                            {{ _("Submit") }}</button>
				</fieldset>
			</form>
		</div>
	</div>
</div>
{% endif %}
<script>
	frappe.ready(function() {
		var login_required = {{ login_required and 1 or 0 }};

		if (login_required && !frappe.is_user_logged_in()) {
			$(".login-required, .comment-form-wrapper").toggleClass("hidden");
		}

		var n_comments = $(".comment-row").length;

		if(n_comments) {
			$(".no_comment").toggle(false);
		}
		if(n_comments > 50) {
			$(".add-comment").toggle(false)
				.parent().append("<div class='text-muted'>Comments are closed.</div>")
		}
		$(".add-comment").click(function() {
			$(this).toggle(false);
		 	$("#comment-form").toggle();
            var full_name = "", user_id = "";
            if(frappe.is_user_logged_in()) {
                full_name = getCookie("full_name");
                user_id = getCookie("user_id");
                if(user_id != "Guest") {
        			$("[name='comment_by']").val(user_id);
        			$("[name='comment_by_fullname']").val(full_name);
                }
            }
			$("#comment-form textarea").val("");
			})

		$("#submit-comment").click(function() {
			var args = {
				comment_by_fullname: $("[name='comment_by_fullname']").val(),
				comment_by: $("[name='comment_by']").val(),
				comment: $("[name='comment']").val(),
				reference_doctype: "{{ reference_doctype or doctype }}",
				reference_name: "{{ reference_name or name }}",
				comment_type: "Comment",
				route: "{{ pathname }}",
			}

			if(!args.comment_by_fullname || !args.comment_by || !args.comment) {
				frappe.msgprint("{{ _("All fields are necessary to submit the comment.") }}");
				return false;
			}

			if (args.comment_by!=='Administrator' && !valid_email(args.comment_by)) {
				frappe.msgprint("{{ _("Please enter a valid email address.") }}");
				return false;
			}

			frappe.call({
				btn: this,
				type: "POST",
				method: "frappe.templates.includes.comments.comments.add_comment",
				args: args,
				callback: function(r) {
					if(r.exc) {
						if(r._server_messages)
							frappe.msgprint(r._server_messages);
					} else {
						$(r.message).appendTo("#comment-list");
						$(".no-comment, .add-comment").toggle(false);
						$("#comment-form").toggle();
					}
					$(".add-comment").text('{{ _("Add Another Comment") }}');
					$(".add-comment").toggle();
				}
			})

			return false;
		})
	});
</script>
