{#-
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
-#}
{% extends g.theme.master %}
{% from 'allura:templates/jinja_master/lib.html' import abbr_date with context %}
{% from 'allura:templates/jinja_master/lib.html' import canonical_tag  %}

{% 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"/>
    {{ canonical_tag() }}
{%- endblock -%}

{% block header %}#{{ticket.ticket_num}} {{ticket.summary}}{% if ticket.deleted %}<span> (deleted)</span>{% endif %}{% endblock %}

{% set editable = h.has_access(ticket, 'update') and not ticket.deleted %}

{% block actions %}
  {{ g.icons['feed'].render(href='feed.rss', title='Follow this Ticket', rel='nofollow') }}
{% if c.user and c.user != c.user.anonymous() %}
  {{c.subscribe_form.display(value=subscribed, action='subscribe', style='icon')}}
{% endif %}
{% if h.has_access(ticket.app, 'admin') %}
  {{ g.icons['move'].render(show_title=True, href='move') }}
{% endif %}
{% if editable %}
  {{ g.icons['edit'].render(show_title=True, extra_css='edit_ticket') }}
  {%if h.has_access(ticket, 'delete') and not ticket.deleted%}
    {{ g.icons['delete'].render(href='delete', extra_css='post-link') }}
  {% endif %}
{%elif h.has_access(ticket, 'delete') and ticket.deleted%}
  {{ g.icons['undelete'].render(href='undelete', extra_css='post-link') }}
{% endif %}
{% endblock %}

{% block edit_box %}
<div class="editbox">
  {% if editable %}
  <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>
  {% endif %}
  <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-5 ticket-assigned-to">
        <label class="simple">Owner: </label>
        {% if ticket.assigned_to_id %}
          {{lib.user_link(ticket.assigned_to)}}
        {% 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:%22{{h.urlquoteplus(label)}}%22">{{label}} ({{ticket.artifacts_labeled_with(label, ticket.app_config).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>
              {% if field.type == 'user' %}
                {% set user = ticket.get_custom_user(field.name) %}
                {% if user and not user.is_anonymous() %}
                  {{lib.user_link(user, avatar=False)}}
                {% else %}
                  nobody
                {% endif %}
              {% elif field.type == 'milestone' %}
                <a href="{{c.app.url}}{{field.name[1:]}}/{{ticket.custom_fields[field.name]}}">{{ticket.custom_fields[field.name]}}</a>
              {% else %}
                {{ticket.custom_fields[field.name]}}
              {% endif %}
            </div>
          {% set cf_count=cf_count+1 %}
        {% endif %}
      {% endfor %}
    {% endif %}
    <div style="clear:both"></div>
    <div class="grid-4">
      <label class="simple">Updated:</label>
      <span id="updated_id">{{abbr_date(ticket.mod_date)}}</span>
    </div>
    <div class="grid-4">
      <label class="simple">Created:</label>
      {{abbr_date(ticket.created_date)}}
    </div>
    <div class="grid-5">
      <label class="simple">Creator:</label>
      {{lib.user_link(ticket.reported_by)}}
    </div>
    <div class="grid-4">
      <label class="simple">Private:</label>
      {{'Yes' if ticket.private else 'No'}}
        {% if ticket.discussion_disabled %}
            <label class="simple"><span class="closed">Discussion Disabled</span></label>
        {% endif %}
    </div>
  </div>
</div>
{% endblock %}

{% block content %}
{% if voting_enabled %}
  {{ c.vote_form.display(artifact=ticket) }}
{% endif %}
<div id="ticket_content">
  <div{% if h.has_access(ticket, 'update') %} class="active-md" data-markdownlink=""{% endif %}>{{g.markdown.cached_convert(ticket, 'description')}}</div>
  {% if ticket.attachments %}
    <strong class="grid-18">{{ticket.attachments|length}} 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" alt="">
            </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, c.user)}}
  <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, 'update') %}
    <script type="text/javascript">
        // Sometimes IE11 is not firing jQuery's "$(document).ready" callbacks
        $(window).on('load', 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_text = title_holder.find('span').text()
          var original_title = title_holder.html();
          var title_actions = title_holder.find('small');
          var vote = $('#vote');

          var discussion_comment_cm = get_cm($('#new_post_holder'));

          $('a.edit_ticket').click(function () {
            var not_posted_comment = discussion_comment_cm.getValue();
            form_holder.show();
            view_holder.hide();
            discussion_holder.hide();
            ticket_content.hide();
            title_actions.hide();
            title_actions.appendTo(document.body);
            title_holder.find('span').text('\u00A0')
            vote.hide();
            $('div.new-ticket-title label').hide();
            $('a.edit_ticket').addClass('btn_activate');
            var cm = get_cm(form_holder);
            cm.refresh();
            cm.focus();
            var comment_cm = get_cm($('textarea[name="ticket_form.comment"]').parent());
            comment_cm.setValue(not_posted_comment);
            $('textarea[name="ticket_form.description"]').trigger('editticket.forgetracker');
            $(this).trigger('editTicket');
            return false;
          });
          var flashErrors = $('div > div.error');
          if (flashErrors.hasClass('error') && flashErrors.html().indexOf('rate limit') < 0){
            $('a.edit_ticket').click();
          }
          $('a.cancel_form').click(function () {
            form_holder.hide();
            view_holder.show();
            discussion_holder.show();
            ticket_content.show();
            title_holder.find('span').text(original_title_text);
            title_actions.appendTo(title_holder);
            title_actions.show();
            vote.show();
            $('a.edit_ticket').removeClass('btn_activate');
            return false;
          });
          $('.post-link').click(function(evt) {
                        var cval = $.cookie('_csrf_token');
                        evt.preventDefault();
                        $.post(this.href, {_csrf_token:cval}, function(val)
                                { window.location = val.location; },
                                'json');
          });
          // delete attachments
          $('div.attachment_thumb a.delete_attachment').click(function () {
            var cval = $.cookie('_csrf_token');
            $.post(this.href, {'delete': 'True','_csrf_token':cval}, function () {
                location.reload();
            });
            return false;
          });
        });
    </script>
  {% endif %}
{% endblock %}

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