<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">

  <?python
     from allura.lib.security import has_artifact_access
     ?>

  <py:def function="threads_table(threads)">
    <input py:for="i,thread in enumerate(threads)"
           type="hidden" name="thread-${i}.id" value="$thread._id"/>
    <table id="threads">
      <thead>
        <tr>
          <th>Topics</th>
          <th>Replies</th>
          <th>Views</th>
          <th>Last Post</th>
          <th>Subscribed</th>
        </tr>
      </thead>
      <tbody>
        <py:for each="i, thread in enumerate(threads)">
          ${thread_row(i, thread)}
        </py:for>
      </tbody>
    </table>
  </py:def>

  <py:def function="forums_table(parent_id)">
    <input py:for="i,forum in enumerate(c.app.subforums_of(parent_id))"
           type="hidden" name="forum-${i}.shortname" value="$forum.shortname"/>
    <table id="forums">
      <thead>
        <tr>
          <th/>
          <th>Forum</th><th>Topics</th><th>Posts</th><th>Last Post</th><th>Subscribed</th>
        </tr>
      </thead>
      <tbody>
        <py:for each="i, forum in enumerate(c.app.subforums_of(parent_id))">
          ${forum_row(i, forum)}
        </py:for>
      </tbody>
    </table>
  </py:def>

  <py:def function="forum_row(i, forum)">
    <tr>
      <td>[icon]</td>
      <td>
        <a href="${forum.url()}">$forum.name</a>($forum.shortname)<br/>
        ${Markup(g.markdown.convert(forum.description))}
        <py:if test="forum.subforums">
          <b>Subforums:</b>
          <span py:for="i, sf in enumerate(forum.subforums)">
            <py:if test="i != 0">, </py:if>
            <a href="${sf.url()}">$sf.name</a> 
          </span>
        </py:if>
      </td>
      <td>${forum.num_topics}</td>
      <td>${forum.num_posts}</td>
      <td>${post_summary(forum.last_post)}</td>
      <td>${checkbox('forum-%s.subscribed' % i, None, forum.subscription())}
      </td>
    </tr>
  </py:def>

  <py:def function="display_comment(comment)">
    <div id="comment-${comment._id}">
      <div class="forge_comment_body">
        <div class="user_info">
          <img src="${g.forge_static('images/error.png')}" alt="icon" width="50" height="50"/>
          <br/>
          ${comment.author().display_name}
        </div>
        <a py:if="c.user._id and comment.author_id==c.user._id"
           href="comments/$comment._id/delete">[X]</a>
        <br/>
        ${Markup(g.markdown.convert(comment.text))}
        ${comment.posted_ago}
        <div class="reply title-pane closed">
          <a class="title" href="#">Reply</a>
          <div class="content">
            <form method="post" action="comments/$comment._id/reply">
              <textarea rows="4" cols="60" name="text"></textarea><br/>
              <input type="submit" value="Save Comment"/>
            </form>
          </div>
        </div>
      </div>
      <div class="forge_comment_replies">
        <py:for each="cc in comment.replies()">
          ${display_comment(cc)}
        </py:for>
      </div>
    </div>
  </py:def>

  <py:def function="thread_row(i, thread)">
    <tr>
      <td><a href="${thread.url()}">${thread.subject or '(no subject)'}</a></td>
      <td>$thread.num_replies</td>
      <td>$thread.num_views</td>
      <td>${post_summary(thread.last_post)}</td>
      <td>${checkbox('thread-%s.subscribed' % i, None, thread.subscription())}</td>
    </tr>
  </py:def>

  <py:def function="post_summary(post)">
    <span py:if="post">
      by <a href="${post.author().url()}">${post.author().display_name}</a>
      ${h.ago(post.timestamp)}
    </span>
  </py:def>

  <py:def function="page_nav(page,count,limit)"
          py:with="cur_page=page / limit; num_pages=count/limit">
    <p>Page ${1 + cur_page} of ${1 + num_pages}</p>
    <a href=".">&lt;&lt;</a> 
    <a href="?page=${page - limit}">&lt;</a>
    <a href="?page=${page + limit}">&gt;</a>
    <a href="?page=${count - limit}">&gt;&gt;</a>
  </py:def>

  <py:def function="display_post(post)">
    <div id="post-${post.slug}" class="title-pane">
      <div class="title">
        <strong>${post.subject or '(no subject)'}</strong>
        by <a href="${post.author().url()}">${post.author().display_name}</a>
        ${h.ago(post.timestamp)}
      </div>        
      <div class="content">
        <a href="${post.url()}">Permalink</a>
        <form method="POST" action="${post.url()}flag">
          ${submit_button('Flag this post as inappropriate or spam')}
        </form>
        <div py:if="has_artifact_access('moderate', post)()"
             class="title-pane closed">
          <span class="title">Moderate</span>
          <div class="content">
            <form method="POST" action="${post.url()}moderate">
              <fieldset py:if="has_artifact_access('moderate', post.thread)()">
                <legend>Promote post to its own thread</legend>
                ${text_field('subject', 'Thread title', post.subject)}
                ${submit_button('Promote to thread')}
              </fieldset>
              ${submit_button('Spam Post', 'spam')}
              ${submit_button('Delete Post', 'delete')}
            </form>
          </div>
        </div>
        <br/>
        ${Markup(g.markdown.convert(post.text))}
        <div py:if="has_artifact_access('moderate', post)()"
             class="title-pane closed">
          <span class="title">Edit Post</span>
          <div class="content">
            <form method="POST" action="${post.url()}">
                ${text_field('subject', 'Subject', post.subject)}
                ${text_area('text', 'Content', post.text)}
                ${submit_button('Update post')}
            </form>
          </div>
        </div>
        <div style="padding-left: 10px; border-left: 1px solid grey">
          <div py:if="has_artifact_access('post', post.thread)()" class="reply title-pane closed">
            <div class="title">Reply to ${post.author().display_name}</div>
            <div class="content">
              <form method="post" action="${post.url() + 'reply'}">
                ${text_field('subject', 'Subject', post.reply_subject())}
                ${text_area('text', 'Content', '')}
                ${submit_button('Send Reply')}
              </form>
            </div>
          </div>
          <div py:if="has_artifact_access('moderate', post)()" class="reply title-pane closed">
            <div class="title">New Attachment</div>
            <div class="content">
              <form method="post" action="${post.url() + 'attach'}" enctype="multipart/form-data">
                ${file_field('file_info', 'File to upload')}
                ${submit_button('Attach file')}
              </form>
            </div>
          </div>
          <div py:for="att in post.attachments">
            <form py:if="c.user._id and post.author_id==c.user._id"
                  method="POST" action="${att.url()}"
                  style="display:inline">
              <input type="submit" name="delete" value="Delete"/>
            </form>
            <a href="${att.url()}">${att.metadata.filename}</a>
            ($att.length bytes)
            <div py:if="att.contentType.startswith('image/') or att.contentType.startswith('text/')"
                 class="title-pane closed">
              <div class="title">View $att.metadata.filename</div>
              <img class="content" py:if="att.contentType.startswith('image/')"
                   style="max-width:100%" src="${att.url()}"/>
              <iframe class="content" width="100%" py:if="att.contentType.startswith('text/')"
                      src="${att.url()}?embed=True"/>
            </div>
          </div>
        </div>
      </div>
    </div>
  </py:def>

</html>
