<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:use-macro="here/main_template/macros/master"
      i18n:domain="senaite.core">
  <head>

    <metal:block fill-slot="senaite_legacy_resources"
                 tal:define="portal context/@@plone_portal_state/portal;">
      <link rel="stylesheet" type="text/css" media="all" href=""
            tal:attributes="href string:${portal/absolute_url}/++plone++senaite.core.static/css/bika.lims.analysisrequest.add.css"/>
      <style>
       #fieldlist {
       }
       #fieldlist li {
           cursor: ns-resize;
       }
       #fieldlist li input {
           vertical-align: middle;
       }
      </style>
      <script>
       document.addEventListener("DOMContentLoaded", () => {
         $("#fieldlist").sortable();
         $("#fieldlist").disableSelection();
       });
      </script>
    </metal:block>
  </head>
  <body>

    <metal:title fill-slot="content-title">
      <h1 i18n:translate="">
        Manage Sample Form Fields
      </h1>
    </metal:title>
    <metal:description fill-slot="content-description">
      <p i18n:translate="">
        Manage the order and visibility of the fields displayed in analysis
        request add forms.
      </p>
      <p i18n:translate="" class="discreet">
        Note: The settings apply to all Sample Add forms; Required fields can not be deselected.
      </p>
    </metal:description>

    <div metal:fill-slot="content-core"
         tal:define="portal context/@@plone_portal_state/portal;">

      <form id="analysisrequest_manage_add_form"
            name="analysisrequest_manage_add_form"
            method="POST">

        <input type="hidden" name="submitted" value="1"/>
        <span tal:replace="structure context/@@authenticator/authenticator"/>

        <ul class="list-group mb-3" id="fieldlist">
          <tal:field tal:repeat="field python:view.get_fields_with_visibility('edit')">
            <tal:def define="fieldName python:field.getName();
                             required python:field.required;
                             widget python:field.widget;">
              <li class="list-group-item edit_field"
                  tal:define="fieldname python:field.getName()">

                <!-- Field Order -->
                <input type="hidden"
                       tal:attributes="name string:order:list;
                                       value string:${fieldName}"/>
                <!-- Field Visibility -->
                <input type="hidden"
                       tal:attributes="name string:visibility.${fieldName}:record:boolean:default;
                                       value python:False"/>
                <input type="checkbox"
                       tal:attributes="name string:visibility.${fieldName}:record:boolean;
                                       value python:True;
                                       checked python:view.is_field_visible(field);
                                       disabled python:required and 'disabled' or ''"/>

                <span tal:replace="python:view.context.translate(widget.Label(here))"/>
                <span class="fieldRequired"
                      tal:condition="required"
                      title="Required"
                      i18n:attributes="title title_required;">&nbsp;</span>
              </li>
            </tal:def>
          </tal:field>

          <tal:field tal:repeat="field python:view.get_fields_with_visibility('hidden')">
            <tal:def define="fieldName python:field.getName();
                             required python:field.required;
                             widget python:field.widget;">
              <li class="list-group-item hidden_field"
                  tal:define="fieldname python:field.getName()">

                <!-- Field Order -->
                <input type="hidden"
                       tal:attributes="name string:order:list;
                                       value string:${fieldName}"/>
                <!-- Field Visibility -->
                <input type="hidden"
                       tal:attributes="name string:visibility.${fieldName}:record:boolean:default;
                                       value python:False"/>
                <input type="checkbox"
                       tal:attributes="name string:visibility.${fieldName}:record:boolean;
                                       value python:True;
                                       checked python:view.is_field_visible(field);
                                       disabled python:required and 'disabled' or ''"/>

                <span tal:replace="python:view.context.translate(widget.Label(here))"/>
                <span class="fieldRequired"
                      tal:condition="required"
                      title="Required"
                      i18n:attributes="title title_required;">&nbsp;</span>
              </li>
            </tal:def>
          </tal:field>
        </ul>

        <input class="btn btn-success btn-sm allowMultiSubmit"
               type="submit"
               name="save"
               i18n:attributes="value"
               value="Save"/>

        <input class="btn btn-warning btn-sm allowMultiSubmit"
               type="submit"
               name="reset"
               i18n:attributes="value"
               value="Reset"/>

      </form>

    </div>

  </body>
</html>
