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

@{ 
	string deviceType = !string.IsNullOrEmpty(GetViewParameterString("DeviceType")) ? GetViewParameterString("DeviceType") : "desktop";

	ProductListViewModel productList = new ProductListViewModel();
	if (Dynamicweb.Context.Current.Items.Contains("ProductList"))
	{
		productList = (ProductListViewModel)Dynamicweb.Context.Current.Items["ProductList"];
	}

	string settingsSortby = "";
	if (!string.IsNullOrEmpty(productList.SortBy))
	{
		settingsSortby = productList.SortOrder.Equals("desc", StringComparison.OrdinalIgnoreCase) ? $"-{productList.SortBy.ToLower()}" : $"{productList.SortBy.ToLower()}";
	}

	string sortBySelection = settingsSortby;
	sortBySelection = Dynamicweb.Context.Current.Request?.Form["SortBy"] ?? settingsSortby;
	sortBySelection = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("SortBy")) ? Dynamicweb.Context.Current.Request.QueryString.Get("SortBy") : sortBySelection;

	string sortNameSelectedRelevance = sortBySelection.ToLower() == string.Empty || sortBySelection.ToLower() == "relevance" ? "checked" : string.Empty;
	string sortNameSelectedAZ = sortBySelection.ToLower() == "nameforsort" ? "checked" : string.Empty;
	string sortNameSelectedZA = sortBySelection.ToLower() == "-nameforsort" ? "checked" : string.Empty;
	string sortPriceLowSelected = sortBySelection.ToLower() == "price" ? "checked" : string.Empty;
	string sortPriceHighSelected = sortBySelection.ToLower() == "-price" ? "checked" : string.Empty;
	string sortNewSelected = sortBySelection.ToLower() == "-created" ? "checked" : string.Empty;
	string sortMostSoldSelected = sortBySelection.ToLower() == "-ordercount" ? "checked" : string.Empty;

	string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", string.Empty);
	bool anonymousUser = Pageview.User == null;
	bool hidePrice = anonymousUsersLimitations.Contains("price") && anonymousUser;
}

@if (Model.Item.GetBoolean("SortByRelevance"))
{
	<div class="form-check">
		<input class="form-check-input" onchange="swift.ProductList.Update(event)" onkeydown="if(event.key==='Enter'){this.checked=!this.checked;swift.ProductList.Update(event);}" type="radio" name="SortBy" value="" id="SortByRelevance_@(deviceType)_@Model.ID" @sortNameSelectedRelevance>
		<label class="form-check-label" for="SortByRelevance_@(deviceType)_@Model.ID">
			@Translate("Relevance")
		</label>
	</div>
}

@if (Model.Item.GetBoolean("SortByNameAZ"))
{
	<div class="form-check">
		<input class="form-check-input" onchange="swift.ProductList.Update(event)" onkeydown="if(event.key==='Enter'){this.checked=!this.checked;swift.ProductList.Update(event);}" type="radio" name="SortBy" value="NameForSort" id="SortByNameAZ_@(deviceType)_@Model.ID" @sortNameSelectedAZ>
		<label class="form-check-label" for="SortByNameAZ_@(deviceType)_@Model.ID">
			@Translate("Name (A-Z)")
		</label>
	</div>
}

@if (Model.Item.GetBoolean("SortByNameZA"))
{
	<div class="form-check">
		<input class="form-check-input" onchange="swift.ProductList.Update(event)" onkeydown="if(event.key==='Enter'){this.checked=!this.checked;swift.ProductList.Update(event);}" type="radio" name="SortBy" value="-NameForSort" id="SortByNameZA_@(deviceType)_@Model.ID" @sortNameSelectedZA>
		<label class="form-check-label" for="SortByNameZA_@(deviceType)_@Model.ID">
			@Translate("Name (Z-A)")
		</label>
	</div>
}

@if (Model.Item.GetBoolean("SortByNewest"))
{
	<div class="form-check">
		<input class="form-check-input" onchange="swift.ProductList.Update(event)" onkeydown="if(event.key==='Enter'){this.checked=!this.checked;swift.ProductList.Update(event);}" type="radio" name="SortBy" value="-created" id="SortByNew_@(deviceType)_@Model.ID" @sortNewSelected>
		<label class="form-check-label" for="SortByNew_@(deviceType)_@Model.ID">
			@Translate("Newest")
		</label>
	</div>
}

@if (!hidePrice)
{
	if (Model.Item.GetBoolean("SortByLowestPrice"))
	{
		<div class="form-check">
			<input class="form-check-input" onchange="swift.ProductList.Update(event)" onkeydown="if(event.key==='Enter'){this.checked=!this.checked;swift.ProductList.Update(event);}" type="radio" name="SortBy" value="Price" id="SortByPriceLow_@(deviceType)_@Model.ID" @sortPriceLowSelected>
			<label class="form-check-label" for="SortByPriceLow_@(deviceType)_@Model.ID">
				@Translate("Lowest price")
			</label>
		</div>
	}
	if (Model.Item.GetBoolean("SortByHighestPrice"))
	{
		<div class="form-check">
			<input class="form-check-input" onchange="swift.ProductList.Update(event)" onkeydown="if(event.key==='Enter'){this.checked=!this.checked;swift.ProductList.Update(event);}" type="radio" name="SortBy" value="-Price" id="SortByPriceHigh_@(deviceType)_@Model.ID" @sortPriceHighSelected>
			<label class="form-check-label" for="SortByPriceHigh_@(deviceType)_@Model.ID">
				@Translate("Highest price")
			</label>
		</div>
	}
}

@if (Model.Item.GetBoolean("SortByMostSold"))
{
	<div class="form-check">
		<input class="form-check-input" onchange="swift.ProductList.Update(event)" onkeydown="if(event.key==='Enter'){this.checked=!this.checked;swift.ProductList.Update(event);}" type="radio" name="SortBy" value="-OrderCount" id="SortByMostSold_@(deviceType)_@Model.ID" @sortMostSoldSelected>
		<label class="form-check-label" for="SortByMostSold_@(deviceType)_@Model.ID">
			@Translate("Most sold")
		</label>
	</div>
}
