<% has_archived = current_account.templates.where.not(archived_at: nil).exists? %>
<% show_dropzone = params[:q].blank? && @pagy.pages == 1 && ((@template_folders.size < 10 && @templates.size.zero?) || (@template_folders.size < 7 && @templates.size < 4) || (@template_folders.size < 4 && @templates.size < 7)) %>
<% if Docuseal.demo? %><%= render 'shared/demo_alert' %><% end %>
<dashboard-dropzone>
  <%= form_for '', url: '', id: form_id = SecureRandom.uuid, method: :post, class: 'hidden', data: { target: 'dashboard-dropzone.form' }, html: { enctype: 'multipart/form-data' } do %>
    <input name="form_id" value="<%= form_id %>">
    <button type="submit"></button>
    <input id="dashboard_dropzone_input" name="files[]" type="file" multiple>
  <% end %>
  <div class="flex justify-between items-center w-full mb-4 relative">
    <% unless show_dropzone %>
      <%= render 'templates/dashboard_dropzone', style: 'height: 114px' %>
    <% end %>
    <div class="flex items-center flex-grow min-w-0">
      <% if has_archived || @pagy.count.nil? || @pagy.count > 0 || @template_folders.present? %>
        <div class="mr-2">
          <%= render 'dashboard/toggle_view', selected: 'templates' %>
        </div>
      <% end %>
      <h1 class="text-2xl truncate md:text-3xl sm:text-4xl font-bold md:block <%= 'hidden' if params[:q].present? %>">
        <%= t('document_templates_html') %>
      </h1>
    </div>
    <div class="flex space-x-2">
      <% if params[:q].present? || @pagy.pages > 1 || @template_folders.present? %>
        <%= render 'shared/search_input' %>
      <% end %>
      <% if can?(:create, ::Template) %>
        <span class="hidden sm:block">
          <%= render 'templates/upload_button' %>
        </span>
        <%= link_to new_template_path, class: 'white-button !border gap-2', data: { turbo_frame: :modal } do %>
          <%= svg_icon('plus', class: 'w-6 h-6 stroke-2') %>
          <span class="hidden md:block"><%= t('create') %></span>
        <% end %>
      <% end %>
    </div>
  </div>
  <% view_archived_html = capture do %>
    <% if has_archived %>
      <div>
        <a href="<%= templates_archived_index_path %>" class="link text-sm"><%= t('view_archived') %></a>
      </div>
    <% end %>
  <% end %>
  <% templates_order_select_html = capture do %>
    <% if params[:q].blank? && @pagy.pages > 1 %>
      <%= render('shared/templates_order_select', with_recently_used: @pagy.count.present? && @pagy.count < 10_000) %>
    <% end %>
  <% end %>
  <% if @template_folders.present? %>
    <div class="grid gap-4 md:grid-cols-3 <%= 'mb-6' if @templates.present? %>">
      <%= render partial: 'template_folders/folder', collection: @template_folders, as: :folder %>
    </div>
  <% end %>
  <% if @templates.present? %>
    <div class="grid gap-4 md:grid-cols-3">
      <%= render partial: 'templates/template', collection: @templates %>
      <% if show_dropzone && (current_user.created_at > 2.weeks.ago || params[:tour] == 'true') && current_user == true_user %>
        <% user_config = current_user.user_configs.find_or_initialize_by(key: UserConfig::SHOW_APP_TOUR) %>
        <% if user_config.new_record? || user_config.value || params[:tour] == 'true' %>
          <div class="hidden md:block">
            <app-tour id="app_tour" data-show-tour="<%= params[:tour] == 'true' || user_config.value %>" data-type="dashboard" data-next-page-path="<%= @templates.first && can?(:edit, @templates.first) ? edit_template_path(@templates.first, params.permit(:tour)) : settings_account_path %>" data-i18n="<%= t('app_tour').to_json %>"></app-tour>
            <% if user_config.new_record? && !params.key?(:tour) %>
              <div class="h-36 rounded-2xl pt-3 px-7 w-full border border-dashed border-base-300">
                <div class="text-xl text-center font-semibold text-base-content">
                  <%= t('welcome_to_docuseal') %>
                </div>
                <div class="my-2 text-center text-xs text-base-content/70">
                  <%= t('start_a_quick_tour_to_learn_how_to_create_an_send_your_first_document') %>
                </div>
                <div class="flex gap-2 mt-3 w-full">
                  <%= button_to button_title(title: t('skip'), icon_disabled: svg_icon('loader', class: 'w-4 h-4 animate-spin')), user_configs_path, params: { user_config: { key: UserConfig::SHOW_APP_TOUR, value: false } }, class: 'btn btn-sm btn-outline w-full', form_class: 'flex-1', method: :post, form: { onsubmit: 'window.app_tour.parentNode.remove()' } %>
                  <%= button_to t('start_tour'), user_configs_path, params: { user_config: { key: UserConfig::SHOW_APP_TOUR, value: true } }, class: 'btn btn-sm btn-warning  w-full', form_class: 'flex-1', method: :post, form: { onsubmit: 'window.app_tour.start()' } %>
                </div>
              </div>
            <% end %>
          </div>
        <% end %>
      <% end %>
    </div>
  <% end %>
</dashboard-dropzone>
<% if show_dropzone %>
  <%= render 'templates/dropzone' %>
<% end %>
<% if @templates.present? || @template_folders.present? || params[:q].blank? %>
  <% if @pagy.pages > 1 %>
    <%= render 'shared/pagination', pagy: @pagy, items_name: @templates.present? ? 'templates' : 'template_folders', left_additional_html: view_archived_html, right_additional_html: templates_order_select_html %>
  <% else %>
    <div class="mt-2">
      <%= view_archived_html %>
    </div>
  <% end %>
<% elsif params[:q].present? %>
  <div class="text-center">
    <div class="mt-16 text-3xl font-semibold">
      <%= t('templates_not_found') %>
    </div>
  </div>
<% end %>
<%= render 'shared/review_form' %>
