[%# 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.
  #%]

[%#
  # type: tracking flag type (e.g. "project", "tracking")
  #%]

[%
  flags = [];
  set_flags = [];
  FOREACH flag IN tracking_flags;
    NEXT UNLESS flag.flag_type == type;
    flags.push(flag);
    NEXT IF flag.bug_flag(bug.id).value == "---";
    set_flags.push(flag);
  END;
  RETURN UNLESS flags.size;
%]

[%# view %]
[% IF set_flags.size %]
  <div class="flags edit-hide">
    <table class="layout-table tracking-flags">
      [% IF type == "tracking" %]
        <tr>
          <th></th>
          <th>Tracking</th>
          <th>Status</th>
        </tr>
      [% END %]
      [% FOREACH row IN tracking_flags_table %]
        [%
          NEXT UNLESS row.type == type;
          tracking_value = row.tracking.bug_flag(bug_id).value || "---";
          status_value = row.status.bug_flag(bug_id).value || "---";
          NEXT IF tracking_value == "---" && status_value == "---";
        %]
        <tr>
          <td class="tracking-flag-name">[% row.name FILTER html %]</td>
          [% IF type == "tracking" %]
            <td class="tracking-flag-tracking">
              [% IF tracking_value != '---' %]
                <a href="[% basepath FILTER none %]buglist.cgi?f1=[% row.tracking.name FILTER uri ~%]
                  &amp;o1=equals&amp;v1=[% tracking_value FILTER uri %]">
              [% END %]
              [%~ tracking_value FILTER html ~%]
              [% '</a>' IF tracking_value != '---' %]
            </td>
          [% END %]
          <td class="tracking-flag-status">
            [% IF status_value != '---' %]
              <a href="[% basepath FILTER none %]buglist.cgi?f1=[% row.status.name FILTER uri ~%]
                &amp;o1=equals&amp;v1=[% status_value FILTER uri %]">
            [% END %]
            [%~ status_value FILTER html ~%]
            [% '</a>' IF status_value != '---' %]
          </td>
        </tr>
      [% END %]
    </table>
  </div>
[% END %]

[%# edit %]
<div class="flags edit-show" style="display:none">
  <table class="layout-table tracking-flags">
    [% IF type == "tracking" %]
      <tr>
        <th></th>
        <th>Tracking</th>
        <th>Status</th>
      </tr>
    [% END %]
    [% FOREACH row IN tracking_flags_table %]
      [% NEXT UNLESS row.type == type %]
      <tr>
        <td class="tracking-flag-name">[% row.name FILTER html %]</td>
        [% IF type == "tracking" %]
          <td class="tracking-flag-tracking">[% INCLUDE tf_select flag=row.tracking %]</td>
        [% END %]
        <td class="tracking-flag-status">[% INCLUDE tf_select flag=row.status %]</td>
      </tr>
    [% END %]
  </table>
</div>

[% BLOCK tf_select %]
  [% RETURN UNLESS flag %]
  <input type="hidden" id="[% flag.name FILTER html %]-dirty">
  <select id="[% flag.name FILTER html %]" name="[% flag.name FILTER html %]">
    [%
      flag_bug_value = flag.bug_flag(bug_id).value;
      FOREACH value IN flag.values;
        IF value.name != flag_bug_value;
          NEXT IF !value.is_active || !flag.can_set_value(value.name);
        END;
    %]
        <option value="[% value.name FILTER html %]"
          id="v[% value.id FILTER html %]_[% flag.name FILTER html %]"
          [% " selected" IF flag_bug_value == value.name %]
        >
          [% value.name FILTER html %]
        </option>
    [% END %]
  </select>
[% END %]
