<%# locals: () %>

<section class="multiform-page" id="source-syncing">
  <h3><%= t('scripts.admin.sync_heading') %></h3>

  <%= form_for(@script, url: sync_update_script_path) do |s| %>

    <div class="form-control">
      <% sync_url_field_label = case @script.sync_type
                                when 'manual', 'automatic'; 'scripts.code_syncing_from_url_html'
                                when 'webhook'; 'scripts.code_syncing_from_webhook_html'
                                when nil; 'scripts.provide_code_syncing_from_url_html'
                                else; raise "Unexpected sync type #{@script.sync_type}"
                                end %>
      <%= t(sync_url_field_label, field: s.url_field(:sync_identifier, placeholder: t('scripts.sync_url_placeholder')), site_name: site_name) %>
    </div>

    <div class="radio-group">
      <%= t('scripts.sync_type_intro') %><br>
      <%= s.radio_button(:sync_type, 'automatic') %> <label for="script_sync_type_automatic"><%= t('scripts.sync_name_and_description', name: t('scripts.sync_type_2.name'), description: t('scripts.sync_type_2.description')) %></label><br>
      <%= s.radio_button(:sync_type, 'manual') %> <label for="script_sync_type_manual"><%= t('scripts.sync_name_and_description', name: t('scripts.sync_type_1.name'), description: t('scripts.sync_type_1.description')) %></label><br>
      <% # only allow changing *off* of Webhook - setting it to Webhook is done when a push comes in
      if @script.sync_type == 'webhook' %>
        <%= s.radio_button(:sync_type, 'webhook') %> <label for="script_sync_type_webhook"><%= t('scripts.sync_name_and_description', name: t('scripts.sync_type_3.name'), description: t('scripts.sync_type_3.description')) %></label><br>
      <% end %>
    </div>

    <% case @script.sync_type %>
    <% when nil %>
      <p>
        <%= it('scripts.sync_webhook_info_without_sync', site_name: site_name, webhook_info_link: user_webhook_info_path) %>
      </p>
    <% when 'manual', 'automatic' %>
      <p>
        <%= it('scripts.sync_webhook_info_with_sync', site_name: site_name, webhook_info_link: user_webhook_info_path) %>
      </p>
    <% end %>

    <div>
      <p><%= t('scripts.sync_additional_info') %></p>
      <div class="indented">
        <% localized_additional_infos = @script.localized_attributes_for('additional_info').select{|la| la.attribute_default || !la.sync_identifier.nil? || la.new_record?}
        localized_additional_infos.each_with_index do |la, index| %>
          <%= render partial: 'sync_additional_info', locals: {la: la, index: index, preview: @preview.nil? ? nil : @preview[index]} %>
        <% end %>
        <div>
          <button id="add-synced-additional-info" name="add-synced-additional-info" value="1" type="submit" data-form-path="<%= script_sync_additional_info_form_path %>"><%= t('scripts.add_synced_additional_info') %></button>
        </div>
      </div>
    </div>

    <% if @script.sync_type %>
      <p>
        <% if !@script_sync_error.nil? %>
          <%= t('scripts.sync_failed_info_html', date: markup_date(@script.last_attempted_sync_date), error: @script.sync_error) %>
        <% end %>
        <% if @script.last_successful_sync_date.nil? %>
          <%= t('scripts.sync_never_succeeded') %>
        <% else %>
          <%= t('scripts.sync_success_info_html', date: markup_date(@script.last_successful_sync_date)) %>
        <% end %>
      </p>
    <% end %>

    <p>
      <% if @script.sync_type.nil? %>
        <input type="submit" name="update-and-sync" value="<%= t('scripts.update_and_sync') %>">
      <% else %>
        <input type="submit" name="update-only" value="<%= t('scripts.update_no_sync') %>">
        <input type="submit" name="update-and-sync" value="<%= t('scripts.update_and_sync') %>">
        <input type="submit" name="stop-syncing" value="<%= t('scripts.turn_off_syncing') %>">
      <% end %>
    </p>

  <% end %>
</section>
