@using BTCPayServer.Client
@model WebhooksViewModel
@{
    ViewData.SetLayoutModel(new LayoutModel(nameof(StoreNavPages.Webhooks), StringLocalizer["Webhooks"]).SetCategory(WellKnownCategories.Store));
}

<div class="sticky-header align-items-start">
    <div class="d-flex flex-column gap-1 flex-grow-1">
        <h2 class="mb-0">@ViewData["Title"]</h2>
        <p class="text-secondary mb-0" text-translate="true">Webhooks allow BTCPay Server to send HTTP events related to your store to another server.</p>
    </div>
	<a id="page-primary" text-translate="true" asp-action="NewWebhook" class="btn btn-primary" role="button" asp-route-storeId="@Context.GetRouteValue("storeId")" permission="@Policies.CanModifyStoreSettings">
        Create Webhook
    </a>
</div>
<partial name="_StatusMessage" />

<div class="row">
    <div class="col-xl-8 col-xxl-constrain">
        <div class="settings-section">
        @if (Model.Webhooks.Any())
        {
            <div class="table-responsive-md settings-section__table">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th text-translate="true">Status</th>
                            <th text-translate="true">Url</th>
                            <th class="actions-col" permission="@Policies.CanModifyStoreSettings" text-translate="true">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var wh in Model.Webhooks)
                        {
                            <tr>
                                <td class="align-baseline">
                                    @if (wh.LastDeliverySuccessful)
                                    {
                                        <span id="delivery-status-icon"
                                              data-bs-toggle="tooltip"
                                              title="@(wh.LastDeliveryTimeStamp == null ? StringLocalizer["No deliveries for this webhook yet"] : StringLocalizer["Last delivery {0}", wh.LastDeliveryTimeStamp.Value.ToTimeAgo()])">
                                            <vc:icon symbol="checkmark" css-class="text-success" />
                                        </span>
                                    }
                                    else
                                    {
                                        <span id="delivery-status-icon"
                                              data-bs-toggle="tooltip"
                                              data-bs-html="true"
                                              title="Error: @wh.LastDeliveryErrorMessage. <br/> Delivery last attempted <span id='last-delivery-time'>@wh.LastDeliveryTimeStamp?.ToTimeAgo()</span>">
                                            <vc:icon symbol="cross" css-class="text-danger" />
                                        </span>
                                    }
                                </td>
                                <td class="d-block text-break">@wh.Url</td>
                                <td class="actions-col text-md-nowrap" permission="@Policies.CanModifyStoreSettings">
                                    <a asp-action="ModifyWebhook" asp-route-webhookId="@wh.Id" text-translate="true">Modify</a> -
                                    <a asp-action="DeleteWebhook" asp-route-webhookId="@wh.Id" data-bs-toggle="modal" data-bs-target="#ConfirmModal" data-confirm-input="@StringLocalizer["Delete"]" text-translate="true">Delete</a>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
            <partial name="_Confirm" model="@(new ConfirmModel(StringLocalizer["Delete Webhook"], StringLocalizer["This webhook will be removed from this store."], StringLocalizer["Delete"]))" permission="@Policies.CanModifyStoreSettings" />
        }
        else
        {
            <p class="text-secondary mb-0" text-translate="true">
                There are no webhooks yet.
            </p>
        }
        </div>
    </div>
</div>

@section PageFootContent {
    <partial name="_ValidationScriptsPartial" />
}
