<%! from django_comment_client.permissions import has_permission %>

<script type="text/template" id="thread-template">
    <article class="discussion-article" data-id="${'<%- id %>'}">
        <div class="thread-content-wrapper"></div>
        
        <ol class="responses">
            <li class="loading"><div class="loading-animation"></div></li>
        </ol>
        <div class="post-status-closed bottom-post-status" style="display: none">
          This thread is closed.
        </div>
        % if course is UNDEFINED or has_permission(user, 'create_comment', course.id):
        <form class="discussion-reply-new" data-id="${'<%- id %>'}">
            <h4>Post a response:</h4>
            <ul class="discussion-errors"></ul>
            <div class="reply-body" data-id="${'<%- id %>'}"></div>
            <div class="reply-post-control">
                <a class="discussion-submit-post control-button" href="#">Submit</a>
            </div>
        </form>
        % endif
    </article>
</script>

<script type="text/template" id="thread-show-template">
  <div class="discussion-post">
      <div><a href="javascript:void(0)" class="dogear action-follow" data-tooltip="follow"></a></div>
      <header>
      ${"<% if (obj.group_id) { %>"}
      <div class="group-visibility-label">${"<%- obj.group_string%>"}</div>
              ${"<% }  %>"}      
      
          <a href="#" class="vote-btn discussion-vote discussion-vote-up" data-role="discussion-vote" data-tooltip="vote">
          <span class="plus-icon">+</span> <span class='votes-count-number'>${'<%- votes["up_count"] %>'}</span></a>
          <h1>${'<%- title %>'}</h1>
          <p class="posted-details">
              ${"<% if (obj.username) { %>"}
              <a href="${'<%- user_url %>'}" class="username">${'<%- username %>'}</a>
              ${"<% } else {print('anonymous');} %>"}
              <span class="timeago" title="${'<%- created_at %>'}">${'<%- created_at %>'}</span>

              <span class="post-status-closed top-post-status" style="display: none">
                &bull; This thread is closed.
              </span>
          </p>
      </header>

      <div class="post-body">${'<%- body %>'}</div>
      <div class="discussion-flag-abuse notflagged" data-role="thread-flag" data-tooltip="Report Misuse">                
      <i class="icon"></i><span class="flag-label">Report Misuse</span></div>      
        
        
      % if course and has_permission(user, 'openclose_thread', course.id):
      <div class="admin-pin discussion-pin notpinned" data-role="thread-pin" data-tooltip="pin this thread">                
      <i class="icon"></i><span class="pin-label">Pin Thread</span></div> 

      %else:
      ${"<% if (pinned) { %>"}
      <div class="discussion-pin notpinned" data-role="thread-pin" data-tooltip="pin this thread">                
      <i class="icon"></i><span class="pin-label">Pin Thread</span></div> 
      ${"<% }  %>"}  
      % endif
      
      
      ${'<% if (obj.courseware_url) { %>'}
      <div class="post-context">
          (this post is about <a href="${'<%- courseware_url%>'}">${'<%- courseware_title %>'}</a>)
      </div>
      ${'<% } %>'}

      <ul class="moderator-actions">
          <li style="display: none"><a class="action-edit" href="javascript:void(0)"><span class="edit-icon"></span> Edit</a></li>
          <li style="display: none"><a class="action-delete" href="javascript:void(0)"><span class="delete-icon"></span> Delete</a></li>
          <li style="display: none"><a class="action-openclose" href="javascript:void(0)"><span class="edit-icon"></span> Close</a></li>
      </ul>
  </div>
</script>

<script type="text/template" id="thread-edit-template">
  <div class="discussion-post edit-post-form">
    <h1>Editing post</h1>
    <ul class="edit-post-form-errors"></ul>
    <div class="form-row">
      <input type="text" class="edit-post-title" name="title" value="${"<%-title %>"}" placeholder="Title">
    </div>
    <div class="form-row">
      <div class="edit-post-body" name="body">${"<%- body %>"}</div>
    </div>
    ## TODO tags
    ## Until we decide what to do with tags, commenting them out.
    ##<div class="form-row">
    ##  <input type="text" class="edit-post-tags" name="tags" placeholder="Tags" value="${"<%- tags %>"}">
    ##</div>
    <input type="submit" class="post-update" value="Update post">
    <a href="#" class="post-cancel">Cancel</a>
  </div>
</script>

<script type="text/template" id="thread-response-template">
    <div class="discussion-response"></div>
    <ol class="comments">
        <li class="new-comment response-local">
            % if course is UNDEFINED or has_permission(user, 'create_sub_comment', course.id):
            <form class="comment-form" data-id="${'<%- wmdId %>'}">
                <ul class="discussion-errors"></ul>
                <div class="comment-body" data-id="${'<%- wmdId %>'}"
                data-placeholder="Add a comment..."></div>
                <div class="comment-post-control">
                    <a class="discussion-submit-comment control-button" href="#">Submit</a>
                </div>
            </form>
            % endif
        </li>
    </ol>
</script>

<script type="text/template" id="thread-response-show-template">
    <header class="response-local">
        <a href="javascript:void(0)" class="vote-btn" data-tooltip="vote"><span class="plus-icon"></span><span class="votes-count-number">${"<%- votes['up_count'] %>"}</span></a>
        <a href="javascript:void(0)" class="endorse-btn${'<% if (endorsed) { %> is-endorsed<% } %>'} action-endorse" style="cursor: default; display: none;" data-tooltip="endorse"><span class="check-icon" style="pointer-events: none; "></span></a>
        ${"<% if (obj.username) { %>"}
        <a href="${'<%- user_url %>'}" class="posted-by">${'<%- username %>'}</a>
        ${"<% } else {print('<span class=\"anonymous\"><em>anonymous</em></span>');} %>"}
        <p class="posted-details" title="${'<%- created_at %>'}">${'<%- created_at %>'}</p>
    </header>
    <div class="response-local"><div class="response-body">${"<%- body %>"}</div>
    <div class="discussion-flag-abuse notflagged" data-role="thread-flag" data-tooltip="report misuse">                
      <i class="icon"></i><span class="flag-label">Report Misuse</span></div>
    </div>
    <ul class="moderator-actions response-local">
        <li style="display: none"><a class="action-edit" href="javascript:void(0)"><span class="edit-icon"></span> Edit</a></li>
        <li style="display: none"><a class="action-delete" href="javascript:void(0)"><span class="delete-icon"></span> Delete</a></li>
        <li style="display: none"><a class="action-openclose" href="javascript:void(0)"><span class="edit-icon"></span> Close</a></li>
    </ul>
</script>

<script type="text/template" id="thread-response-edit-template">
  <div class="edit-post-form">
    <h1>Editing response</h1>
    <ul class="edit-post-form-errors"></ul>
    <div class="form-row">
      <div class="edit-post-body" name="body">${"<%- body %>"}</div>
    </div>
    <input type="submit" class="post-update" value="Update response">
    <a href="#" class="post-cancel">Cancel</a>
  </div>
</script>

<script type="text/template" id="response-comment-show-template">
  <div id="comment_${'<%- id %>'}">
    <div class="response-body">${'<%- body %>'}</div>
    <div class="discussion-flag-abuse notflagged" data-role="thread-flag" data-tooltip="report misuse">                
      <i class="icon"></i><span class="flag-label"></span></div> 
    <p class="posted-details">&ndash;posted <span class="timeago" title="${'<%- created_at %>'}">${'<%- created_at %>'}</span> by
        ${"<% if (obj.username) { %>"}
        <a href="${'<%- user_url %>'}" class="profile-link">${'<%- username %>'}</a>
        ${"<% } else {print('anonymous');} %>"}
    </p>
  </div>
</script>

<script type="text/template" id="thread-list-item-template">
    <a href="${'<%- id %>'}" data-id="${'<%- id %>'}">
        <span class="title">${"<%- title %>"}</span>
        ${"<% if (unread_comments_count > 0) { %>"}
            <span class="comments-count unread" data-tooltip="${"<%- unread_comments_count %>"} new comment${"<%- unread_comments_count > 1 ? 's' : '' %>"}">${"<%- comments_count %>"}</span>
        ${"<% } else { %>"}
            <span class="comments-count">${"<%- comments_count %>"}</span>
        ${"<% } %>"}
        <span class="votes-count">+${"<%- votes['up_count'] %>"}</span>
    </a>
</script>
