<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:i18n="http://genshi.edgewall.org/i18n"
      py:with="preview_mode = 'preview' in req.args">
  <xi:include href="layout.html" />

  <head>
    <title py:choose="">
      <py:when test="ticket.exists">#${ticket.id} (${ticket.summary})</py:when>
      <py:otherwise>New Ticket</py:otherwise>
    </title>
    <script py:if="ticket.exists" type="text/javascript" src="${chrome.htdocs_location}js/threaded_comments.js"></script>
    <script type="text/javascript" py:choose="">
      jQuery(document).ready(function($) {
        $("div.description").find("h1,h2,h3,h4,h5,h6").addAnchor(_("Link to this section"));
        $(".foldable").enableFolding(false, true);
      <py:when test="ticket.exists">/*<![CDATA[*/
        $("#attachments").toggleClass("collapsed");
        $("#modify").parent().toggleClass("collapsed");

        /* only enable control elements for the currently selected action */
        var actions = $("#action input[name='action']");

        function updateActionFields() {
          actions.each(function () {
            $(this).siblings().find("*[id]").enable($(this).checked());
            $(this).siblings().filter("*[id]").enable($(this).checked());
          });
        }
        actions.click(updateActionFields);
        updateActionFields();

        function setRevertHandler() {
          $("button.trac-revert").click(function() {
            var div = $("div", this);
            var field_name = div[0].id.substr(7);
            var field_value = div.text();
            var input = $("#propertyform *[name=field_" + field_name + "]");
            if (input.length > 0) {
              if (input.filter("input[type=radio]").length > 0) {
                input.val([field_value]);
              } else if (input.filter("input[type=checkbox]").length > 0) {
                input.val(field_value == "1" ? [field_value] : []);
              } else {
                input.val(field_value);
              }
            } else { // Special case for CC checkbox
              input = $("#propertyform input[name=cc_update]").val([]);
            }
            input.change();
            $(this).closest("li").remove();
            return false;
          });
        }
        setRevertHandler();

        var comment_focused = false;
        $("#comment").focus(function() { comment_focused = true; })
                     .blur(function() { comment_focused = false; });
        $("#propertyform").autoSubmit({preview: '1'}, function(data, reply) {
          var items = $(reply);
          // Update ticket box
          $("#ticket").replaceWith(items.filter('#ticket'));
          // Unthread, unrevert and update changelog
          if (!$('#trac-comments-oldest').checked())
            $('#trac-comments-oldest').click().change();
          $("#changelog").replaceWith(items.filter("#changelog"));
          if ($('#trac-comments-only-toggle').attr('checked'))
            $('#trac-comments-only-toggle').click().attr('checked', true);
          // Show warning
          var new_changes = $("#changelog .trac-new");
          $("#trac-edit-warning").toggle(new_changes.length != 0);
          if (new_changes.length != 0)
            $("#changelog").parent().show().removeClass("collapsed");
          // Update view time
          $("#propertyform input[name='view_time']").replaceWith(items.filter("input[name='view_time']"));
          // Update preview
          var preview = $("#ticketchange").html(items.filter('#preview').children());
          var show_preview = preview.children().length != 0;
          $("#ticketchange").toggle(show_preview);
          setRevertHandler();
          // Collapse property form if comment editor has focus
          if (show_preview && comment_focused)
            $("#modify").parent().addClass("collapsed");
        }, "#ticketchange .trac-loading");
        $("#trac-comment-editor").autoSubmit({preview_comment: '1'}, function(data, reply) {
          var comment = $("#trac-comment-editor").next("div.comment").html(reply);
          comment.toggle(comment.children().length != 0);
        }, "#changelog .trac-loading");
        /*]]>*/
        <py:if test="preview_mode">
        $("#trac-add-comment").scrollToTop();
        </py:if>
      </py:when>
      <py:otherwise>
        $("#propertyform").autoSubmit({preview: '1'}, function(data, reply) {
          $('#ticket').replaceWith(reply);
        }, "#ticket .trac-loading");
        <py:if test="not preview_mode">
        $("#field-summary").focus();
        </py:if>
      </py:otherwise>
      });
    </script>
  </head>

  <body>
    <py:def function="commentref(prefix, cnum, cls=None)">
      <a href="#comment:$cnum" class="$cls">$prefix$cnum</a>
    </py:def>

    <div id="content" class="ticket"
         py:with="can_append = 'TICKET_APPEND' in perm(ticket.resource);
                  can_create = 'TICKET_CREATE' in perm(ticket.resource) and not ticket.exists;
                  can_modify = 'TICKET_CHGPROP' in perm(ticket.resource);
                  can_edit = 'TICKET_EDIT_DESCRIPTION' in perm(ticket.resource);
                  only_for_admin = 'TICKET_ADMIN' in perm(ticket.resource);
                  has_edit_comment = 'TICKET_EDIT_COMMENT' in perm(ticket.resource);
                  has_property_editor = not version and version != 0 and not cnum_edit
                                        and (can_append or can_modify or can_edit or can_create)">

      <h1 py:if="not ticket.exists">
        Create New Ticket <span py:if="preview_mode and ticket.type" class="status">(${ticket.type})</span>
      </h1>

      <py:if test="ticket.exists">
        <xi:include href="ticket_box.html" py:with="preview_mode = change_preview.fields"/>

        <!--! do not show attachments for old versions of this ticket or for new tickets -->
        <py:if test="not version and version != 0 and ticket.exists">
          <xi:include href="list_of_attachments.html"
                      py:with="alist = attachments; foldable = True"/>
        </py:if>

        <div style="${'display: none' if not changes else None}">
          <div style="position: relative">
            <form id="prefs" method="get" action="${href.prefs()}" style="position: absolute; right: 0">
              <div id="trac-comments-order">
                <input type="radio" id="trac-comments-oldest" name="trac-comments-order" value="oldest" checked="checked" />
                <label for="trac-comments-oldest">Oldest first</label>
                <input type="radio" id="trac-comments-newest" name="trac-comments-order" value="newest" />
                <label for="trac-comments-newest">Newest first</label>
                <span id="trac-threaded-toggle" style="display: none">
                  <input type="radio" id="trac-comments-threaded" name="trac-comments-order" value="threaded" />
                  <label for="trac-comments-threaded">Threaded</label>
                </span>
              </div>
              <div>
                <input id="trac-comments-only-toggle" type="checkbox" />
                <label for="trac-comments-only-toggle">Comments only</label>
              </div>
            </form>
          </div>

          <h3 class="foldable">Change History <span class="trac-count">(${len(changes)})</span></h3>

          <div id="changelog">
            <py:for each="change in changes">
              <div class="change${' trac-new' if change.date > start_time and 'attachment' not in change.fields else None}"
                   id="${'trac-change-%d-%d' % (change.cnum, to_utimestamp(change.date)) if 'cnum' in change else None}">
                <xi:include href="ticket_change.html"/>
              </div>
            </py:for>
          </div>
        </div>
      </py:if>
      <!--! End of the section we don't show on initial new tickets -->

      <form py:if="has_property_editor" method="post" id="propertyform"
            action="${href.ticket(ticket.id) + '#trac-add-comment' if ticket.exists
                      else href.newticket() + '#ticket'}">
        <!--! Add comment -->
        <div py:if="ticket.exists and can_append" id="trac-add-comment" class="field">
          <h3 class="foldable" id="edit">Add Comment</h3>
          <div>
            <div id="trac-edit-warning" class="warning system-message"
                 style="${'display: none' if start_time == ticket['changetime'] else None}"
                 i18n:msg="">
              This ticket has been modified since you started editing. You should review the
              <em class="trac-new">other modifications</em> which have been appended above,
              and any <em class="trac-conflict">conflicts</em> shown in the preview below.
              You can nevertheless proceed and submit your changes if you wish so.
            </div>
            <!--! Comment field -->
            <fieldset>
              <label for="comment" i18n:msg="">You may use
                <a tabindex="42" href="${href.wiki('WikiFormatting')}">WikiFormatting</a>
                here.
              </label>
              <textarea id="comment" name="comment" class="wikitext trac-resizable" rows="10" cols="78">
${comment}</textarea>
            </fieldset>
          </div>
        </div>

        <div>
          <h3 py:if="ticket.exists" class="foldable">Modify Ticket</h3>
          <div id="modify">
            <!--! Properties -->
            <fieldset id="properties" py:if="can_modify or can_edit or can_create"
                      py:with="fields = [f for f in fields if not f.skip]">
              <legend py:choose="">
                  <py:when test="ticket.exists">Change Properties</py:when>
                  <py:otherwise>Properties</py:otherwise>
              </legend>
              <table>
                <tr py:if="can_modify or can_create">
                  <th><label for="field-summary">Summary:</label></th>
                  <td class="fullrow" colspan="3">
                    <input type="text" id="field-summary" name="field_summary"
                           value="$ticket.summary" size="70" />
                  </td>
                </tr>
                <py:if test="only_for_admin">
                  <tr>
                    <th><label for="field-reporter">Reporter:</label></th>
                    <td class="fullrow" colspan="3">
                      <input type="text" id="field-reporter" name="field_reporter"
                             value="${ticket.reporter}" size="70" />
                    </td>
                  </tr>
                </py:if>
                <py:if test="can_edit or can_create">
                  <tr>
                    <th><label for="field-description">Description:</label></th>
                    <td class="fullrow" colspan="3">
                      <fieldset>
                        <label for="field-description" id="field-description-help" i18n:msg="">You may use
                          <a tabindex="42" href="${href.wiki('WikiFormatting')}">WikiFormatting</a> here.
                        </label>
                        <textarea id="field-description" name="field_description"
                                  class="wikitext trac-resizable" rows="10" cols="68">
${ticket.description}</textarea>
                      </fieldset>
                    </td>
                  </tr>
                </py:if>
                <tr py:for="row in group(fields, 2, lambda f: f.type != 'textarea')"
                    py:if="can_modify or can_create"
                    py:with="fullrow = len(row) == 1">
                  <py:for each="idx, field in enumerate(row)"
                          py:with="value = ticket.get_value_or_default(field.name)">
                    <th class="col${idx + 1}" py:if="idx == 0 or not fullrow">
                      <label for="field-${field.name}" py:if="field"
                             py:strip="field.type == 'radio'" i18n:msg="field">${
                        field.edit_label or field.label or field.name}:</label>
                    </th>
                    <td class="col${idx + 1}" py:if="idx == 0 or not fullrow"
                        colspan="${3 if fullrow else None}">
                      <py:choose test="field.type" py:if="field">
                        <select py:when="'select'" id="field-${field.name}" name="field_${field.name}">
                          <option py:if="field.optional"></option>
                          <option py:for="option in field.options"
                                  selected="${value == option or None}"
                                  value="$option" py:content="option"></option>
                          <optgroup py:for="optgroup in field.optgroups"
                                    py:if="optgroup.options"
                                    label="${optgroup.label}">
                            <option py:for="option in optgroup.options"
                                    selected="${value == option or None}"
                                    value="$option" py:content="option"></option>
                          </optgroup>
                        </select>
                        <textarea py:when="'textarea'" id="field-${field.name}" name="field_${field.name}"
                                  cols="${field.width}" rows="${field.height}"
                                  class="${'wikitext ' if field.format == 'wiki' else None}trac-resizable">
${value}</textarea>
                        <span py:when="'checkbox'">
                          <input type="checkbox" id="field-${field.name}" name="field_${field.name}"
                                 checked="${value == '1' or None}" value="1" />
                          <input type="hidden" name="field_checkbox_${field.name}" value="1" />
                        </span>
                        <label py:when="'radio'"
                               py:for="idx, option in enumerate(field.options)">
                          <input type="radio" name="field_${field.name}" value="${option}"
                                 checked="${value == option or None}" />
                          ${option}
                        </label>
                        <py:otherwise><!--! Text input fields -->
                          <py:choose>
                            <span py:when="field.cc_entry"><!--! Special case for Cc: field -->
                              <em>${field.cc_entry}</em>
                              <input type="checkbox" id="field-cc" name="cc_update"
                                title="This checkbox allows you to add or remove yourself from the CC list."
                                checked="${field.cc_update}" />
                            </span>
                            <!--! Cc: when TICKET_EDIT_CC is allowed -->
                            <span py:when="field.name == 'cc'">
                              <input  type="text" id="field-${field.name}"
                                title="Space or comma delimited email addresses and usernames are accepted."
                                name="field_${field.name}" value="${value}" />
                            </span>
                            <!--! All the other text input fields -->
                            <input py:otherwise="" type="text" id="field-${field.name}"
                              name="field_${field.name}" value="${value}" />
                          </py:choose>
                        </py:otherwise>
                      </py:choose>
                    </td>
                  </py:for>
                </tr>
              </table>
            </fieldset>

            <!--! Workflow support -->
            <py:if test="ticket.exists"> <!--! do not display the actions for New tickets -->
              <py:choose>
                <fieldset py:when="can_append or can_modify" id="action">
                  <legend>Action</legend>
                  <div py:for="key, label, controls, hints in action_controls">
                      <input type="radio" id="action_$key" name="action" value="$key"
                             checked="${key == action or None}" />
                      <label for="action_$key">$label</label>
                      $controls
                      <span class="hint" py:for="hint in hints">$hint</span>
                  </div>
                </fieldset>
                <py:otherwise>
                  <py:for each="key, label, controls, hints in action_controls">
                    <input py:if="key == action" type="hidden" name="action" value="$action" />
                  </py:for>
                </py:otherwise>
              </py:choose>
            </py:if>
          </div>
        </div>

        <!--! Preview of ticket changes -->
        <div py:if="ticket.exists and can_append" id="ticketchange" class="ticketdraft"
             style="${'display: none' if not (change_preview.fields or change_preview.comment)
                                         or cnum_edit is not None else None}">
          <xi:include href="ticket_change.html" py:with="change = change_preview; preview = True"/>
        </div>

        <!--! Author or Reporter -->
        <div py:if="authname == 'anonymous'" class="field">
          <fieldset py:choose="">
            <legend py:when="ticket.exists">Author</legend>
            <legend py:otherwise="">Reporter</legend>
            <table>
              <tr>
                <th>
                  <label for="author">Your email or username:</label><br />
                </th>
                <td>
                  <input type="text" id="author" name="author" size="40" value="$author_id" />
                  <br />
                </td>
              </tr>
            </table>
            <p py:if="author_id == 'anonymous'" class="hint">
              <i18n:msg>E-mail address and user name can be saved in the <a href="${href.prefs()}">Preferences</a>.</i18n:msg>
            </p>
          </fieldset>
        </div>

        <!--! Attachment on creation checkbox -->
        <p py:if="not ticket.exists and 'ATTACHMENT_CREATE' in perm(ticket.resource.child('attachment'))">
          <label>
            <input type="checkbox" name="attachment" checked="${'attachment' in req.args or None}" />
            I have files to attach to this ticket
          </label>
        </p>

        <div py:if="ticket.exists" class="trac-nav">
          <a href="#attachments" title="Go to the list of attachments">Attachments</a> &uarr;
        </div>
        <div class="buttons">
          <py:if test="ticket.exists">
            <input type="hidden" name="start_time" value="${to_utimestamp(start_time)}" />
            <input type="hidden" name="view_time" value="${to_utimestamp(ticket['changetime'])}" />
            <input type="hidden" name="replyto" value="${replyto}" />
          </py:if>
          <input type="submit" name="preview" value="${_('Preview')}" accesskey="r" />&nbsp;
          <input type="submit" name="submit" value="${_('Submit changes') if ticket.exists else _('Create ticket')}" />
        </div>

      </form>

      <xi:include href="ticket_box.html" py:if="not ticket.exists"
                  py:with="hide = not preview_mode; preview_mode = True"/>

      <div id="help" i18n:msg="">
        <strong>Note:</strong> See
        <a href="${href.wiki('TracTickets')}">TracTickets</a> for help on using
        tickets.
      </div>
    </div>
  </body>
</html>
