@inherits ViewModelTemplate<ProductViewModel>
@using Dynamicweb.Frontend
@using Dynamicweb.Rendering
@using Dynamicweb.Ecommerce.ProductCatalog
@using System.Globalization

@functions{
	string DoubleToString(double? value)
	{
		if (value.HasValue)
		{
			return value.Value.ToString(CultureInfo.InvariantCulture);
		}
		return null;
	}
}

@{
	if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
	{
		Dynamicweb.Context.Current.Items["ProductDetails"] = Model;
	}
	else
	{
		Dynamicweb.Context.Current.Items.Add("ProductDetails", Model);
	}

	string viewType = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("ViewType")) ? Dynamicweb.Context.Current.Request.Form.Get("ViewType") : string.Empty;
	bool quantitySelector = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("QuantitySelector")) ? Convert.ToBoolean(Dynamicweb.Context.Current.Request.Form.Get("QuantitySelector")) : false;

	string url = "/Default.aspx?ID=" + (GetPageIdByNavigationTag("CartService"));
	if (!url.Contains("LayoutTemplate"))
	{
		url += url.Contains("?") ? "&LayoutTemplate=Swift-v2_MiniCart.cshtml" : "?LayoutTemplate=Swift-v2_MiniCart.cshtml";
	}

	//Basic ParagraphViewModel setting
	var paragraphViewModel = new ParagraphViewModel()
	{
		ID = 123456789
	};

	string buttonLayout = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("ButtonLayout")) ? Dynamicweb.Context.Current.Request.Form.Get("ButtonLayout") : string.Empty;
	string buttonAspectRatio = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("ButtonAspectRatio")) ? Dynamicweb.Context.Current.Request.Form.Get("ButtonAspectRatio") : string.Empty;

	var parms = new Dictionary<string, object>();
	parms.Add("ButtonLayout", buttonLayout);
	parms.Add("ButtonAspectRatio", buttonAspectRatio);
}

@if (Model.VariantInfo?.VariantInfo != null)
{
	if (viewType == "ModalContent")
	{		
		string disableAddToCart = null;
		double? stepQty = Model.PurchaseQuantityStep > 0 ? Model.PurchaseQuantityStep : 1;
		double? minQty = Model.PurchaseMinimumQuantity > 0 ? Model.PurchaseMinimumQuantity : 1;
		double? maxQty = null;
		double? valueQty = minQty > stepQty ? minQty : stepQty;
		bool isStockType = Model.ProductType == Dynamicweb.Ecommerce.Products.ProductType.Stock;
		if (isStockType)
		{
			if (Model.NeverOutOfstock)
			{
				disableAddToCart = "disabled";
			}
			else
			{
				disableAddToCart = (Model.StockLevel <= 0) || Model.Discontinued ? "disabled" : disableAddToCart;
				maxQty = Model.StockLevel;
			}
		}

		disableAddToCart = string.IsNullOrEmpty(Model.VariantId) ? "disabled" : disableAddToCart;

		<div class="modal-header">
			<h5 class="modal-title">@Model.Name</h5>
			<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
		</div>
		<div class="modal-body">
			@RenderPartial("Components/VariantSelector.cshtml", paragraphViewModel, parms)
		</div>
		<div class="modal-footer">
			<form method="post" action="@url">
				<input type="hidden" name="redirect" value="false">
				<input type="hidden" name="ProductName" value="@Model.Name">
				<input type="hidden" name="ProductVariantName" value="@Model.VariantName">
				<input type="hidden" name="ProductId" value="@Model.Id">
				<input type="hidden" name="VariantId" value="@Model.VariantId" id="VariantIdField_@Model.Id">
				<input type="hidden" name="ProductReferer" value="variant_selector">
				<input type="hidden" name="cartcmd" value="add">

				<input type="hidden" name="ProductCurrency" value="@Dynamicweb.Ecommerce.Common.Context.Currency.Code">
				<input type="hidden" name="ProductReferer" value="variant_selector_modal">
				<input type="hidden" name="ProductPrice" value="@Model.Price.ToStringInvariant()">

				<template class="js-step-quantity-warning">
					<div class="modal-header">
						<h5 class="modal-title">@Translate("The quantity is not valid")</h5>
					</div>
					<div class="modal-body">
						<p class="text-muted">@Translate("Please select a quantity that is dividable by") @stepQty</p>
					</div>
				</template>

				<template class="js-min-quantity-warning">
					<div class="modal-header">
						<h5 class="modal-title">@Translate("The product could not be added to the cart")</h5>
					</div>
					<div class="modal-body">
						<p class="text-muted">@Translate("The quantity is not valid. You must buy at least") @Model.PurchaseMinimumQuantity</p>
					</div>
				</template>

				<template class="js-value-missing-warning">
					<div class="modal-header">
						<h5 class="modal-title">@Translate("No amount specified")</h5>
						<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
					</div>
					<div class="modal-body">
						<p class="text-muted">@Translate("Specify an amount to add to the cart")</p>
					</div>
				</template>

				@if (quantitySelector)
				{
					<div class="d-flex flex-fill flex-column flex-sm-row gap-2">
						@RenderPartial("Components/QuantitySelector.cshtml", Model)
						<button type="button" onclick="swift.Cart.Update(event)" class="btn btn-primary flex-fill js-add-to-cart-button" data-dw-button="primary" title="@Translate("Add to cart")" id="AddToCartButton@(Model.Id)_@Pageview.CurrentParagraph.ID" @disableAddToCart>@Translate("Add to cart")</button>

						<div class="invalid-feedback d-none">
							@Translate("Please select a quantity that is dividable by") @stepQty
						</div>

					</div>
					<label for="Quantity_@(Model.Id)_@Model.VariantId" class="visually-hidden">@Translate("Quantity")</label>
				}
				else
				{
					<input id="Quantity_@(Model.Id)_@Model.VariantId" name="Quantity" value="@DoubleToString(valueQty)" type="hidden" @disableAddToCart>
					<button type="button" onclick="swift.Cart.Update(event)" class="btn btn-primary flex-fill js-add-to-cart-button" data-dw-button="primary" title="@Translate("Add to cart")" id="AddToCartButton@(Model.Id)_@Pageview.CurrentParagraph.ID" @disableAddToCart>@Translate("Add to cart")</button>
				}
			</form>
		</div>
	}
	else
	{
		@RenderPartial("Components/VariantSelector.cshtml", paragraphViewModel)
	}

	<script type="module">
		swift.VariantSelector.init();
	</script>
}
