{% extends "/layouts/main.twig" %}

{% set active_menu = 'library' %}

{% set strings = {
  delete_success: __("Document has been deleted successfully.")
} %}

{% set xdata %}
list("/library/documents", {{ strings|json_encode }})
{% endset %}

{% block title p__('title', 'Document library')|title %}

{% block template %}
{# Header #}
<div>
  {% include "snippets/back.twig" with {link: 'app/library', label: 'Library'} %}

  <h1 class="mt-4">{{ __('Document library') }}</h1>
</div>

{# Filters #}
{% include "/snippets/filters.twig" with { 
  total: __("Total :count documents"),
  sort: [
    {
      value: null,
      label: p__('label', 'Default')
    },
  
    {
      value: 'created_at',
      label: p__('label', 'Date')
    },
  
    {
      value: 'title',
      label: p__('label', 'Title')
    }
  ]
} %}

{# List #}
<div class="group/list" data-state="initial" :data-state="state">
  <div class="hidden group-data-[state=empty]/list:block">
    {% include "sections/empty.twig" with { title: p__('heading', 'Empty result set'), message: __('There are no documents yet.'), reset: __('There are no documment matching your search.') } %}
  </div>

  <div
    class="hidden md:grid grid-cols-12 gap-3 items-center px-3 py-2 text-content-dimmed text-xs group-data-[state=empty]/list:hidden">
    <div class="col-span-11">{{ p__('label', 'Document') }}</div>
    <div class="col-span-1"></div>
  </div>

  <ul class="text-sm group-data-[state=empty]/list:hidden flex flex-col gap-1">
    {% for i in range(1,5) %}
    <li
      class="hidden md:grid-cols-12 justify-between gap-3 items-center p-3 box group-data-[state=initial]/list:flex md:group-data-[state=initial]/list:grid">
      <div class="flex items-center col-span-11 gap-3">

        <div class="avatar loading">
        </div>

        <div>
          <div class="w-32 h-5 my-0.5 loading"></div>

          <div class="w-20 h-5 my-0.5 loading md:hidden"></div>
        </div>
      </div>

      <div class="md:col-span-1 justify-self-end">
        <i class="ti ti-dots-vertical text-content-dimmed"></i>
      </div>
    </li>
    {% endfor %}

    <template x-for="doc in resources" :key="doc.id">
      <li
        class="relative flex items-center justify-between gap-3 p-3 md:grid md:grid-cols-12 box hover:border-line"
        x-data>
        <a :href="`app/writer/${doc.id}`"
          class="absolute top-0 left-0 w-full h-full cursor-pointer"></a>

        <div class="flex items-center gap-3 md:col-span-11">
          <div class="avatar"
            :style="{backgroundColor: doc.preset ? doc.preset.color : null, color: doc.preset ? '#fff' : null}">

            <template x-if="!doc.preset">
              <span x-text="doc.title.match(/(\b\S)?/g).join('').slice(0, 2)">
              </span>
            </template>

            <template x-if="doc.preset">
              <span>
                <template
                  x-if="doc.preset.image && doc.preset.image.startsWith('<svg')">
                  <div x-html="doc.preset.image"></div>
                </template>

                <template
                  x-if="doc.preset.image && !doc.preset.image.startsWith('<svg')">
                  <i class="ti" :class='`ti-${doc.preset.image}`'></i>
                </template>

                <template x-if="!doc.preset.image">
                  <span
                    x-text="doc.preset.title.match(/(\b\S)?/g).join('').slice(0, 2)">
                  </span>
                </template>
              </span>
            </template>
          </div>

          <div>
            <div x-html="doc.title" class="line-clamp-1">
            </div>

            <div class="mt-1 text-xs text-content-dimmed">
              <x-time :datetime="doc.created_at" data-type="date"></x-time>
            </div>
          </div>
        </div>

        <div class="md:col-span-1 justify-self-end">
          <div class="relative"
            @click.outside="$refs.context.removeAttribute('data-open')">

            <button @click="$refs.context.toggleAttribute('data-open')">
              <i
                class="ti ti-dots-vertical text-content-dimmed hover:text-intermediate-content"></i>
            </button>

            <div class="z-10 menu" x-ref="context">
              <ul>
                <li>
                  <a :href="`app/writer/${doc.id}`"
                    class="flex items-center gap-2 px-4 py-2 hover:no-underline hover:bg-intermediate">
                    <i class="ti ti-pencil"></i>
                    {{ __('Edit') }}
                  </a>
                </li>

                <li>
                  <button
                    class="flex items-center w-full gap-2 px-4 py-2 hover:no-underline hover:bg-intermediate"
                    @click.prevent="currentResource = doc; modal.open('delete-modal')">
                    <i class="ti ti-trash"></i>
                    {{ __('Delete') }}
                  </button>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </li>
    </template>
  </ul>
</div>

{% include "sections/delete-modal.twig" with { 
  message: __('Do you really want to delete :title from documents?'),
  title: 'currentResource.title'
} %}
{% endblock %}