@model CheckoutShippingAddressModel

<div class="checkout-data">
    @if (Model.DisplayPickupInStore && Model.PickupPointsModel.AllowPickupInStore)
    {
        @await Html.PartialAsync("_PickupPoints", Model.PickupPointsModel)
        <script>
            $(document).ready(function () {
                $(document).unbind('checkout_toggle_pickup_in_store');
                $(document).on('checkout_toggle_pickup_in_store', function (e) {
                    if (e.checked)
                        $('#shipping-addresses-form').hide();
                    else
                        $('#shipping-addresses-form').show();
                });
            });
        </script>
    }
    @if (Model.PickupPointsModel == null || !Model.PickupPointsModel.PickupInStoreOnly)
    {
        <div class="shipping-addresses" id="shipping-addresses-form">
            @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.OpCheckoutShippingAddressTop, additionalData = Model })
            @if (Model.ExistingAddresses.Count > 0)
            {
                <div class="section select-shipping-address">
                    <label for="shipping-address-select">@T("Checkout.SelectShippingAddressOrEnterNewOne")</label>
                    @if (Model.InvalidExistingAddresses.Count > 0)
                    {
                        <label class="min-amount-warning">
                            @string.Format(T("Checkout.Addresses.Invalid").Text, Model.InvalidExistingAddresses.Count)
                        </label>
                    }
                    <div>
                        <select name="shipping_address_id" id="shipping-address-select" class="address-select"
                                title="" onchange="Shipping.newAddress(!this.value)">
                            @foreach (var address in Model.ExistingAddresses)
                            {
                                var addressLine = "";
                                addressLine += address.FirstName;
                                addressLine += " " + address.LastName;
                                if (address.StreetAddressEnabled && !string.IsNullOrEmpty(address.Address1))
                                {
                                    addressLine += ", " + address.Address1;
                                }
                                if (address.CityEnabled && !string.IsNullOrEmpty(address.City))
                                {
                                    addressLine += ", " + address.City;
                                }
                                if (address.CountyEnabled && !string.IsNullOrEmpty(address.County))
                                {
                                    addressLine += ", " + address.County;
                                }
                                if (address.StateProvinceEnabled && !string.IsNullOrEmpty(address.StateProvinceName))
                                {
                                    addressLine += ", " + address.StateProvinceName;
                                }
                                if (address.ZipPostalCodeEnabled && !string.IsNullOrEmpty(address.ZipPostalCode))
                                {
                                    addressLine += " " + address.ZipPostalCode;
                                }
                                if (address.CountryEnabled && !string.IsNullOrWhiteSpace(address.CountryName))
                                {
                                    addressLine += ", " + address.CountryName;
                                }
                                //how should we render "FormattedCustomAddressAttributes" here?
                                <option value="@(address.Id)">@(addressLine)</option>
                            }
                            <option value="" selected="@Model.NewAddressPreselected">@T("Checkout.NewAddress")</option>
                        </select>
                    </div>
                </div>
            }
            @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.OpCheckoutShippingAddressMiddle, additionalData = Model })
            <div class="section new-shipping-address" id="shipping-new-address-form">
                <div class="enter-address">
                    <div asp-validation-summary="ModelOnly" class="message-error"></div>
                    @{
                        var dataDictAddress = new ViewDataDictionary(ViewData);
                        dataDictAddress.TemplateInfo.HtmlFieldPrefix = "ShippingNewAddress";
                        @await Html.PartialAsync("_CreateOrUpdateAddress", Model.ShippingNewAddress, dataDictAddress)
                    }
                </div>
            </div>
            @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.OpCheckoutShippingAddressBottom, additionalData = Model })
        </div>
    }
</div>