<% close_on_submit = local_assigns.fetch(:close_on_submit, true) %>
<%= form_for template, url: template_recipients_path(template), method: :post, html: { autocomplete: 'off', class: 'mt-1', id: :submitters_form }, data: { close_on_submit: } do |f| %>
  <% unless close_on_submit %>
    <toggle-on-submit data-element-id="form_saved_alert"></toggle-on-submit>
  <% end %>
  <div class="space-y-3 divide-y">
    <% template.submitters.each_with_index do |submitter, index| %>
      <div class="<%= 'pt-3' if index.positive? %>">
        <%= f.fields_for :submitters, item = Struct.new(:name, :uuid, :is_requester, :email, :invite_by_uuid, :optional_invite_by_uuid, :linked_to_uuid, :option).new(*submitter.values_at('name', 'uuid', 'is_requester', 'email', 'invite_by_uuid', 'optional_invite_by_uuid', 'linked_to_uuid')), index: do |ff| %>
          <% item.option = item.is_requester.present? ? 'is_requester' : (item.email.present? ? 'email' : (item.linked_to_uuid.present? ? "linked_to_#{item.linked_to_uuid}" : (item.invite_by_uuid.present? ? "invite_by_#{item.invite_by_uuid}" : (item.optional_invite_by_uuid.present? ? "optional_invite_by_#{item.optional_invite_by_uuid}" : '')))) %>
          <%= ff.hidden_field :uuid %>
          <div class="form-control">
            <%= ff.text_field :name, class: 'w-full outline-none border-transparent focus:border-transparent focus:ring-0 bg-base-100 px-1 peer mb-2', autocomplete: 'off', placeholder: "#{index + 1}#{(index + 1).ordinal} Party", required: true %>
            <% if template.submitters.size == 2 %>
              <%= tag.input name: ff.field_name(:email), value: ff.object.email, type: :email, class: 'base-input', multiple: true, autocomplete: 'off', placeholder: t('default_email'), disabled: ff.object.is_requester || ff.object.invite_by_uuid.present? || ff.object.optional_invite_by_uuid.present?, id: field_uuid = SecureRandom.uuid %>
            <% else %>
              <toggle-attribute data-target-id="<%= email_field_uuid = SecureRandom.uuid %>" data-class-name="hidden" data-value="email">
                <%= ff.select :option, [[t('not_specified'), 'not_set'], (local_assigns[:with_submission_requester] == false ? nil : [t('submission_requester'), 'is_requester']), [t('specified_email'), 'email'], *(template.submitters - [submitter]).flat_map { |e| [[t('invite_by_name', name: e['name']), "invite_by_#{e['uuid']}"], [t('invite_by_name', name: e['name']) + " (#{t(:optional).capitalize})", "optional_invite_by_#{e['uuid']}"]] }, *(template.submitters - [submitter]).map { |e| [t('same_as_name', name: e['name']), "linked_to_#{e['uuid']}"] }].compact, {}, class: 'base-select mb-3' %>
              </toggle-attribute>
              <%= tag.input name: ff.field_name(:email), type: :email, value: ff.object.email, multiple: true, class: "base-input #{'hidden' if item.option != 'email'}", autocomplete: 'off', placeholder: t('default_email'), id: email_field_uuid %>
            <% end %>
          </div>
          <% if template.submitters.size == 2 %>
            <checkbox-group class="mt-3 flex items-center space-x-4">
              <% if local_assigns[:with_submission_requester] != false %>
                <label class="flex items-center space-x-2 cursor-pointer">
                  <toggle-attribute data-target-id="<%= field_uuid %>" class="flex" data-attribute="disabled">
                    <%= ff.check_box :is_requester, class: 'base-checkbox' %>
                  </toggle-attribute>
                  <span class="select-none">
                    <%= t('submission_requester') %>
                  </span>
                </label>
              <% end %>
              <% if index == 1 %>
                <label class="flex items-center space-x-2 cursor-pointer">
                  <toggle-attribute data-target-id="<%= field_uuid %>" class="flex" data-attribute="disabled">
                    <indeterminate-checkbox data-indeterminate="<%= ff.object.optional_invite_by_uuid.present? %>" data-show-indeterminate-id="invite_optional" data-name="<%= ff.field_name(:invite_by_uuid) %>" data-indeterminate-name="<%= ff.field_name(:optional_invite_by_uuid) %>" class="flex">
                      <%= ff.check_box ff.object.optional_invite_by_uuid.present? ? :optional_invite_by_uuid : :invite_by_uuid, { class: 'base-checkbox' }, template.submitters.first['uuid'], '' %>
                    </indeterminate-checkbox>
                  </toggle-attribute>
                  <span class="select-none">
                    <%= t('invite_by_name', name: template.submitters.first['name']) %> <span id="invite_optional" class="<%= 'hidden' if ff.object.optional_invite_by_uuid.blank? %>">(<%= t(:optional).capitalize %>)</span>
                  </span>
                </label>
              <% end %>
            </checkbox-group>
          <% end %>
        <% end %>
      </div>
    <% end %>
  </div>
<% end %>
<% if local_assigns[:with_toggles] != false %>
  <% unless current_account.account_configs.exists?(key: AccountConfig::ENFORCE_SIGNING_ORDER_KEY, value: true) %>
    <%= form_for template, url: template_preferences_path(template), method: :post, html: { autocomplete: 'off', class: 'mt-2' }, data: { close_on_submit: false } do |f| %>
      <div class="flex items-center pt-4 mt-4 justify-between border-t w-full">
        <span>
          <%= t('enforce_recipients_order') %>
        </span>
        <%= f.fields_for :preferences, Struct.new(:submitters_order).new(template.preferences['submitters_order']) do |ff| %>
          <%= ff.check_box :submitters_order, { class: 'toggle', onchange: 'this.form.requestSubmit()' }, 'preserved', '' %>
        <% end %>
      </div>
    <% end %>
  <% end %>
  <% if can?(:manage, :personalization_advanced) %>
    <%= form_for template, url: template_preferences_path(template), method: :post, html: { autocomplete: 'off', class: 'mt-2' }, data: { close_on_submit: false } do |f| %>
      <div class="flex items-center mt-4 justify-between w-full">
        <span>
          <%= t('ensure_unique_recipients') %>
        </span>
        <%= f.fields_for :preferences, Struct.new(:validate_unique_submitters).new(template.preferences['validate_unique_submitters']) do |ff| %>
          <%= ff.check_box :validate_unique_submitters, { class: 'toggle', onchange: 'this.form.requestSubmit()' }, 'true', '' %>
        <% end %>
      </div>
    <% end %>
  <% end %>
<% end %>
<div class="form-control mt-5">
  <%= button_tag button_title(title: t('save'), disabled_with: t('updating')), class: 'base-button', form: :submitters_form %>
  <% unless close_on_submit %>
    <div class="flex justify-center">
      <span id="form_saved_alert" class="text-sm invisible font-normal mt-0.5"><%= t('changes_have_been_saved') %></span>
    </div>
  <% end %>
</div>
