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

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

@if (product is object) {
	string horizontalAlign = "end";
	horizontalAlign = horizontalAlign == "center" ? "justify-content-center" : horizontalAlign;
	horizontalAlign = horizontalAlign == "end" ? "justify-content-end" : horizontalAlign;
	horizontalAlign = horizontalAlign == "full" ? "" : horizontalAlign;

	string buttonSize = Model.Item.GetRawValueString("ButtonSize", "regular");
	buttonSize = buttonSize == "small" ? " btn-sm" : buttonSize;
	buttonSize = buttonSize == "regular" ? string.Empty : buttonSize;
	buttonSize = buttonSize == "large" ? " btn-lg" : buttonSize;

	string flexFill = "flex-fill";
	string fullWidth = "w-100";

	string shopPageId = Pageview.CurrentParagraph.Item["ShopPageLink"] != null ? Pageview.CurrentParagraph.Item["ShopPageLink"].ToString() : "";

	var ecomLanguages = Dynamicweb.Ecommerce.Services.Languages.GetLanguages();
	var assetGroups = Dynamicweb.Ecommerce.Services.DetailsGroups.GetGroups();

	bool hideTitle = Model.Item.GetBoolean("HideTitle");
	string titleFontSize = Model.Item.GetRawValueString("TitleSize", "fs-6");

	string confirmationPage = Pageview.CurrentParagraph.Item["ConfirmationPageLink"] != null ? Pageview.CurrentParagraph.Item["ConfirmationPageLink"].ToString() : "";

	int languageCount = 0;

	string productKey = product.Id + product.VariantId;

	<div class="item_@Model.Item.SystemName.ToLower() w-100">
		@if (!hideTitle)
		{
			<h2 class="@titleFontSize">@Model.Item.GetString("Title")</h2>
		}

		<form id="ProductExportForm" action="/dwapi/ecommerce/products/export" class="js-product-export-form">
			<input type="hidden" name="ShopId" value="@Pageview.Area.EcomShopId">
			<input type="hidden" name="MainProductID" value="@productKey">
			<input type="hidden" name="TemplatePath" value="/Designs/Swift-v2/eCom/ProductCatalog/ExportProductsMail.cshtml">
			<input type="hidden" name="RepositoryName" value="ProductsFrontend">
			<input type="hidden" name="QueryName" value="products">

			<div class="mb-2">
				<h3 class="h5">@Translate("Product information")</h3>
				<div>@Translate("Select a language and format for your product file")</div>
			</div>

			<div class="mb-3">
				<div class="mb-1">@Translate("Language")</div>
				<select name="LanguageId" id="LanguageId" class="form-select" aria-label="@Translate("Language")">
					@foreach (var language in ecomLanguages)
					{
						string selected = languageCount == 0 ? "selected" : "";

						<option @selected>
							@language.GetDisplayName() (@language.CountryCode)
						</option>

						languageCount++;
					}
				</select>
			</div>

			<div class="form-floating mb-4">
				<select class="form-select" id="DownloadCartFormatSelector" name="ExportFormat" aria-label="@Translate("Export format")">
					<option value="" selected>@Translate("None")</option>
					<option value="json">JSON</option>
					<option value="csv">CSV</option>
					<option value="xml">XML</option>
				</select>
				<label for="DownloadCartFormatSelector">@Translate("Export format")</label>
			</div>

			<div class="mb-2">
				<h3 class="h5">@Translate("Image settings")</h3>
				<div>@Translate("Select a resolution for your images")</div>
			</div>

			<div class="form-floating mb-4">
				<input type="hidden" name="Dpi" id="DownloadCartDpi" value="72">
				<input type="hidden" name="ImageFormat" id="DownloadCartImageFormat" value="webp">

				<select class="form-select" id="DownloadCartImageImageSettingsSelector" aria-label="@Translate("Resolution")"
						onchange="document.querySelector('#DownloadCartDpi').value = this.options[this.selectedIndex].getAttribute('data-dpi'); document.querySelector('#DownloadCartImageFormat').value = this.options[this.selectedIndex].getAttribute('data-image-format');">
					<option data-dpi="72" data-image-format="webp" selected>@Translate("Web images") (webp, dpi: 72)</option>
					<option data-dpi="144" data-image-format="jpg">@Translate("Print images") (jpg, dpi: 144)</option>
					<option data-dpi="300" data-image-format="jpg">@Translate("Press images") (jpg, dpi: 300)</option>
					<option data-dpi="600">@Translate("Origin images")</option>
				</select>
				<label for="DownloadCartImageFormatSelector">@Translate("Resolution")</label>
			</div>

			<div class="mb-2">
				<h3 class="h5">@Translate("Product assets")</h3>
				<div>@Translate("Optionally select additional product assets to download")</div>
			</div>

			<div class="mb-3">
				<div class="mb-1">@Translate("Image assets")</div>
				<div class="dropdown js-dropdown">
					<button class="form-select text-start w-100 js-dropdown-btn" type="button" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
						@Translate("None")
					</button>
					<div class="dropdown-menu w-100 p-3">
						<div class="form-check" onclick="swift.ProductExport.SelectAllOptions(this);">
							<input class="form-check-input" type="checkbox" name="AssetCategories" data-short-name="@Translate("All")" value="" id="AssetsCheckAll">
							<label class="form-check-label" for="AssetsCheckAll">
								@Translate("All")
							</label>
						</div>

						@foreach (var group in assetGroups)
						{
							<div class="form-check" onclick="swift.ProductExport.UpdateSelector(this)">
								<input class="form-check-input" type="checkbox" name="AssetCategories" data-short-name="@group.GetName(Dynamicweb.Ecommerce.Common.Context.LanguageID)" value="@group.SystemName" id="Check_@group.SystemName">
								<label class="form-check-label" for="Check_@group.SystemName">
									@group.GetName(Dynamicweb.Ecommerce.Common.Context.LanguageID)
								</label>
							</div>
						}
					</div>
				</div>
			</div>

			<div class="js-recipient-tokens"></div>

			<div class="js-recipients">
				<div class="mb-3 w-100 js-recipient-field">
					<label for="DownloadCartRecipientEmail">@Translate("Send download link to")</label>
					<div class="input-group input-secondary-button-group">
						<input type="email" class="form-control js-recipient-email" id="DownloadCartRecipientEmail" placeholder="@Translate("Send download link to")" required>
						<button type="button" class="btn btn-secondary js-recipient-add-btn" data-dw-button="secondary" onclick="swift.ProductExport.AddRecipent(this)">@Translate("Add")</button>
					</div>
					<div class="invalid-feedback">
						@Translate("Please enter an email address")
					</div>
				</div>
			</div>

			<div class="alert alert-warning mb-2 d-none js-download-images-warning">@Translate("The solution does not have license to download")</div>

			<div class="d-flex @horizontalAlign @fullWidth">
				<button type="button" onclick="swift.ProductExport.SetEmailAddresses(this)" class="btn btn-primary @(buttonSize) @flexFill" data-dw-button="primary" title="@Translate("Send download link")">@Translate("Send download link")</button>
			</div>

			<div class="alert alert-success mt-3 d-none js-download-success">@Translate("A download link is sent to the recipients")</div>
		</form>
	</div>
}
else if (Pageview.IsVisualEditorMode)
{
	<div class="alert alert-dark m-0">@Translate("The Download data feature, will be shown here")</div>
}
