@model CheckoutShippingAddressModel
@using Nop.Core;
@inject IWebHelper webHelper

@{
    var storeLocation = webHelper.GetStoreLocation();
}

<div class="checkout-data">
    @if (Model.DisplayPickupInStore && Model.PickupPointsModel.AllowPickupInStore)
    {
        @await Html.PartialAsync("_PickupPoints", Model.PickupPointsModel)
        <script>
            $(function () {
                $(document).unbind('checkout_toggle_pickup_in_store');
                $(document).on('checkout_toggle_pickup_in_store', function (e) {
                    if (e.checked) {
                        $('#shipping-addresses-form').hide();
                        $('#edit-shipping-address-buttons').hide();
                    } else {
                        $('#shipping-addresses-form').show();
                        $('#edit-shipping-address-buttons').show();
                    }
                });
            });
        </script>
    }
    @if (Model.PickupPointsModel == null || !Model.PickupPointsModel.PickupInStoreOnly)
    {
        <div class="shipping-addresses" id="shipping-addresses-form">
            @await Component.InvokeAsync(typeof(WidgetViewComponent), 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 class="row">
                        <div class=" offset-md-3 offset-lg-3 offset-xl-3 col-sm-12 col-md-6 col-lg-6 col-xl-6">
                            <div class="opc-select-address-container">
                                <select name="shipping_address_id" id="shipping-address-select" class="address-select form-control custom-select" title="" onchange="Shipping.newAddress(this.value, @Model.SelectedBillingAddress)">
                                    @foreach (var address in Model.ExistingAddresses)
                                    {
                                        var addressLine = "";
                                        addressLine += address.FirstName;
                                        addressLine += " " + address.LastName;
                                        addressLine += ", " + address.AddressLine;
                                        //how should we render "FormattedCustomAddressAttributes" here?
                                        <option value="@(address.Id)">@(addressLine)</option>
                                    }
                                    <option value="" selected="@Model.NewAddressPreselected">@T("Checkout.NewAddress")</option>
                                </select>
                                <button id="edit-shipping-address-button" type="button" class="btn btn-primary button-1" style="display: none" onclick="Shipping.editAddress('@(storeLocation)checkout/GetAddressById/'); return false;">@T("Common.Edit")</button>

                                <button id="delete-shipping-address-button" type="button" class="btn btn-primary button-1" style="display: none" onclick="Shipping.deleteAddress('@(storeLocation)checkout/DeleteEditShippingAddress/'); return false;">@T("Common.Delete")</button>
                            </div>
                        </div>
                    </div>
                    @if (Model.InvalidExistingAddresses.Count > 0)
                    {
                        <label class="min-amount-warning">
                            @string.Format(T("Checkout.Addresses.Invalid").Text, Model.InvalidExistingAddresses.Count)
                        </label>
                    }
                    
                </div>
            }
            @await Component.InvokeAsync(typeof(WidgetViewComponent), 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 alert alert-danger"></div>
                    @{
                        var dataDictAddress = new ViewDataDictionary(ViewData);
                        dataDictAddress.TemplateInfo.HtmlFieldPrefix = "ShippingNewAddress";
                        @await Html.PartialAsync("_CreateOrUpdateAddress", Model.ShippingNewAddress, dataDictAddress)
                    }
                </div>
            </div>
            @await Component.InvokeAsync(typeof(WidgetViewComponent), new { widgetZone = PublicWidgetZones.OpCheckoutShippingAddressBottom, additionalData = Model })
        </div>
    }
</div>