<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:tal="http://xml.zope.org/namespaces/tal"
  xmlns:metal="http://xml.zope.org/namespaces/metal"
  xmlns:i18n="http://xml.zope.org/namespaces/i18n"
  i18n:domain="senaite.core">

  <head tal:define="portal context/@@plone_portal_state/portal;">
      <script type="text/javascript"
          tal:attributes="src python:portal.absolute_url() + '/senaite_widgets/datetimewidget.js'"></script>
      <script type="text/javascript"
          tal:attributes="src python:portal.absolute_url() + '/senaite_widgets/referencewidget.js'"></script>
  </head>

  <body
    tal:define="senaite_view python:context.restrictedTraverse('@@senaite_view');
                test nocall:senaite_view/test;
                root_sample python:view.is_primary_with_partitions();
                dummy python:view.get_fields_grouped_by_location();
                prominent python:dummy[0];
                sublists python:dummy[1];
                errors python:request.get('errors',{})"
    tal:condition="sublists">
    <tal:comment condition="python:False">
      <!--
        Note: the `novalidate` attribute is needed to avoid browser validation
        when e.g. the "expected sampling date" has passed and another value
        wants to be changed.

        https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Constraint_validation
      -->
    </tal:comment>
    <form novalidate method="post" name="header_form">
      <input type="hidden" name="header_table_submitted" value="1" />

      <table class="header_table table table-condensed table-bordered table-sm">

        <!-- Render "Prominent" Fields -->
        <tr tal:repeat="action prominent">
          <tal:def define="mode python:action['mode'];
                           fieldName python:action['fieldName'];
                           field python:context.Schema()[fieldName];
                           field_macro here/widgets/field/macros/edit;
                           editable python:mode=='edit';
                           primary_bound python:getattr(field, 'primary_bound', False);
                           widget python:field.widget;
                           accessor python:field.getAccessor(context);">
            <td class="active label">
              <span tal:replace="python:widget.label"/>
              <!-- Display a notification icon if the field is primary bound
                   (if changes propagate to partitions) -->
              <tal:primary_bound condition="python:all([root_sample, editable, primary_bound])">
                <span title="Changes will be propagated to partitions"
                      i18n:attributes="title"
                      class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
              </tal:primary_bound>
              <!-- Display Required Icon -->
              <span class="fieldRequired"
                    tal:condition="python:field.required and editable"
                    title="Required"
                    i18n:attributes="title title_required;"> </span>
            </td>
            <td colspan="5">
              <!-- Render the Field -->
              <tal:x condition="python:mode!='structure'">
                <metal:field use-macro="python:context.widget(fieldName, mode=mode)"/>
              </tal:x>
              <span
                tal:condition="python:mode=='structure' and action['html']"
                tal:content="structure action/html"/>
            </td>
          </tal:def>
        </tr>

        <!-- Render "Standard" Fields -->
        <tr tal:repeat="row_nr python:range(len(sublists[0]))">
          <tal:repeat repeat="col_nr python:range(3)">
            <tal:print_blanks condition="python:len(sublists[col_nr]) <= row_nr">
              <td tal:attributes="class python:row_nr%2 and 'key odd' or 'key even'"></td>
              <td tal:attributes="class python:row_nr%2 and 'value odd' or 'value even'"></td>
            </tal:print_blanks>
            <tal:skip_blanks condition="python:len(sublists[col_nr]) > row_nr">
              <tal:def define="action python:sublists[col_nr][row_nr];
                               mode python:action['mode'];
                               fieldName python:action['fieldName'];
                               field python:context.Schema()[fieldName];
                               field_macro here/widgets/field/macros/edit;
                               editable python:mode=='edit';
                               primary_bound python:getattr(field, 'primary_bound', False);
                               widget python:field.widget;
                               accessor python:field.getAccessor(context);">
                <td tal:attributes="class python:row_nr%2 and 'key odd active label' or 'key even active label'">
                  <span tal:replace="python:widget.label"/>
                  <!-- Display a notification icon if the field is primary bound
                       (if changes propagate to partitions) -->
                  <tal:primary_bound condition="python:all([root_sample, editable, primary_bound])">
                    <span title="Changes will be propagated to partitions"
                          i18n:attributes="title"
                          class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                  </tal:primary_bound>
                  <!-- Display Required Icon -->
                  <span class="fieldRequired"
                        tal:condition="python:field.required and editable"
                        title="Required"
                        i18n:attributes="title title_required;"> </span>
                </td>
                <td tal:attributes="class python:row_nr%2 and 'value odd' or 'value even'">
                  <!-- Render Field -->
                  <tal:x condition="python:mode!='structure'">
                    <metal:field
                      use-macro="python:context.widget(fieldName, mode=mode)"/>
                  </tal:x>
                  <span
                    tal:condition="python:mode=='structure' and action['html']"
                    tal:content="structure action/html"/>

                </td>
              </tal:def>
            </tal:skip_blanks>
          </tal:repeat>
        </tr>
      </table>
      <input tal:condition="view/is_edit_allowed"
             class="context btn btn-sm btn-primary"
             type="submit"
             name="form.button.save"
             value="Save"
             i18n:domain="plone"
             i18n:attributes="value label_save;"/>
    </form>
  </body>
</html>
