@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.QueryPublisher.QueryResultViewModel>
@using Dynamicweb.Core.Encoders
@using Dynamicweb
@using Dynamicweb.Frontend

@{
	string? query = Dynamicweb.Context.Current?.Request.QueryString.Get("QueryName") ?? null;
	string? repository = Dynamicweb.Context.Current?.Request.QueryString.Get("RepositoryName") ?? null;
	string? areaid = Dynamicweb.Context.Current?.Request.QueryString.Get("AreaId") ?? null;
}

<form method="get" class="d-flex gap-3 mb-4 flex-wrap"
	hx-get="@Dynamicweb.Context.Current?.Request.RawUrl"
	hx-trigger="keyup delay:1s, change"
	hx-swap="outerHTML"
	hx-replace-url="false"
	hx-select="[data-swift-posts-list]"
	hx-target="[data-swift-posts-list]">

	@if (!string.IsNullOrEmpty(query)) {
		<input type="hidden" name="QueryName" value="@query">
		<input type="hidden" name="RepositoryName" value="@repository">
		<input type="hidden" name="AreaId" value="@areaid">
	}

	<div class="flex-fill" style="max-width: 26rem">
		@RenderPartial("Designs/Swift-v2/QueryPublisher/Post/PostSearch.cshtml")
	</div>
	<div class="overflow-y-auto">
		@RenderPartial("Designs/Swift-v2/QueryPublisher/Post/PostHorizontalFacets.cshtml")
	</div>
</form>

<div data-swift-posts-list>
	@if (Model.Results.Count() > 0)
	{
		<div class="grid gap-4">
			@foreach (var post in Model.Results)
			{
				if (post.TryGetValue("PageId", out object? pageId) && pageId is int)
				{
					var pageObject = Dynamicweb.Content.Services.Pages?.GetPage((int)pageId);
					var pageInfo = pageObject != null ? Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(pageObject) : null;
					string? parentCategory = pageObject?.Parent != null && pageObject.Parent.Level != 1 ? pageObject.Parent.GetDisplayName() : null;

					if (pageInfo != null)
					{
						<a href="/Default.aspx?ID=@pageInfo.ID" class="g-col-12 card text-decoration-none">
							<div class="grid gap-0">
								@if (pageInfo.TryGetImageFile(out ImageFileViewModel imageFile))
								{
									<div class="g-col-12 g-col-md-4 position-relative">
										<img src="@imageFile.ToGetImage(new GetImageSettings { Ratio = "4/3" })" class="img-fluid rounded-start h-100 object-fit-cover" alt="@pageInfo.Name">
										@if (parentCategory != null) {
											<div class="badge badge-neutral-light position-absolute top-0 end-0 m-2">@parentCategory</div>
										}
									</div>
								}
								<div class="g-col-12 g-col-md-8">
									<div class="card-body d-flex flex-column h-100">
										<div class="flex-fill pb-3 mb-3 border-bottom">
											<h4 class="card-title">
												@pageInfo.Name
											</h4>
											<p class="card-text">
												@pageInfo.Description
											</p>
										</div>
										<div>
											<div class="opacity-75 fs-7">@pageInfo.CreatedDate.ToShortDateString()</div>
										</div>
									</div>
								</div>
							</div>
						</a>
					}
				}
			}
		</div>

		@RenderPartial("Designs/Swift-v2/QueryPublisher/Post/PostPagination.cshtml")
	}
	else
	{
		<div class="alert alert-dark w-100 text-center" role="alert">
			@Translate("No results found")
		</div>
	}
</div>