@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.ParagraphViewModel>
@using System.Linq
@using Dynamicweb.Ecommerce
@using Dynamicweb.Ecommerce.ProductCatalog
@using Dynamicweb.Ecommerce.Products
@using Dynamicweb.Ecommerce.Stocks

@functions
{
	private static bool BackInStockRegisteredForUser(ProductViewModel product, string unitId = "", long stocklocationId = 0)
	{
		if (!Dynamicweb.Security.UserManagement.User.IsExtranetUserLoggedIn()) return false;

		Product productObject = Services.Products.GetProductById(product.Id, product.VariantId, product.LanguageId);
		StockLocation stockLocation = Services.StockService.GetStockLocation(stocklocationId);
		double unitStock = productObject.GetUnitStock(stockLocation, unitId);
		return unitStock <= 0d && ProductBackInStockNotification.BackInStockNotificationExists(productObject, unitId);
	}
}

@{
	ProductViewModel product = null;
	if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
	{
		product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
	}
}

@if ((product is object && product.ProductType == Dynamicweb.Ecommerce.Products.ProductType.Stock))
{
	bool productInStock = product.StockLevel > 0 || product.NeverOutOfstock;

	if (!productInStock)
	{
		string notifiedMessage = Model.Item.GetString("NotifiedMessage");
		string notifiedMessageId = $"NotifiedMessage_item_{Model.Item.SystemName.ToLower()}_{product.Id}_{product.VariantId.Replace(".", "_")}";

		string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", "");
		horizontalAlign = horizontalAlign == "center" ? "justify-content-center" : horizontalAlign;
		horizontalAlign = horizontalAlign == "end" ? "justify-content-end" : horizontalAlign;
		horizontalAlign = horizontalAlign == "full" ? "" : horizontalAlign;

		string flexFill = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "flex-fill" : "";
		string fullWidth = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "w-100" : "";

		bool notificationRegisteredForUser = BackInStockRegisteredForUser(product);

		<div class="d-flex flex-row w-100 @horizontalAlign @fullWidth item_@Model.Item.SystemName.ToLower()">
			<div class="@horizontalAlign @fullWidth @(notificationRegisteredForUser ? "" : "d-none")" id="@notifiedMessageId">
				@notifiedMessage
			</div>

			@if (!notificationRegisteredForUser)
			{

				string notifyIcon = Model.Item.GetString("Icon");
				string notifyLabel = !notifyIcon.Contains("_none") ? "<span class=\"icon-2\">" + ReadFile(notifyIcon) + "</span>" : "";
				notifyLabel += " " + Translate("Notify me when available");

				string modalId = $"modal_item_{Model.Item.SystemName.ToLower()}_{product.Id}_{product.VariantId.Replace(".", "_")}";
				string formId = $"NotificationForm_item_{Model.Item.SystemName.ToLower()}_{product.Id}_{product.VariantId.Replace(".", "_")}";
				string notificationButtonId = $"NotificationButton_item_{Model.Item.SystemName.ToLower()}_{product.Id}_{product.VariantId.Replace(".", "_")}";
				string notificationOnClick = !Pageview.IsVisualEditorMode ? $"swift.BackInStockNotification.SubmitNotification('{formId}', '{modalId}', '{notifiedMessageId}', '{notificationButtonId}')" : "";

				string productPage = $"/Default.aspx?ID={GetPageIdByNavigationTag("ProductDetailPage")}";

				<div class="d-flex @horizontalAlign @fullWidth js-input-group">
					<div class="input-group input-primary-button-group flex-nowrap">
						<button type="button" class="btn btn-primary @flexFill" data-dw-button="primary" id="@notificationButtonId" data-bs-toggle="modal" data-bs-target="#@modalId">
							<span class="text-nowrap d-flex align-items-center justify-content-center gap-2">
								@notifyLabel
							</span>
						</button>
					</div>
				</div>

				<div class="modal fade" id="@modalId" tabindex="-1" aria-labelledby="label_@modalId" aria-hidden="true">
					<div class="modal-dialog pt-8">
						<div class="modal-content">
							<div class="modal-header">
								<h5 class="modal-title" id="label_@modalId">@Translate("Back in stock")</h5>
								<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="@Translate("Close")"></button>
							</div>
							<div class="modal-body">
								<form name="@product.Id" id="@formId" method="post" action="@productPage">
									<input type="hidden" name="ProductID" value="@product.Id" />
									<input type="hidden" name="VariantID" value="@product.VariantId" />
									<input type="hidden" name="LanguageID" value="@product.LanguageId" />
									<input type="hidden" name="CartCmd" value="createnotificationforthisproduct" />
									<div class="m-2">
										@Translate("Send me an email when the product is in stock.")
									</div>
									<div class="form-floating m-2">
										<input class="form-control" type="text" id="NotificationEmail" value="@(Pageview.User != null ? Pageview.User.Email : "")" name="NotificationEmail" placeholder="@Translate("Email")" required="required" aria-required="aria-required" />
										<label for="NotificationEmail" class="form-label">@Translate("Email")</label>
									</div>
								</form>
							</div>
							<div class="modal-footer">
								<button type="button" class="btn btn-primary" data-dw-button="primary" onclick="@notificationOnClick">@Translate("Submit")</button>
							</div>
						</div>
					</div>
				</div>
			}
		</div>
	}
}
else if (Pageview.IsVisualEditorMode)
{
	<div class="alert alert-dark m-0">@Translate("Back in stock notification will be shown here")</div>
}
