@model ConfigurationModel

@{
    Layout = "_ConfigurePlugin";
}

<script>
    $(document).ready(function () {
        $("#@Html.IdFor(model => model.UseSandbox)").click(toggleSandbox);
        $("#@Html.IdFor(model => model.UseAllAvailableCarriers)").click(toggleAllCarriers);
        $("#@Html.IdFor(model => model.AddressVerification)").click(toggleAddressVerification);
        toggleSandbox();
        toggleAddressVerification();
    });

    function toggleSandbox() {
        if ($('#@Html.IdFor(model => model.UseSandbox)').is(':checked')) {
            $('#api-key').hideElement();
            $('#test-api-key').showElement();
            $('#carriers').hideElement();
        } else {
            $('#api-key').showElement();
            $('#test-api-key').hideElement();
            $('#carriers').showElement();
            toggleAllCarriers();
        }
    }

    function toggleAllCarriers() {
        if ($('#@Html.IdFor(model => model.UseAllAvailableCarriers)').is(':checked')) {
            $('#carrier-accounts').hideElement();
        } else {
            $('#carrier-accounts').showElement();
        }
    }

    function toggleAddressVerification() {
        if ($('#@Html.IdFor(model => model.AddressVerification)').is(':checked')) {
            $('#strict-address-verification').showElement();
        } else {
            $('#strict-address-verification').hideElement();
        }
    }
</script>

<form asp-controller="EasyPost" asp-action="Configure" method="post">
    <div class="cards-group">

        <div class="card card-default no-margin">
            <div class="card-header">
                @T("Plugins.Shipping.EasyPost.Configuration.Credentials")
            </div>
            <div class="card-body">
                <p>
                    To enable this shipping provider, you'll need to:<br />
                    <br />
                    1. <a href="https://www.easypost.com/signup" target="_blank">Sign up for an EasyPost account</a><br />
                    2. Enter your carrier specific credentials on the <a href="https://www.easypost.com/account/carriers" target="_blank">Carrier Account Dashboard</a><br />
                    <em>Note: Unless you've entered your carrier information for other carriers, you'll just receive USPS rates.</em><br />
                    <em>Note: Negotiated rates are only available in Production mode.</em><br />
                    3. Find your <a href="https://www.easypost.com/account/api-keys" target="_blank">API keys</a>. Use Test key for testing purposes and switch to your Production key when you're ready to buy real postage.<br />
                    See <a href="https://docs.nopcommerce.com/en/getting-started/configure-shipping/shipping-providers/easypost.html" target="_blank">documentation</a> for more information.<br />
                </p>
                <div class="form-group row">
                    <div class="col-md-3">
                        <nop-label asp-for="UseSandbox" />
                    </div>
                    <div class="col-md-9">
                        <nop-editor asp-for="UseSandbox" />
                        <span asp-validation-for="UseSandbox"></span>
                    </div>
                </div>
                <div class="form-group row" id="api-key">
                    <div class="col-md-3">
                        <nop-label asp-for="ApiKey" />
                    </div>
                    <div class="col-md-9">
                        <nop-editor asp-for="ApiKey" asp-required="true" html-attributes="@(new { value = Model.ApiKey })" />
                        <span asp-validation-for="ApiKey"></span>
                    </div>
                </div>
                <div class="form-group row" id="test-api-key">
                    <div class="col-md-3">
                        <nop-label asp-for="TestApiKey" />
                    </div>
                    <div class="col-md-9">
                        <nop-editor asp-for="TestApiKey" html-attributes="@(new { value = Model.TestApiKey })" />
                        <span asp-validation-for="TestApiKey"></span>
                    </div>
                </div>
            </div>
        </div>

        @if (!string.IsNullOrEmpty(Model.ApiKey))
        {
            <div class="card card-default no-margin" id="carriers">
                <div class="card-header">
                    @T("Plugins.Shipping.EasyPost.Configuration.Carriers")
                </div>
                <div class="card-body">
                    <p>
                        When shipment is creating, the service responds with shipping rates for the all carriers you've enabled.
                        If you wish to restrict your rating to a specific carrier, specify carrier accounts here.
                    </p>
                    <div class="form-group row">
                        <div class="col-md-3">
                            <nop-label asp-for="UseAllAvailableCarriers" />
                        </div>
                        <div class="col-md-9">
                            <nop-editor asp-for="UseAllAvailableCarriers" />
                            <span asp-validation-for="UseAllAvailableCarriers"></span>
                        </div>
                    </div>
                    <div class="form-group row" id="carrier-accounts">
                        <div class="col-md-3">
                            <nop-label asp-for="AvailableCarrierAccounts" />
                        </div>
                        <div class="col-md-9">
                            @if (!Model.AvailableCarrierAccounts.Any())
                            {
                                @T("Plugins.Shipping.EasyPost.Configuration.Fields.CarrierAccounts.None")
                            }
                            @foreach (var account in Model.AvailableCarrierAccounts)
                            {
                                <div class="form-check">
                                    <label class="form-check-label">
                                        <input class="form-check-input" type="checkbox" name="@(nameof(Model.CarrierAccounts))" value="@(account.Value)" checked="@(account.Selected)" />
                                        @(account.Text)
                                    </label>
                                </div>
                            }
                        </div>
                    </div>
                </div>
            </div>
        }

        <div class="card card-default no-margin">
            <div class="card-header">
                @T("Plugins.Shipping.EasyPost.Configuration.AddressVerification")
            </div>
            <div class="card-body">
                <p>
                    EasyPost offers several verification tools that can be used to detect deliverability issues, correct minor errors in spelling/formatting, and determine if an address is residential or not (which has a significant effect on ahipment rating for many carriers).<br />
                    The address verification service supports 240+ countries. See the <a href="https://www.easypost.com/docs/address-verification-by-country" target="_blank">full listing</a> to see which verification level is available for each country. <br />
                    International AVS is also a premium, stand-alone service that must be subscribed to in order to be used. If you'd like to learn more about International AVS, <a href="https://www.easypost.com/talk-to-easypost" target="_blank">contact representatives</a> for more information.
                </p>
                <div class="form-group row">
                    <div class="col-md-3">
                        <nop-label asp-for="AddressVerification" />
                    </div>
                    <div class="col-md-9">
                        <nop-editor asp-for="AddressVerification" />
                        <span asp-validation-for="AddressVerification"></span>
                    </div>
                </div>
                <nop-nested-setting asp-for="AddressVerification" id="strict-address-verification" disable-auto-generation="true">
                    <div class="form-group row">
                        <div class="col-md-3">
                            <nop-label asp-for="StrictAddressVerification" />
                        </div>
                        <div class="col-md-9">
                            <nop-editor asp-for="StrictAddressVerification" />
                            <span asp-validation-for="StrictAddressVerification"></span>
                        </div>
                    </div>
                </nop-nested-setting>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-md-9 offset-md-3">
                <button type="submit" name="save" class="btn btn-primary">@T("Admin.Common.Save")</button>
            </div>
        </div>

    </div>
</form>