<div id="<%= AccountConfig::SUBMITTER_INVITATION_EMAIL_KEY %>_form">
  <% template_email_preferences_values = @template.preferences.values_at('request_email_subject', 'request_email_body').compact_blank %>
  <% default_template_email_preferences_values = AccountConfigs.find_or_initialize_for_key(current_account, AccountConfig::SUBMITTER_INVITATION_EMAIL_KEY).value.values_at('subject', 'body') %>
  <% is_custom_template_email = template_email_preferences_values.present? %>
  <% multiple_submitters = @template.submitters.size > 1 && @template.submitters.size < 5 %>
  <% if is_custom_template_email || @template.preferences['submitters'].to_a.any? %>
    <%= button_to nil, template_preferences_path(@template), id: 'submitter_invitation_email_reset_link', method: :delete, class: 'hidden', params: { config_key: AccountConfig::SUBMITTER_INVITATION_EMAIL_KEY }, data: { turbo_confirm: t('are_you_sure_') }, form: { data: { close_on_submit: false } } %>
  <% end %>
  <%= form_for @template, url: template_preferences_path(@template), method: :post, html: { autocomplete: 'off', class: 'mt-1', id: 'submitter_invitation_email_template_form' }, data: { close_on_submit: false } do |f| %>
    <toggle-on-submit data-element-id="email_saved_alert1"></toggle-on-submit>
    <%= tag.input id: 'request_email_per_submitter', value: '1', name: 'request_email_per_submitter', class: 'peer', type: 'checkbox', hidden: true, checked: @template.preferences['submitters'].to_a.size > 1 %>
    <div class="peer-checked:hidden">
      <%= f.fields_for :preferences, Struct.new(:request_email_subject, :request_email_body).new(*(template_email_preferences_values.presence || default_template_email_preferences_values)) do |ff| %>
        <div class="form-control">
          <div class="flex justify-between">
            <%= ff.label :request_email_subject, t('email_subject'), class: 'label' %>
            <% if is_custom_template_email %>
              <label for="submitter_invitation_email_reset_link" class="label underline">
                <%= t('reset_default') %>
              </label>
            <% elsif multiple_submitters %>
              <label for="request_email_per_submitter" class="label underline">
                <%= t('edit_per_party') %>
              </label>
            <% end %>
          </div>
          <%= ff.text_field :request_email_subject, required: true, class: 'base-input', dir: 'auto' %>
        </div>
        <div class="form-control">
          <%= ff.label :request_email_body, t('email_body'), class: 'label' %>
          <%= render 'personalization_settings/markdown_editor', name: ff.field_name(:request_email_body), value: ff.object.request_email_body, variables: AccountConfig::EMAIL_VARIABLES[AccountConfig::SUBMITTER_INVITATION_EMAIL_KEY] %>
        </div>
      <% end %>
    </div>
    <% if multiple_submitters %>
      <div class="hidden peer-checked:block">
        <% options = @template.submitters.map { |e| [e['name'], "request_email_#{e['uuid']}"] } %>
        <toggle-visible data-element-ids="<%= options.map(&:last).to_json %>" class="flex relative px-1">
          <ul class="tabs w-full flex flex-nowrap mb-2">
            <% options.each_with_index do |(label, val), index| %>
              <div class="w-full">
                <%= f.radio_button :selected, val, checked: index.zero?, id: "#{val}_radio", data: { action: 'click:toggle-visible#trigger' }, class: 'hidden peer' %>
                <%= f.label :selected, label, value: val, for: "#{val}_radio", class: 'tab w-full tab-lifted peer-checked:tab-active' %>
              </div>
            <% end %>
          </ul>
        </toggle-visible>
        <% @template.submitters.each_with_index do |submitter, index| %>
          <div id="request_email_<%= submitter['uuid'] %>" class="<%= 'hidden' if index != 0 %>">
            <% submitter_preferences = f.object.preferences['submitters'].to_a.find { |e| e['uuid'] == submitter['uuid'] } || {} %>
            <% submitter_email_preferences_values = submitter_preferences.values_at('request_email_subject', 'request_email_body').compact_blank.presence %>
            <% submitter_email_values = submitter_email_preferences_values || template_email_preferences_values.presence || default_template_email_preferences_values %>
            <%= hidden_field_tag 'template[preferences][submitters][][uuid]', submitter['uuid'] %>
            <div class="form-control">
              <div class="flex justify-between">
                <label class="label"><%= t('email_subject') %></label>
                <% if submitter_email_preferences_values.present? %>
                  <label for="submitter_invitation_email_reset_link" class="label underline">
                    <%= t('reset_default') %>
                  </label>
                <% end %>
              </div>
              <%= text_field_tag 'template[preferences][submitters][][request_email_subject]', submitter_email_values.first, required: true, class: 'base-input', dir: 'auto', autocomplete: 'off' %>
            </div>
            <div class="form-control">
              <label class="label"><%= t('email_body') %></label>
              <%= render 'personalization_settings/markdown_editor', name: 'template[preferences][submitters][][request_email_body]', value: submitter_email_values.last, variables: AccountConfig::EMAIL_VARIABLES[AccountConfig::SUBMITTER_INVITATION_EMAIL_KEY] %>
            </div>
          </div>
        <% end %>
      </div>
    <% end %>
  <% end %>
  <%= form_for @template, url: template_preferences_path(@template), method: :post, html: { autocomplete: 'off', class: 'mt-1' }, data: { close_on_submit: false } do |f| %>
    <toggle-on-submit data-element-id="email_saved_alert1"></toggle-on-submit>
    <%= f.fields_for :preferences, Struct.new(:request_email_enabled).new(@template.preferences['request_email_enabled']) do |ff| %>
      <div class="flex items-center justify-between py-2.5 px-1 mb-2">
        <span>
          <%= t('send_signature_request_email') %>
        </span>
        <submit-form data-on="change" class="flex">
          <%= ff.check_box :request_email_enabled, { checked: ff.object.request_email_enabled != false, class: 'toggle' }, 'true', 'false' %>
        </submit-form>
      </div>
    <% end %>
  <% end %>
  <div class="form-control pt-2">
    <%= button_tag button_title(title: t('save'), disabled_with: t('saving')), form: 'submitter_invitation_email_template_form', class: 'base-button' %>
    <div class="flex justify-center">
      <span id="email_saved_alert1" class="text-sm invisible font-normal mt-0.5"><%= t('changes_have_been_saved') %></span>
    </div>
  </div>
</div>
