[%# This Source Code Form is subject to the terms of the Mozilla Public
  # License, v. 2.0. If a copy of the MPL was not distributed with this
  # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  #
  # This Source Code Form is "Incompatible With Secondary Licenses", as
  # defined by the Mozilla Public License, v. 2.0.
  #%]

[%#
  # field: (field object) bug_fields.$field_name object
  # field_type: (const) constants.FIELD_TYPE_*
  # no_label: (boolean) don't output label
  # label: (string) field label text (default: field_descs.${$field.name}
  # aria_label: (string) invisible aria-label attribute
  # view_only: (boolean) don't allow editing (default: determined from bug.check_can_change_field)
  # edit_only: (boolean) always render the edit ui
  # container: (boolean) output just a label and the content (e.g. for multiple fields next to one label)
  # name: (string) field name (default: field.name)
  # prefix: (string) string to prepend to 'name' and 'id' attributes (default: empty)
  # value: (string) visible value (default: bug.$name)
  # values: (array of string) list of value objects (FIELD_TYPE_SINGLE_SELECT and _BUG_URLS only) (default: lazy-load on edit)
  # inline: (boolean) output field as a table-cell instead of as a stand-alone div (default: false)
  # no_indent: (boolean) don't indent the field (left-padding) (default: false)
  # full_width: (boolean) the field takes up the complete page width (default: false)
  # short_width: (boolean) the field shouldn't take up much space at all (default: false)
  # hide_on_view: (boolean) hide field from read-only view (default: false)
  # hide_on_edit: (boolean) hide content when in edit mode (default: false)
  # append_content: (boolean) when called as a WRAPPER, the content will be appended to the
  #                 edit HTML instead of replacing it.  forces edit_only (default: false);
  # default: (string) default value (e.g. used as a placeholder in user fields)
  # help: (string) optional URL that describes the field (requires a label to be defined and visible)
  # required: (boolean) if the field is required. (At present, only implemented for FIELD_TYPE_FREETEXT
  #           and FIELD_TYPE_SINGLE_SELECT)
  # action: (hash) show a button to the right of the edit field (user fields only currently).  keys:
  #   id: (string) optional button id
  #   class: (string) optional button class
  #   caption: (string) button caption
  #   hidden: (boolean) don't show the button (default: false)
  #%]

[%
IF field_type.defined && !field;
  RETURN;
END;
IF !name.defined;
  name = field.name;
END;
IF !value.defined;
  value = bug.$name;
END;
IF hide_on_edit;
  view_only = 1;
END;
IF view_only || container;
  editable = 0;
END;
IF !editable.defined;
  editable = bug.check_can_change_field(name, 0, 1);
END;
IF append_content;
  edit_only = 1;
END;
IF inline && !label.defined;
  no_label = 1;
END;
IF !no_label && !label.defined;
  label = field_descs.${field.name};
END;
IF field_type == "";
  field_type = -1;
END;
field_name = name;
IF prefix.defined;
  name = prefix _ name;
END;
# date picker fields should always be short
IF field_type == constants.FIELD_TYPE_DATE
    || field_type == constants.FIELD_TYPE_DATETIME;
  short_width = 1;
END;
# determine if a field is set
IF field_type == constants.FIELD_TYPE_BUG_URLS || field_type == constants.FIELD_TYPE_BUG_LIST;
  has_value = values.size > 0;
ELSIF field_type == constants.FIELD_TYPE_MULTI_SELECT || field_type == constants.FIELD_TYPE_USERS;
  has_value = value.size > 0;
ELSE;
  has_value = value != "";
END;
# switch to view mode if edit-only and the current user cannot edit
IF !editable && edit_only;
  edit_only = 0;
END;
IF hide_on_view && hide_on_edit;
  RETURN;
END;
%]

<div class="field
            [%~ " indent" IF no_label && !no_indent %]
            [%~ " inline" IF inline %]
            [%~ " contains-buttons" IF use_buttons ~%]
            [%~ " bug-list" IF field_type == constants.FIELD_TYPE_BUG_LIST ~%]
            [%~ " edit-hide" IF hide_on_edit %]
            [%~ " edit-show" IF hide_on_view && !hide_on_edit %]"
  [% IF name %] id="field-[% name FILTER html %]"[% END %]
  [% IF hide_on_view %] style="display:none"[% END %]
>
  [% IF label.defined && !no_label %]
    <div class="name">
      [%~ IF help.defined %]
        <a href="[% help FILTER none %]" id="[% name FILTER html %]-help-link" class="help">
        [% aria_labelledby_id = "${name}-help-link" %]
        [% aria_labelledby_html = BLOCK %]aria-labelledby="[% aria_labelledby_id FILTER html %]"[% END %]
      [% END %]
      [%~ label _ ":" FILTER html IF label %]
      [%~ IF help.defined %]
        </a>
      [% END %]
    </div>
  [% END %]

  [% IF !aria_labelledby_html && (aria_label.defined || label.defined) %]
    [% aria_labelledby_html = BLOCK %]aria-label="[% aria_label || label FILTER html %]"[% END %]
  [% END %]

  [%# read-only HTML %]
  [% UNLESS edit_only %]
    <div class="[% "value" IF !container %][% " edit-hide" IF editable %][% " container" IF container %]">
      [% IF name %]
        <span id="field-value-[% name FILTER html %]">
      [% END %]
      [% IF content.defined %]
        [% content FILTER none %]
      [% ELSIF !editable && !has_value %]
        ---
      [% ELSE %]
        [% SWITCH field_type %]

          [% CASE constants.FIELD_TYPE_USER %]
            [%# users %]
            [% INCLUDE bug_modal/user.html.tmpl u=value nick_only=1 gravatar_size=20 %]

          [% CASE constants.FIELD_TYPE_BUG_URLS %]
            [%# see also %]
            [% INCLUDE bug_urls values=values edit=0 %]

          [% CASE constants.FIELD_TYPE_BUG_LIST %]
            [%# bug lists (generally dependencies) %]
            [% INCLUDE bug_list values=values edit=0 %]

          [% CASE constants.FIELD_TYPE_TEXTAREA %]
            [%# text areas %]
            <span class="multiline-value">[% value FILTER html FILTER html_line_break %]</span>

          [% CASE constants.FIELD_TYPE_MULTI_SELECT %]
            [%# multi-select %]
            [% value.join(", ") FILTER html %]

          [% CASE constants.FIELD_TYPE_DATETIME %]
            [%# datetime %]
            [% value FILTER time %]

          [% CASE constants.FIELD_TYPE_DATE %]
            [%# date %]
            <time>[% value FILTER html %]</time>

          [% CASE constants.FIELD_TYPE_BUG_ID %]
            [%# bug id %]
            [% value FILTER bug_link(value, use_alias => 1) FILTER none %]

          [% CASE %]
            [%# every else %]
            [% value FILTER html %]

        [% END %]
      [% END %]
      [% IF name %]
        </span>
      [% END %]
    </div>
  [% END %]

  [%# edit HTML %]
  [% IF editable %]
    [% "MISSING NAME" UNLESS name %]
    <div class="value edit edit-show [% " wide" IF full_width %][% " short" IF short_width %]"
      [% UNLESS edit_only +%] style="display:none"[% END %]>
      [% SWITCH field_type %]

        [% CASE constants.FIELD_TYPE_SINGLE_SELECT %]
          [%# single value select %]
          <input type="hidden" id="[% name FILTER html %]-dirty">
          [% IF use_buttons %]
          <div role="radiogroup" class="buttons toggle" id="[% name FILTER html %]"
               [%= 'aria-required="true"' IF (field.is_mandatory || required) %] [%= aria_labelledby_html FILTER none %]>
            [% IF values.defined %]
              [% FOREACH v IN values %]
                [% NEXT IF NOT v.is_active AND NOT value.contains(v.name).size %]
                [% NEXT IF NOT bug.check_can_change_field(name, bug.${field_name}, v.name) %]
                [% NEXT IF (field.is_mandatory || required) && v.name == '--' %]
                <div class="item">
                  <input id="v[% v.id FILTER html %]_[% name FILTER html %]" type="radio" name="[% name FILTER html %]"
                         value="[% v.name FILTER html %]" [% " checked" IF value.contains(v.name).size %]>
                  <label for="v[% v.id FILTER html %]_[% name FILTER html %]">
                    [% IF name == 'bug_type' %]
                      <span class="bug-type-label iconic-text" data-type="[% v.name FILTER html %]">
                        <span class="icon" aria-hidden="true"></span>[% v.name FILTER html %]
                      </span>
                    [% ELSE %]
                      [%~ v.name FILTER html ~%]
                    [% END %]
                  </label>
                </div>
              [% END %]
            [% ELSE %]
            <div class="item">
              <input id="[% name FILTER html %]_[% value FILTER html %]_radio" type="radio"
                     name="[% name FILTER html %]" value="[% value FILTER html %]" checked>
              <label for="[% name FILTER html %]_[% value FILTER html %]_radio">
                [% IF name == 'bug_type' %]
                  <span class="bug-type-label iconic-text" data-type="[% value FILTER html %]">
                    <span class="icon" aria-hidden="true"></span>[% value FILTER html %]
                  </span>
                [% ELSE %]
                  [%~ value FILTER html ~%]
                [% END %]
              </label>
            </div>
            [% END %]
          </div>
          [% ELSE %]
          <select name="[% name FILTER html %]" id="[% name FILTER html %]" [% aria_labelledby_html FILTER none %]>
            [% IF values.defined %]
              [% FOREACH v IN values %]
                [% NEXT IF NOT v.is_active AND NOT value.contains(v.name).size %]
                [% NEXT IF NOT bug.check_can_change_field(name, bug.${field_name}, v.name) %]
                <option value="[% v.name FILTER html %]"
                        id="v[% v.id FILTER html %]_[% name FILTER html %]"
                        [% " selected" IF value.contains(v.name).size %]
                >[% v.name FILTER html %]</option>
              [% END %]
            [% ELSE %]
              <option value="[% value FILTER html %]" selected>[% value FILTER html %]</option>
            [% END %]
          </select>
          [% END %]

        [% CASE constants.FIELD_TYPE_MULTI_SELECT %]
          [%# multi value select %]
          <input type="hidden" id="[% name FILTER html %]-dirty">
          <select name="[% name FILTER html %]" id="[% name FILTER html %]" multiple size="5" [% aria_labelledby_html FILTER none %]>
            [% IF values.defined %]
              [%# not implemented %]
            [% ELSE %]
              [% FOREACH v IN value %]
                <option value="[% v FILTER html %]" selected>[% v FILTER html %]</option>
              [% END %]
            [% END %]
          </select>

        [% CASE constants.FIELD_TYPE_FREETEXT %]
          [%# normal input field %]
          <input name="[% name FILTER html %]" id="[% name FILTER html %]" value="[% value FILTER html %]"
                 [% IF default %] placeholder="[% default FILTER html %]"[% END %]
                 [%+ aria_labelledby_html FILTER none +%] [% " required" IF required %]>
        [% CASE constants.FIELD_TYPE_USER %]
          [% IF action && !action.hidden %]
            <button type="button" class="field-button minor [%= action.class FILTER html IF action.class %]"
              data-field="[% name FILTER html %]"
              [% IF action.id %]id="[% action.id FILTER html %]"[% END %]>
              [% action.caption FILTER html %]
            </button>
            <div class="field-edit-container">
          [% END %]
          [%# single user %]
          [% INCLUDE global/userselect.html.tmpl
              id = name
              name = name
              value = value.login
              classes = [ "bz_userfield" ]
              placeholder = default
              aria_labelledby = aria_labelledby_id
            %]
          [% IF action && !action.hidden %]
            </div>
          [% END %]

        [% CASE constants.FIELD_TYPE_USERS %]
          [%# multiple users %]
          <input type="hidden" name="defined_[% name FILTER html %]" value="[% value.join(", ") FILTER html %]">
          [% INCLUDE global/userselect.html.tmpl
              id = name
              name = name
              value = value.join(", ")
              classes = [ "bz_userfield" ]
              placeholder = default
              multiple = 5
              aria_labelledby = aria_labelledby_id
            %]

        [% CASE constants.FIELD_TYPE_KEYWORDS %]
          [%# keywords %]
          <input type="text" id="[% name FILTER html %]" name="[% name FILTER html %]"
            value="[% value FILTER html %]" [% aria_labelledby_html FILTER none %]>

        [% CASE constants.FIELD_TYPE_BUG_URLS %]
          [%# see also %]
          [% INCLUDE bug_urls values=values edit=1 %]

        [% CASE constants.FIELD_TYPE_BUG_LIST %]
          [%# bug lists %]
          [% INCLUDE bug_list values=values edit=1 %]

        [% CASE constants.FIELD_TYPE_TEXTAREA %]
          [%# text area %]
            <button type="button" class="secondary edit-textarea-btn [%= "edit-textarea-set-btn" IF value != "" %]"
              id="[% name FILTER html %]-edit">Edit</button>
            [% IF content.defined && !append_content %]
              <span id="[% name FILTER html %]-view">[% content FILTER none %]</span>
            [% ELSE %]
              <span class="multiline-value" id="[% name FILTER html %]-view">[% value FILTER html FILTER html_line_break %]</span>
            [% END %]
            <textarea id="[% name FILTER html %]" name="[% name FILTER html %]"
              rows="10" cols="10" style="display:none" [% aria_labelledby_html FILTER none %]>[% value FILTER html %]</textarea>

        [% CASE constants.FIELD_TYPE_DATETIME %]
          [%# datetime %]
          <input type="datetime-local" name="[% name FILTER html %]" id="[% name FILTER html %]"
            value="[% value FILTER html %]" [% aria_labelledby_html FILTER none %]>

        [% CASE constants.FIELD_TYPE_DATE %]
          [%# date %]
          <input type="date" name="[% name FILTER html %]" id="[% name FILTER html %]"
            value="[% value FILTER html %]" [% aria_labelledby_html FILTER none %]>

        [% CASE constants.FIELD_TYPE_INTEGER %]
          [%# integer %]
          <input type="number" name="[% name FILTER html %]" id="[% name FILTER html %]"
            value="[% value FILTER html %]" [% aria_labelledby_html FILTER none %]>

        [% CASE constants.FIELD_TYPE_BUG_ID %]
          [%# bug id %]
          <input type="text" name="[% name FILTER html %]" id="[% name FILTER html %]"
            value="[% value FILTER html %]" [% aria_labelledby_html FILTER none %]>

        [% CASE %]
          [%# error %]
          ('[% name FILTER html %]' [[% field_type FILTER html %])] not supported)

      [% END %]

      [% IF append_content && content.defined %]
        [% content FILTER none %]
      [% END %]
    </div>
  [% END %]
</div>

[%# bug-urls, currently just see-also %]
[% BLOCK bug_urls %]
  [%
    # Process internal (bug) and external links separately
    internal_links = [];
    external_links = [];
    FOREACH value IN values;
      IF value.isa('Bugzilla::BugUrl::Bugzilla::Local');
        internal_links.push(value);
      ELSE;
        external_links.push(value);
      END;
    END;
  %]

  [% IF edit %]
    [% FOREACH link IN internal_links %]
      <div class="link">
        [% link.bug.id FILTER bug_link(link.bug.id, use_alias => 1) FILTER none %]
        <label><input type="checkbox" name="remove_see_also" value="[% link.name FILTER html %]"> Remove</label>
      </div>
    [% END %]
  [% ELSE %]
    [%# Show bugs by type %]
    [% INCLUDE bug_list values=internal_links.pluck('bug') edit=0 %]
  [% END %]

  [% FOREACH link IN external_links %]
    <div class="link">
      <a href="[% link.name FILTER html %]" title="[% link.name FILTER truncate(256) FILTER html %]">
        [%~ link.name FILTER pretty_url FILTER truncate(40) FILTER html ~%]
      </a>
      [% IF edit %]
        <label><input type="checkbox" name="remove_see_also" value="[% link.name FILTER html %]"> Remove</label>
      [% END %]
    </div>
  [% END %]

  [% IF edit %]
    <button type="button" id="[% name FILTER html %]-btn" class="bug-urls-btn minor"
            aria-label="[% 'Add ' _ label FILTER html %]">Add</button>
    <input id="[% name FILTER html %]" name="[% name FILTER html %]" style="display:none"
           aria-label="[% label FILTER html %]">
  [% END %]
[% END %]

[%# bug lists, dependencies, blockers %]
[% BLOCK bug_list %]
  [% IF NOT edit %]
    [% FOREACH type IN bug_fields.bug_type.legal_values.pluck('name') %]
      [%
        bugs = [];
        FOREACH b IN values; bugs.push(b) IF b.bug_type == type; END;
        NEXT UNLESS bugs.size > 0;
      %]
      <div class="bug-list">
        <span class="bug-type-label iconic" title="[% type FILTER html %]"
              aria-label="[% type FILTER html %]" data-type="[% type FILTER html %]">
          <span class="icon" aria-hidden="true"></span>
        </span>
        [% FOREACH b IN bugs %]
          [% INCLUDE bug/link.html.tmpl bug=b link_text=b.id use_alias=1  %]
          [% ", " UNLESS loop.last %]
        [% END %]
      </div>
    [% END %]
  [% ELSE %]
    <input type="text" id="[% name FILTER html %]" name="[% name FILTER html %]"
           value="[% values.pluck('id').join(", ") FILTER html %]" [% aria_labelledby_html FILTER none %]>
  [% END %]
[% END %]
