{% extends g.theme.master %}
{% from 'allura:templates/jinja_master/lib.html' import abbr_date with context %}

{% do g.register_forge_css('css/forge/hilite.css') %}
{% do g.register_app_css('css/tracker.css') %}

{% block title %}{{c.project.name}} / {{c.app.config.options.mount_label}} / #{{ticket.ticket_num}} {{ticket.summary}}{% endblock %}

{% block head %}
  <link rel="alternate" type="application/rss+xml" title="RSS" href="feed.rss"/>
  <link rel="alternate" type="application/atom+xml" title="Atom" href="feed.atom"/>
{% endblock %}

{% block header %}#{{ticket.ticket_num}} {{ticket.summary}}{% endblock %}

{% block actions %}
<a href="{{ticket.url()}}feed.rss" title="Follow this Ticket"><b data-icon="{{g.icons['feed'].char}}" class="ico {{g.icons['feed'].css}}"></b></a>
{% if c.user and c.user != c.user.anonymous() %}
  {{c.subscribe_form.display(value=subscribed, tool_subscribed=tool_subscribed, action='subscribe', style='icon')}}
  <a href="#" title="Edit" class="edit_ticket"><b data-icon="{{g.icons['pencil'].char}}" class="ico {{g.icons['pencil'].css}}"></b> Edit</a>
{% endif %}
{% endblock %}

{% block edit_box %}
<div class="editbox">
  <div class="form_holder" style="display: none">
    {{c.ticket_form.display(action='%supdate_ticket_from_widget' % ticket.url(),
      value=ticket, ticket=ticket, show_comment=True)}}
  </div>
  <div class="view_holder">
      {% if '_milestone' in ticket.custom_fields %}
        <div class="grid-4">
          <label class="simple">Milestone: </label>
          {% if ticket.custom_fields['_milestone'] %}
            <a href="{{c.app.url}}milestone/{{ticket.custom_fields['_milestone']}}">{{ticket.custom_fields['_milestone']}}</a>
          {% else %}
            None
          {% endif %}
        </div>
      {% endif %}
      <div class="grid-4">
        <label class="simple">Status: </label>
        <span class="{{ticket.open_or_closed}}">{{ticket.status}}</span>
      </div>
      <div class="grid-4 ticket-assigned-to">
        <label class="simple">Owner: </label>
        {% if ticket.assigned_to_id %}
          <a href="{{ticket.assigned_to.url()}}">{{ticket.assigned_to_name()}}</a>
        {% else %}
          {{ticket.assigned_to_name()}}
        {% endif %}
      </div>
      <div class="grid-4">
        <label class="simple">Labels: </label>
        {% for label in ticket.labels %}
          <span class="ui-corner-all tag">
            <a href="../search?q=labels:{{h.urlquoteplus(label)}}">{{label}} ({{ticket.artifacts_labeled_with(label).count()}})</a>
          </span>
        {% else %}
          None
        {% endfor %}
      </div>
    <div style="clear:both"></div>
    {% set min_c_fields = '_milestone' in ticket.custom_fields and 1 or 0 %}
    {% if globals.custom_fields and globals.custom_fields.__len__() > min_c_fields %}
      {% set cf_count=0 %}
      {% for field in globals.custom_fields or [] %}
        {% if field.name != '_milestone' %}
          {% if cf_count%4 == 0 and cf_count != 0 %}
            <div style="clear:both"></div>
          {% endif %}
            <div class="grid-4">
              <label class="simple">{{field.label}}:</label>
              {{ticket.custom_fields[field.name]}}
            </div>
          {% set cf_count=cf_count+1 %}
        {% endif %}
      {% endfor %}
    {% endif %}
    <div style="clear:both"></div>
    <div class="grid-4">
      <label class="simple">Updated:</label>
      {{abbr_date(ticket.mod_date)}}
    </div>
    <div class="grid-4">
      <label class="simple">Created:</label>
      {{abbr_date(ticket.created_date)}}
    </div>
    <div class="grid-8">
      <label class="simple">Creator:</label>
      <a href="{{ticket.reported_by.url()}}">{{ticket.reported_by.display_name}}</a>
    </div>
  </div>
</div>
{% endblock %}

{% block content %}
<div id="ticket_content">
  {{g.markdown.convert(ticket.description)|safe}}
  {% if ticket.attachments %}
    <strong class="grid-18">{{ticket.attachments.__len__()}} Attachments</strong>
    <div class="clear">
    {% for att in ticket.attachments %}
        <div class="attachment_thumb">
          {% if att.is_image() and not att.is_embedded() %}
            <a href="{{att.url()}}" class="file_type">
              <img src="{{att.url()}}/thumb">
            </a>
          {% else %}
            <a href="{{att.url()}}" class="file_type">
              <span>{{att.filename.split('.')[-1] or 'file'}}</span>
            </a>
          {% endif %}
          <a href="{{att.url()}}">{{att.filename}}</a>
        </div>
    {% endfor %}
    </div>
    <div style="clear:both"></div>
  {% endif %}
</div>
{% endblock %}

{% block after_content %}
  {{lib.related_artifacts(ticket)}}
  <div id="discussion_holder">
    <h2>Discussion</h2>
    {% set thread=ticket.discussion_thread %}
    {{c.thread.display(value=thread,new_post_text="New Comment",page=page,limit=limit,count=count)}}
  </div>
{% endblock %}

{% block extra_js %}
  {% if h.has_access(ticket, 'write')() %}
    <script type="text/javascript">
        $(document).ready(function () {
          // show/hide form
          var form_holder = $('div.form_holder');
          var view_holder = $('div.view_holder');
          var discussion_holder = $('#discussion_holder');
          var ticket_content = $('#ticket_content');
          var title_holder = $('h2.dark');
          var original_title = title_holder.text();
          var title_actions = title_holder.find('small');
          $('a.edit_ticket').click(function () {
            form_holder.show();
            view_holder.hide();
            discussion_holder.hide();
            ticket_content.hide();
            title_actions.hide();
            title_actions.appendTo(document.body);
            title_holder.text('\u00A0');
            $('a.edit_ticket').addClass('btn_activate');
            $('textarea[name="ticket_form.description"]').trigger('editticket.forgetracker').focus();
            $(this).trigger('editTicket');
            return false;
          });
          $('a.cancel_form').click(function () {
            form_holder.hide();
            view_holder.show();
            discussion_holder.show();
            ticket_content.show();
            title_holder.text(original_title);
            title_actions.appendTo(title_holder);
            title_actions.show();
            $('a.edit_ticket').removeClass('btn_activate');
            return false;
          });
          // delete attachments
          $('div.attachment_thumb a.delete_attachment').click(function () {
            var cval = $.cookie('_session_id');
            $.post(this.href, {'delete': 'True','_session_id':cval}, function () {
                location.reload();
            });
            return false;
          });
        });
    </script>
  {% endif %}
{% endblock %}

{% block extra_css %}
<style type="text/css">
  input[name='ticket_form.summary']{
    top: 5px;
    position: absolute;
    z-index: 21;
  }
  div.form_holder .reply textarea{
    height: 60px;
  }
  div.actions{
    width: 150px;
  }
  .markdown_edit textarea {
    height: 100px;
  }
</style>
{% endblock %}
