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

{% set active_menu = 'categories' %}

{% set strings = {
  delete_success: __('Category has been deleted successfully.'),
} %}

{% set xdata %}
list("categories", {{ strings|json_encode }})
{% endset %}

{% block title p__('title', 'Categories')|title %}

{% block template %}
{# Header #}
<div class="flex items-center justify-between">
  <div>
    <h1>{{ p__('heading', 'Categories') }}</h1>

    <template x-if="total !== null">
      <div class="text-sm text-content-dimmed md:hidden">
        {{ __('Total :count categories')|replace({':count': '<span x-text="total"></span>'})|raw }}
      </div>
    </template>
  </div>

  <div class="flex items-center gap-2 md:hidden">
    <button type="button" class="hidden w-8 h-8 avatar">
      <i class="text-lg ti ti-adjustments-horizontal"></i>
    </button>

    <a href="admin/categories/new"
      class="w-8 h-8 rounded-full button button-accent button-sm">
      <i class="text-lg ti ti-plus"></i>
    </a>
  </div>

  <a href="admin/categories/new"
    class="hidden md:flex button button-accent button-sm">
    {{ p__('button', 'Create new category') }}
  </a>
</div>

{# Filters #}
{% include "/snippets/filters.twig" with { 
  total: __('Total :count categories'),
  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="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 categories yet.'), reset: __('There are no categories matching your search.') } %}
  </div>

  <div
    class="hidden md:grid md: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-9">{{ p__('label', 'Name') }}</div>
    <div class="col-span-2">{{ p__('label', 'Created') }}</div>
    <div class="col-span-1"></div>
  </div>

  <ul class="text-sm group-data-[state=empty]: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 gap-3 md:col-span-9">
        <div class="avatar loading"></div>

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

      <div class="hidden col-span-2 md:block">
        <span class="inline-block w-20 h-5 my-0.5 loading"></span>
      </div>

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

    <template x-for="category in resources" :key="category.id">
      <li
        class="relative flex items-center justify-between gap-3 p-3 md:grid-cols-12 md:grid box hover:border-line"
        x-data>

        <a :href="`admin/categories/${category.id}`"
          class="absolute top-0 left-0 w-full h-full cursor-pointer"></a>

        <div class="flex items-center gap-3 md:col-span-9">
          <div class="avatar">
            <span
              x-text="category.title.match(/(\b\S)?/g).join('').slice(0, 2)"></span>
          </div>

          <div>
            <div class="font-bold" x-text="category.title">
            </div>

            <div class="text-content-dimmed md:hidden">
              <x-time :datetime="category.created_at" data-type="date"></x-time>
            </div>
          </div>
        </div>

        <div class="hidden md:block md:col-span-2">
          <x-time :datetime="category.created_at" data-type="date"></x-time>
        </div>

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

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

            <div class="menu" x-ref="context">
              <ul>
                <li><a :href="`admin/categories/${category.id}`"
                    class="flex items-center gap-2 px-4 py-2 hover:no-underline hover:bg-intermediate">
                    <i class="ti ti-pencil"></i>
                    {{ p__('button', '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 = category; modal.open('delete-modal')">
                    <i class="ti ti-trash"></i>
                    {{ p__('button', '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 categories?'),
  title: 'currentResource.title'
} %}
{% endblock %}