@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
@using Dynamicweb.Ecommerce.Orders

@{ 
	string voucherCodeError = GetString("Ecom:Cart.ValidationError.EcomOrderVoucherCode.ErrorMessage");
	string renderedVoucherCode = GetString("Ecom:Order.Customer.VoucherCode");
	string appliedVoucherCode = string.Empty;
	Order? order = !string.IsNullOrWhiteSpace(GetString("Ecom:Order.ID")) ? new OrderService().GetById(GetString("Ecom:Order.ID")) : Dynamicweb.Ecommerce.Common.Context.Cart;

	if (order is object && order.VoucherUseType != VoucherUseCategoryType.None && !string.IsNullOrWhiteSpace(order.VoucherCode))
	{
		appliedVoucherCode = order.VoucherCode;
	}

	List<string> voucherCodes = new List<string>();
	if (!string.IsNullOrWhiteSpace(appliedVoucherCode))
	{
		voucherCodes.AddRange(Dynamicweb.Ecommerce.Services.Vouchers.GetVoucherCodesFromString(appliedVoucherCode));
	}

	bool hasAppliedVouchers = voucherCodes.Count > 0;
	bool voucherWasPostedButNotApplied = !string.IsNullOrWhiteSpace(renderedVoucherCode) && string.IsNullOrWhiteSpace(appliedVoucherCode);
	string voucherErrorMessage = hasAppliedVouchers ? string.Empty : voucherCodeError;
	if (string.IsNullOrWhiteSpace(voucherErrorMessage) && voucherWasPostedButNotApplied)
	{
		voucherErrorMessage = Translate("Voucher could not be applied");
	}
	string validState = !string.IsNullOrWhiteSpace(voucherErrorMessage) ? "is-invalid" : string.Empty;

	<div class="py-3 border-top" id="orderVoucherForm">
		<form method="post" id="VoucherForm">
			<div class="input-group input-primary-button-group has-validation">
				<input type="text" class="form-control @(validState)" name="EcomOrderVoucherCode" placeholder="@Translate("Add voucher here")" id="EcomOrderVoucherCodeField" value="" aria-label="@Translate("Voucher input")">
				<button class="btn btn-sm btn-primary d-inline-flex align-items-center justify-content-center" data-dw-button="primary" type="submit" id="EcomOrderVoucherApply" style="min-width: 100px;">
					<span>@Translate("Apply")</span>
				</button>

				<div class="invalid-feedback">
					@voucherErrorMessage
				</div>
			</div>
			<label for="EcomOrderVoucherCodeField" class="visually-hidden">@Translate("Add voucher here")</label>
		</form>

		@if (hasAppliedVouchers)
		{
			<div class="d-flex flex-wrap gap-2 pt-3">
				@for (int i = 0; i < voucherCodes.Count; i++)
				{
					string code = voucherCodes[i];
					List<string> remainingVoucherCodes = new List<string>(voucherCodes);
					remainingVoucherCodes.RemoveAt(i);
					string remainingVoucherCode = string.Join(",", remainingVoucherCodes);

					<form method="post">
						<input type="hidden" name="EcomOrderVoucherCode" value="@remainingVoucherCode">
						<button type="submit" class="badge fw-normal d-inline-flex align-items-center justify-content-center opacity-50 opacity-100-hover border-0" style="background-color:var(--dw-color-foreground);color:var(--dw-color-background);" aria-label="@Translate("Remove voucher")" id="RemoveVoucherButton_@i">
							<span class="icon-2">
								@ReadFile("/Files/Images/Icons/xmark.svg")
							</span>
							<span class="mx-2">@code</span>
						</button>
					</form>
				}
			</div>
		}
	</div>
}
