<template x-if="orders.length > 0">
  <section class="flex flex-col gap-6 box " data-density="comfortable">
    <div class="flex items-center gap-2">
      <h2>{{ p__('heading', 'Latest orders') }}</h2>

      <a class="badge" :href="`admin/orders?sort=created_at:desc`">
        {{ p__('button', 'View all') }}
      </a>
    </div>

    <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-3">{{ p__('label', 'Order') }}</div>
        <div class="col-span-2">{{ p__('label', 'Status') }}</div>
        <div class="col-span-2">{{ p__('label', 'Credits') }}</div>
        <div class="col-span-2">{{ p__('label', 'Total') }}</div>
        <div class="col-span-2">{{ p__('label', 'Created') }}</div>
        <div class="col-span-1"></div>
      </div>

      <ul class="text-sm flex flex-col gap-1 group-data-[state=empty]:hidden">
        <template x-for="order in orders" :key="order.id">
          <li
            class="relative grid items-center grid-cols-12 gap-3 p-3 box hover:border-line"
            x-data>
            <a :href="`admin/orders/${order.id}`"
              class="absolute top-0 left-0 w-full h-full cursor-pointer"></a>

            <div class="flex items-center col-span-11 gap-3 md:col-span-3">
              <div>
                <div class="flex items-center gap-2">
                  <div class="flex items-center">
                    <span class="font-bold" x-text="order.plan.title"></span>

                    <i
                      class="ti ti-direction-horizontal text-content-dimmed"></i>

                    <template x-if="order.plan.billing_cycle == 'monthly'">
                      <span>{{ p__('billing-cycle', 'Monthly') }}</span>
                    </template>

                    <template x-if="order.plan.billing_cycle == 'yearly'">
                      <span>{{ p__('billing-cycle', 'Yearly') }}</span>
                    </template>

                    <template x-if="order.plan.billing_cycle == 'lifetime'">
                      <span>{{ p__('billing-cycle', 'Lifetime') }}</span>
                    </template>

                    <template x-if="order.plan.billing_cycle == 'one-time'">
                      <span>{{ p__('billing-cycle', 'Add-on credit') }}</span>
                    </template>
                  </div>
                </div>

                <div class="text-xs text-content-dimmed mt-0.5">
                  {% set name %}
                  <a :href="`admin/workspaces/${order.workspace.id}`"
                    x-text="order.workspace.name"
                    class="relative hover:underline text-content"></a>
                  {% endset %}

                  {{ __('Workspace: :name')|replace({':name' : name})|raw }}
                </div>
              </div>
            </div>

            <div class="hidden md:block md:col-span-2">
              <template x-if="order.status == 'completed'">
                <span>{{ p__('order-status', 'Completed') }}</span>
              </template>

              <template x-if="order.status == 'pending'">
                <span>{{ p__('order-status', 'Pending') }}</span>
              </template>

              <template x-if="order.status == 'failed'">
                <span>{{ p__('order-status', 'Failed') }}</span>
              </template>
            </div>

            <div class="hidden md:block md:col-span-2">
              <x-credit :data-value="order.plan.credit_count"></x-credit>
            </div>

            <div class="hidden md:block md:col-span-2">
              <div class="font-bold">
                <x-money :data-value="order.total"
                  :currency="order.currency.code"
                  :minor-units="order.currency.fraction_digits"></x-money>
              </div>

              <div class="text-xs text-content-dimmed mt-0.5"
                x-text="order.plan.billing_cycle == 'monthly' ? `{{ __('per month') }}` : order.plan.billing_cycle == 'yearly' ? `{{ __('per year') }}` : `{{ __('one-time') }}`">
              </div>
            </div>

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

            <div class="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="ti ti-dots-vertical text-content-dimmed hover:text-intermediate-content"></i>
                </button>

                <div class="menu" x-ref="context">
                  <ul>
                    <li>
                      <a :href="`admin/orders/${order.id}`"
                        class="flex items-center gap-2 px-4 py-2 hover:no-underline hover:bg-intermediate">
                        {{ p__('button', 'Details') }}
                      </a>
                    </li>
                  </ul>
                </div>
              </div>
            </div>
          </li>
        </template>
      </ul>
    </div>
  </section>
</template>