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

@{
	string apiKey = Pageview.AreaSettings.GetString("Google_APIKey", string.Empty);

	string addressdeliveryAddressString = GetString("Ecom:Order.Delivery.Company") + GetString("Ecom:Order.Delivery.Address") + GetString("Ecom:Order.Delivery.Address2") + GetString("Ecom:Order.Delivery.Zip") + GetString("Ecom:Order.Delivery.City") + GetString("Ecom:Order.Delivery.Phone");
	string addressBillingAddressString = GetString("Ecom:Order.Customer.Company") + GetString("Ecom:Order.Customer.Address") + GetString("Ecom:Order.Customer.Address2") + GetString("Ecom:Order.Customer.Zip") + GetString("Ecom:Order.Customer.City") + GetString("Ecom:Order.Customer.Phone");
	string disabled = (addressdeliveryAddressString != addressBillingAddressString) && addressBillingAddressString != "" || Pageview.CurrentParagraph.Item.SystemName == "Swift-v2_QuoteCheckoutApp" ? string.Empty : "disabled";
	bool enableAdditionalAddress = Pageview.CurrentParagraph.Item["EnableAdditionalAddress"] != null ? (Boolean)Pageview.CurrentParagraph.Item["EnableAdditionalAddress"] : false;
	bool enableFindLocation = Pageview.CurrentParagraph.Item["EnableFindLocation"] != null ? (Boolean)Pageview.CurrentParagraph.Item["EnableFindLocation"] : false;

	string currentCountry = Pageview.Area?.EcomCountryCode != null ? Pageview.Area.EcomCountryCode : "dk";
	foreach (LoopItem country in GetLoop("Countries"))
	{
		if (country.GetString("Ecom:Country.IsDeliveryCountryOrDefault") == "true")
		{
			currentCountry = country.GetString("Ecom:Country.Code2");
		}
	}

	string countrySelectorColumnSize = "g-col-6";
	string customerRegionLabel = "State";
}

<div class="mt-0 checkout_swift_billing-anonymous">

	<div class="grid">
		<div class="g-col-12">
			<div class="form-floating">
				<input @disabled type="text" class="form-control" name="EcomOrderCustomerCompany" id="EcomOrderCustomerCompany" value="@GetString("Ecom:Order.Customer.Company")" placeholder="@Translate("Company")">
				<label for="EcomOrderCustomerCompany">@Translate("Company (Optional)")</label>
			</div>
		</div>

		<div class="g-col-12">
			<div class="form-floating">
				<input @disabled type="text" class="form-control" name="EcomOrderCustomerName" id="EcomOrderCustomerName" value="@GetString("Ecom:Order.Customer.Name")" placeholder="@Translate("Name")">
				<label for="EcomOrderCustomerName">@Translate("Name")</label>
			</div>
		</div>

		<div class="g-col-12">
			<div class="form-floating">
				<input @disabled type="text" class="form-control" name="EcomOrderCustomerAddress" id="EcomOrderCustomerAddress" value="@GetString("Ecom:Order.Customer.Address")" placeholder="@Translate("Street & Number")">
				<label for="EcomOrderCustomerAddress">@Translate("Street & Number")</label>
			</div>
		</div>
		
		@if (enableAdditionalAddress)
		{
			<div class="g-col-12">
				<div class="form-floating">
					<input @disabled type="text" class="form-control" name="EcomOrderCustomerAddress2" id="EcomOrderCustomerAddress2" value="@GetString("Ecom:Order.Customer.Address2")" placeholder="@Translate("Additional address")">
					<label for="EcomOrderCustomerAddress2">@Translate("Additional address")</label>
				</div>
			</div>
		}
		
		<div class="g-col-12">

			<div class="grid">

				<div class="g-col-3">
					<div class="form-floating">
						<input @disabled type="text" class="form-control" name="EcomOrderCustomerZip" id="EcomOrderCustomerZip" value="@GetString("Ecom:Order.Customer.Zip")" placeholder="@Translate("Zip code")">
						<label for="EcomOrderCustomerZip">@Translate("Zip code")</label>
					</div>
				</div>

				@if (currentCountry == "US" || currentCountry == "CA" || currentCountry == "GB")
				{
					if (GetLoop("CustomerRegions").Count > 0)
					{
						countrySelectorColumnSize = "g-col-3";
					}
				}

				<div class="@countrySelectorColumnSize">
					<div class="form-floating">
						<input @disabled type="text" class="form-control" name="EcomOrderCustomerCity" id="EcomOrderCustomerCity" value="@GetString("Ecom:Order.Customer.City")" placeholder="@Translate("Town / City")">
						<label for="EcomOrderCustomerCity">@Translate("Town / City")</label>
					</div>
				</div>

				@if (currentCountry == "US" || currentCountry == "CA" || currentCountry == "GB")
				{
					if (GetLoop("CustomerRegions").Count > 0)
					{
						if (currentCountry == "CA")
						{
							customerRegionLabel = Translate("Province");
						}
						else if (currentCountry == "GB")
						{
							customerRegionLabel = Translate("Region");
						}
						else if (currentCountry == "US")
						{
							customerRegionLabel = Translate("State");
						}

						<div class="g-col-3">
							<div class="form-floating">
								<select @disabled class="form-select" name="EcomOrderCustomerRegion" id="EcomOrderCustomerRegion" onchange="submitForm()">
									@foreach (LoopItem region in GetLoop("CustomerRegions"))
									{
										if (region.GetString("Ecom:CustomerRegion.IsCustomerRegion") == "true")
										{
											<option value="@region.GetString("Ecom:CustomerRegion.RegionCode")" selected>@region.GetString("Ecom:CustomerRegion.Name")</option>
										}
										else
										{
											<option value="@region.GetString("Ecom:CustomerRegion.RegionCode")">@region.GetString("Ecom:CustomerRegion.Name")</option>
										}
									}
								</select>
								<label for="EcomOrderCustomerRegion">@customerRegionLabel</label>
							</div>
						</div>
					}
				}

				<div class="g-col-12 g-col-md-3">
					<div class="form-floating">
						<select @disabled class="form-control" name="EcomOrderCustomerCountry" id="EcomOrderCustomerCountry" onchange="submitForm()">
							@foreach (LoopItem country in GetLoop("Countries"))
							{
								if (country.GetString("Ecom:Country.IsCustomerCountryOrDefault") == "true")
								{
									<option value="@country.GetString("Ecom:Country.Code2")" selected>@country.GetString("Ecom:Country.Name")</option>
								}
								else
								{
									<option value="@country.GetString("Ecom:Country.Code2")">@country.GetString("Ecom:Country.Name")</option>
								}
							}
						</select>
						<label for="EcomOrderCustomerCountry">@Translate("Country")</label>
					</div>
				</div>
			</div>
		</div>

		<div class="g-col-12">
			<div class="form-floating">
				<input @disabled type="tel" class="form-control" name="EcomOrderCustomerPhone" id="EcomOrderCustomerPhone" value="@GetString("Ecom:Order.Customer.Phone")" placeholder="@Translate("Phone number")">
				<label for="EcomOrderCustomerPhone">@Translate("Phone number")</label>
			</div>
		</div>
	</div>

</div>

@if (enableFindLocation && !string.IsNullOrEmpty(apiKey))
{
	<script type="module">
		const assetLink = 'https://maps.googleapis.com/maps/api/js?key=@(apiKey)&libraries=places';

		let assetFound = false;
		document.head.querySelectorAll('script').forEach(element => {
			if (assetLink == element.src) {
				assetFound = true;
			}
		});

		swift.AssetLoader.Load(assetLink, 'js');

		if (!assetFound) {
			document.addEventListener('load.swift.assetloader', function() {
				var mapSettings = { currentCountry: "@currentCountry" };
				swift.Places.init(mapSettings);
			});
		} else if (typeof google !== 'undefined') {
			var mapSettings = { currentCountry: "@currentCountry" };
			swift.Places.init(mapSettings);
		}
	</script>
}
