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

{% set active_menu = '/admin/coupons' %}

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

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

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

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

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

		<div class="flex gap-2 items-center 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/coupons/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/coupons/new" class="hidden md:flex button button-accent button-sm">
			{{ p__('button', 'Create new coupon') }}
		</a>
	</div>

	{# Filters #}
	{% include "/snippets/filters.twig" with { 
      total: __("Total :count coupons"),
      filters: [
        {
          label: p__('label', 'Status'),
          model: 'status',
          options: [
            {
              value: '0',
              label: p__('status', 'Inactive')
            },
            {
              value: '1',
              label: p__('status', 'Active')
            }
          ]
        }
      ],
      sort: [
        {
          value: null,
          label: p__('label', 'Default')
        },

        {
          value: 'title',
          label: p__('label', 'Title')
        },
      
        {
          value: 'created_at',
          label: p__('label', 'Date')
        }
      ]
    } 
  %}

	{# 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 coupons yet.'), reset: __('There are no coupons 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-3">{{ p__('label', 'Title') }}</div>
			<div class="col-span-3">{{ p__('label', 'Code') }}</div>
			<div class="col-span-2">{{ p__('label', 'Amount') }}</div>
			<div class="col-span-2">{{ p__('label', 'Created') }}</div>
			<div class="col-span-1">{{ p__('label', 'Status') }}</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 gap-3 items-center justify-between p-3 box group-data-[state=initial]/list:flex md:group-data-[state=initial]/list:grid">
					<div class="flex col-span-3 gap-3 items-center">
						<div class="avatar loading"></div>

						<div>
							<div class="flex gap-2 items-center">
								<span class="w-20 h-5 loading"></span>
							</div>

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

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

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

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

					<div class="hidden md:block md:col-span-1"></div>

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

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

					<div class="flex col-span-3 gap-3 items-center">
						<x-avatar :title="coupon.title"></x-avatar>

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

							<div class="text-content-dimmed md:hidden">
								<template x-if="coupon.discount_type == 'fixed'">
									<x-money :data-value="coupon.amount" currency="{{ currency.code }}" minor-units="{{ currency.fraction_digits }}"></x-money>
								</template>

								<template x-if="coupon.discount_type == 'percentage'">
									<span x-text="`${coupon.amount / 100}%`"></span>
								</template>
							</div>
						</div>
					</div>

					<div class="hidden md:flex md:col-span-3">
						<x-copy class="relative z-100 badge" :data-copy="coupon.code" x-text="coupon.code"></x-copy>
					</div>

					<div class="hidden md:block md:col-span-2">
						<template x-if="coupon.discount_type == 'fixed'">
							<x-money :data-value="coupon.amount" currency="{{ currency.code }}" minor-units="{{ currency.fraction_digits }}"></x-money>
						</template>

						<template x-if="coupon.discount_type == 'percentage'">
							<span x-text="`${coupon.amount / 100}%`"></span>
						</template>
					</div>

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

					<div class="hidden md:block md:col-span-1">
						<label class="inline-flex relative z-10 gap-2 items-center cursor-pointer">
							<input type="checkbox" name="status" class="hidden peer" :checked="coupon.status == 1" @click="toggleStatus(coupon)">

							<span class="block relative w-10 h-6 rounded-3xl transition-all bg-line peer-checked:bg-success after:h-5 after:w-5 after:top-0.5 after:absolute after:left-0 after:ms-0.5 after:transition-all after:rounded-full after:bg-white peer-checked:after:left-4"></span>

							<span class="text-content-dimmed peer-checked:hidden">
								{{ p__('status', 'Inactive') }}
							</span>

							<span class="hidden text-success peer-checked:inline">
								{{ p__('status', 'Active') }}
							</span>
						</label>
					</div>

					<div class="justify-self-end md:col-span-1">
						<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/coupons/${coupon.id}`" class="flex gap-2 items-center 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 gap-2 items-center px-4 py-2 w-full hover:no-underline hover:bg-intermediate" @click.prevent="currentResource = coupon; 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 coupons?'),
    title: 'currentResource.title'
  } %}
{% endblock %}
