@inherits ViewModelTemplate<FavoriteListListViewModel>
@using Dynamicweb.Frontend
@using Dynamicweb.Rendering
@using Dynamicweb.Ecommerce.Frontend
@using Dynamicweb.Ecommerce.ProductCatalog

@{
    var baseUrl = SearchEngineFriendlyURLs.GetFriendlyUrl($"default.aspx?ID={Pageview?.Page?.ID}");
    string currentSortBy = Dynamicweb.Context.Current?.Request.QueryString.Get("SortBy") ?? string.Empty;
    string currentSortOrder = Dynamicweb.Context.Current?.Request.QueryString.Get("SortOrder") ?? string.Empty;
    string arrow = currentSortOrder == "asc" ? ReadFile("/Files/Images/Icons/arrow-up.svg") : ReadFile("/Files/Images/Icons/arrow-down.svg");
}

<div data-swift-favorites-list>
    <header class="hstack gap-3 mb-4 flex-wrap">
        <h1 class="h4 mb-0 flex-fill">@(Pageview?.Page?.GetDisplayName() ?? string.Empty)</h1>

        <button type="button" class="btn btn-sm btn-primary hstack gap-2 ms-auto py-2 px-3" data-dw-button
            hx-trigger="click"
            hx-get="@(baseUrl)"
            hx-vals='{"LayoutTemplate": "/Designs/Swift-v2/eCom/CustomerExperienceCenter/Favorites/Modals/CreateFavoriteListModal.cshtml"}'
            hx-target="main"
            hx-swap="beforeend">
            <span class="icon text-opacity-50">
                @ReadFile("/Files/Images/Icons/plus.svg")
            </span>
            @Translate("Add list")
        </button>

    </header>

    <form hx-get="@(baseUrl)"
        hx-trigger="submit, change from:input[name='SortBy']"
        hx-target="[data-swift-favorites-list]"
        hx-select="[data-swift-favorites-list]"
        hx-swap="outerHTML"
        hx-replace-url="true"
        hx-on:click="const sortInput = event.target.closest('label')?.querySelector('input[name=SortBy]');
                     if (!sortInput) return;
                     const sortOrderField = this.querySelector('[name=SortOrder]');
                     if (!sortOrderField) return;
                     if (sortInput.checked) {
                         sortOrderField.value = sortOrderField.value === 'asc' ? 'desc' : 'asc';
                         this.requestSubmit();
                     } else {
                         sortOrderField.value = 'asc';
                     }">
        <input type="hidden" name="SortOrder" value="@(currentSortOrder)">

    @if (Model.FavoriteLists is not null)
    {
        <div class="card overflow-hidden">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>
                                <label class="hstack gap-1" role="button">
                                    <input type="radio" name="SortBy" value="Name" class="visually-hidden" checked="@(currentSortBy=="Name")" />
                                    @Translate("Name")
                                    @if(currentSortBy=="Name") {
                                        <span class="icon-1">@arrow</span>
                                    }
                                </label>
                            </th>
                            <th>@Translate("Products")</th>
                            <th>
                                <label class="hstack gap-1" role="button">
                                    <input type="radio" name="SortBy" value="Created" class="visually-hidden" checked="@(currentSortBy=="Created")" />
                                    @Translate("Created")
                                    @if(currentSortBy=="Created") {
                                        <span class="icon-1">@arrow</span>
                                    }
                                </label>
                            </th>
                            <th>@Translate("Actions")</th>
                        </tr>
                    </thead>
                    <tbody>
                        @if (Model.FavoriteLists.Count > 0)
                        {
                            @foreach (var favoriteList in Model.FavoriteLists)
                            {
                                <tr class="position-relative">
                                    <td class="text-wrap" style="max-inline-size: 10rem;">
                                        <div class="hstack lh-1">
                                            <a href="@(baseUrl)?FavoriteListId=@(favoriteList.Id)" class="text-decoration-none text-decoration-underline-hover">
                                                <span class="lh-1">@(!string.IsNullOrEmpty(favoriteList.Name) ? favoriteList.Name : (Translate("Untitled") + " #" + favoriteList.Id))</span>
                                            </a>
                                            @if(favoriteList.IsDefault) { <span class="badge lh-sm badge-neutral-light ms-2">@Translate("Default")</span> }
                                        </div>
                                        <div class="fs-8 text-muted" style="max-inline-size: 15rem; text-wrap: nowrap; overflow: hidden; text-overflow: ellipsis;">@(!string.IsNullOrEmpty(favoriteList.Description) ? favoriteList.Description : "-")</div>
                                    </td>
                                    <td>
                                        <div class="d-grid gap-1" style="grid-template-columns: repeat(4, 2rem);">
                                            @if (favoriteList.ProductList.Products.Any())
                                            {
                                                foreach (var product in favoriteList.ProductList.Products.Take(3))
                                                {
                                                    @if(product?.DefaultImage?.TryGetImageFileViewModel(out var image) is true)
                                                    {
                                                        <div class="d-flex img-thumbnail" style="aspect-ratio: 1;">
                                                            <img class="img-fluid mh-100 m-auto" src="@(image?.ToGetImage(new(){Height = 22, Width = 22, Ratio = "1/1", Crop = Dynamicweb.Imaging.ImageHandling.CropMode.KeepAspectRatio }))" alt="@product?.Name" loading="lazy" decoding="async">
                                                        </div>
                                                    }
                                                }
                                                if(favoriteList.ProductList.Products.Count > 3)
                                                {
                                                    
                                                    <span class="icon text-muted m-auto d-flex align-items-center justify-content-center gap-0 fs-8 p-0 fw-semibold">
                                                        @ReadFile("/Files/Images/Icons/plus.svg")
                                                        @(favoriteList.ProductList.Products.Count - 3)
                                                    </span>
                                                    
                                                }
                                            }
                                        </div>
                                    </td>
                                    <td>
                                        <div class="vstack">
                                            @(favoriteList?.Created?.ToString(Pageview?.Area?.CultureInfo?.DateTimeFormat?.ShortDatePattern) ?? "-")
                                            <span class="text-muted fs-8">
                                                @(favoriteList?.Created?.ToString(Pageview?.Area?.CultureInfo?.DateTimeFormat?.ShortTimePattern) ?? "")
                                            </span>
                                        </div>
                                    </td>
                                    <td>
                                        <button type="button" class="btn btn-icon position-relative z-2" data-dw-button data-bs-toggle="dropdown" aria-expanded="false" data-bs-popper-config='{"strategy":"fixed", "placement":"bottom-end"}'>
                                            <span class="icon">
                                                @ReadFile("/Files/Images/Icons/ellipsis.svg")
                                            </span>
                                        </button>
                                        <ul class="dropdown-menu" hx-disinherit="hx-select hx-replace-url">
                                            <li>
                                                <button type="button" class="dropdown-item hstack gap-2"
                                                    hx-get="/dwapi/ecommerce/favorites/lists/@(favoriteList?.Id)"
                                                    hx-headers='{"x-dw-template": "/Designs/Swift-v2/eCom/CustomerExperienceCenter/Favorites/Modals/RenameFavoriteListModal.cshtml", "x-dw-culture": "@Pageview?.Area?.CultureInfo?.Name", "accept": "text/html"}'
                                                    hx-target="main"
                                                    hx-swap="beforeend"
                                                    hx-ext="swift-auth">
                                                    <span class="icon fs-6">
                                                        @ReadFile("/Files/Images/Icons/pen.svg")
                                                    </span>
                                                    @Translate("Edit")
                                                </button>
                                            </li>
                                            <li>
                                                <button type="button" class="dropdown-item hstack gap-2"
                                                    hx-get="?favoritecmd=favoritelistsetdefault&userid=@(Pageview?.User?.ID)&favoritelistid=@(favoriteList?.Id)"
                                                    hx-select="[data-swift-favorites-list]"
                                                    hx-target="[data-swift-favorites-list]"
                                                    hx-swap="outerHTML"
                                                    hx-replace-url="false"
                                                    disabled="@(favoriteList?.IsDefault ?? false)"
                                                    >
                                                    <span class="icon fs-6">
                                                        @ReadFile("/Files/Images/Icons/star.svg")
                                                    </span>
                                                    @Translate("Set as default")
                                                </button>
                                            </li>
                                            <li>
                                            <hr class="dropdown-divider my-1">
                                            </li>                   
                                            <li>
                                                <button type="button" class="dropdown-item hstack gap-2 text-danger"
                                                    hx-get="/dwapi/ecommerce/favorites/lists/@(favoriteList?.Id)"
                                                    hx-vals='{"userId": @(Pageview?.User?.ID)}'
                                                    hx-headers='{"x-dw-template": "/Designs/Swift-v2/eCom/CustomerExperienceCenter/Favorites/Modals/DeleteFavoriteListModal.cshtml", "x-dw-culture": "@Pageview?.Area?.CultureInfo?.Name", "accept": "text/html"}'
                                                    hx-target="main"
                                                    hx-swap="beforeend"
                                                    hx-ext="swift-auth"
                                                    >
                                                    <span class="icon fs-6">
                                                        @ReadFile("/Files/Images/Icons/trash-can.svg")
                                                    </span>
                                                    @Translate("Delete")
                                                </button>
                                            </li>                                
                                        </ul>
                                    </td>
                                </tr>
                            }
                        }
                        else
                        {
                            <tr>
                                <td class="text-center py-5" colspan="4">
                                    <div data-swift-empty-state>
                                        <div data-swift-empty-state-icon>
                                            <span class="icon">
                                                @ReadFile("/Files/Images/Icons/heart.svg")
                                            </span>
                                        </div>
                                        <div>@Translate("No favorites found")</div>
                                    </div>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    }

    @if (Model.PageCount > 1)
    {
        <div class="hstack justify-content-between mt-3">
            <div class="text-muted fs-7">
                @Translate("Showing page") @(Model.CurrentPage) @Translate("of") @(Model.PageCount)
            </div>

            <div class="hstack gap-2">
                <button type="submit" class="btn btn-outline-secondary text-body hstack p-2" name="PageNum" value="@(Model.CurrentPage - 1)" data-dw-button disabled="@(Model.CurrentPage == 1)">
                    <span class="icon">
                        @ReadFile("/Files/Images/Icons/chevron-left.svg")
                    </span>
                </button>
                <button type="submit" class="btn btn-outline-secondary text-body hstack p-2" name="PageNum" value="@(Model.CurrentPage + 1)" data-dw-button disabled="@(Model.CurrentPage == Model.PageCount)">
                    <span class="icon">
                        @ReadFile("/Files/Images/Icons/chevron-right.svg")
                    </span>
                </button>
            </div>
        </div>
    }
    </form>
</div>