<% content_for :title, 'Imports' %>

<div class="w-full my-5">
  <div class="flex justify-between items-center">
    <h1 class="font-bold text-4xl">Imports</h1>
    <%= link_to "New import", new_import_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>

    <% if current_user.safe_settings.immich_url && current_user.safe_settings.immich_api_key %>
      <%= link_to 'Import Immich data', settings_background_jobs_path(job_name: 'start_immich_import'), method: :post, data: { turbo_confirm: 'Are you sure?', turbo_method: :post }, class: 'rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium' %>
    <% else %>
      <a href='' class="rounded-lg py-3 px-5 bg-blue-900 text-gray-500 block font-medium tooltip cursor-not-allowed" data-tip="You need to provide your Immich instance data in the Settings">Import Immich data</a>
    <% end %>
    <% if current_user.safe_settings.photoprism_url && current_user.safe_settings.photoprism_api_key %>
      <%= link_to 'Import Photoprism data', settings_background_jobs_path(job_name: 'start_photoprism_import'), method: :post, data: { turbo_confirm: 'Are you sure?', turbo_method: :post }, class: 'rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium' %>
    <% else %>
      <a href='' class="rounded-lg py-3 px-5 bg-blue-900 text-gray-500 block font-medium tooltip cursor-not-allowed" data-tip="You need to provide your Photoprism instance data in the Settings">Import Photoprism data</a>
    <% end %>
  </div>

  <div id="imports" class="min-w-full">
    <% if @imports.empty? %>
      <div class="hero min-h-80 bg-base-200 my-5">
        <div class="hero-content text-center">
          <div class="max-w-md">
            <h1 class="text-5xl font-bold">Hello there!</h1>
            <p class="py-6">
              Here you'll find your imports, but now there are none. Let's <%= link_to 'create one', new_import_path, class: 'link' %>!
            </p>
          </div>
        </div>
      </div>
    <% else %>
      <div class="flex justify-center my-5">
        <div class='flex'>
          <%= paginate @imports %>
        </div>
      </div>
      <div class="overflow-x-auto">
        <table class="table table-zebra overflow-x-auto">
          <thead>
            <tr>
              <th>Name</th>
              <th>File size</th>
              <th>Imported points</th>
              <th>Status</th>
              <th>Created at</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody
            data-controller="imports"
            data-imports-target="index"
            data-user-id="<%= current_user.id %>"
          >
            <% @imports.each do |import| %>
              <tr data-import-id="<%= import.id %>"
                  id="import-<%= import.id %>"
                  data-points-total="<%= import.processed %>"
                  class="hover">
                <td>
                  <%= link_to import.name, import, class: 'underline hover:no-underline' %>
                  (<%= import.source %>)
                  &nbsp;
                  <%= link_to '🗺️', map_path(import_id: import.id) %>
                  &nbsp;
                  <%= link_to '📋', points_path(import_id: import.id) %>
                </td>
                <td><%= number_to_human_size(import.file&.byte_size) || 'N/A' %></td>
                <td data-points-count>
                  <%= number_with_delimiter import.processed %>
                </td>
                <td data-status-display><%= import.status %></td>
                <td><%= human_datetime(import.created_at) %></td>
                <td class="whitespace-nowrap">
                  <% if import.deleting? %>
                    <span class="loading loading-spinner loading-sm"></span>
                    <span class="text-sm text-gray-500">Deleting...</span>
                  <% else %>
                    <% if import.file.present? %>
                      <%= link_to 'Download', rails_blob_path(import.file, disposition: 'attachment'), class: "btn btn-outline btn-sm btn-info", download: import.name %>
                    <% end %>
                    <%= link_to 'Delete', import, data: { turbo_confirm: "Are you sure?", turbo_method: :delete }, method: :delete, class: "btn btn-outline btn-sm btn-error" %>
                  <% end %>
                </td>
              </tr>
            <% end %>
          </tbody>
        </table>
      </div>
      <div class="flex justify-center my-5">
        <div class='flex'>
          <%= paginate @imports %>
        </div>
      </div>
    <% end %>
  </div>
</div>
