@inherits ViewModelTemplate<UserAddressViewModel>
@using Dynamicweb.Users.Frontend.UserAddresses
@using Dynamicweb.Rendering
@using Dynamicweb.Frontend
@using Dynamicweb.Security.UserManagement

@{
    string currentCountry = !string.IsNullOrEmpty(Model.Address.CountryCode) ? Model.Address.CountryCode : (!string.IsNullOrEmpty(Pageview.User.CountryCode) ? Pageview.User.CountryCode : (Pageview.Area.EcomCountryCode ?? string.Empty));
    var regions = Dynamicweb.Ecommerce.Services.Countries.GetRegions(currentCountry);
}

<form method="post" class="vstack gap-3 h-100" data-swift-address-editform>
    <input type="hidden" name="AddressId" value="@Model.Address.Id">

    <div class="form-floating">
        <select class="form-select" id="CountryCode" name="CountryCode">
            @foreach (var country in Dynamicweb.Ecommerce.Services.Countries.GetCountries())
            {
                <option value="@country.Code2" selected="@(country.Code2 == Model.Address.CountryCode)">@country.GetName(Pageview.Area.EcomLanguageId)</option>
            }
        </select>
        <label for="CountryCode" class="form-label">@Translate("Country")</label>
    </div>

    <div class="form-floating">
        <input type="text" class="form-control" placeholder="@Translate("Name")" name="Name" id="Name" value="@Model.Address.Name" required>
        <label for="Name" class="form-label">@Translate("Name")</label>
    </div>

    <div class="form-floating">
        <input type="text" class="form-control" placeholder="@Translate("Address")" name="Address" id="Address" value="@Model.Address.Address" required />
        <label for="Address" class="form-label">@Translate("Address")</label>
    </div>

    <div class="form-floating">
        <input type="text" class="form-control" placeholder="@Translate("Additional address")" name="Address2" id="Address2" value="@Model.Address.Address2" />
        <label for="Address2" class="form-label">@Translate("Additional address")</label>
    </div>

    <div class="form-floating">
        <input type="text" class="form-control" placeholder="@Translate("City")" name="City" id="City" value="@Model.Address.City" required />
        <label for="City" class="form-label">@Translate("City")</label>
    </div>

    <div class="hstack gap-3">

        @if (regions.Count > 1)
        {
            <div class="form-floating w-50">
                <select class="form-select" name="@Translate("State/Region")" id="State">
                    @foreach (var region in regions)
                    {
                        <option value="@region.RegionCode" selected="@(region.RegionCode == Pageview.User.State)">@region.GetName(Dynamicweb.Ecommerce.Common.Context.LanguageID)</option>
                    }
                </select>
                <label for="State">@Translate("State/Region")</label>
            </div>
        }
        
        <div class="form-floating @(regions.Count > 1 ? "w-50" : "flex-fill")">
            <input type="text" inputmode="numeric" class="form-control" placeholder="@Translate("Zip code")" name="Zip" id="Zip" value="@Model.Address.Zip" required />
            <label for="Zip" class="form-label">@Translate("Zip code")</label>
        </div>

    </div>

    <div class="form-floating">
        <input type="tel" inputmode="numeric" class="form-control" placeholder="@Translate("Phone")" name="Phone" id="Phone" value="@Model.Address.Phone">
        <label for="Email" class="form-label">@Translate("Phone")</label>
    </div>

    <div class="form-floating">
        <input type="email" inputmode="email" class="form-control" placeholder="@Translate("Email")" name="Email" id="Email" value="@Model.Address.Email">
        <label for="Email" class="form-label">@Translate("Email")</label>
    </div>

    <div class="form-floating mb-3 flex-fill">
        <select class="form-select" name="AddressKind" id="AddressKind" aria-label="@Translate("Address usage")">
            <option value="@UserAddressKind.None" @(Model.Address.AddressKind == UserAddressKind.None ? "selected" : string.Empty)>@Translate("Both for billing and delivery")</option>
            <option value="@UserAddressKind.Shipping" @(Model.Address.AddressKind == UserAddressKind.Shipping ? "selected" : string.Empty)>@Translate("Delivery address")</option>
            @if (!Model.Address.IsDefault)
            {
                <option value="@UserAddressKind.Billing" @(Model.Address.AddressKind == UserAddressKind.Billing ? "selected" : string.Empty)>@Translate("Billing address")</option>
            }
        </select>
        <label for="AddressKind" class="form-label">@Translate("Address usage")</label>
    </div>

    <button type="submit" name="Cmd" value="SaveAddress" class="btn btn-primary mt-auto" data-dw-button id="SaveAddressButton">
        @Translate("Save address")
    </button>
</form>

