@using Bit.Admin.Enums;
@inject Bit.Admin.Services.IAccessControlService AccessControlService
@model BillingInformationModel

@{
    var canManageTransactions = Model.Entity == "User" ? AccessControlService.UserHasPermission(Permission.User_BillingInformation_CreateEditTransaction)
                                                       : AccessControlService.UserHasPermission(Permission.Org_BillingInformation_CreateEditTransaction);

    var canDownloadInvoice = Model.Entity == "User" ? AccessControlService.UserHasPermission(Permission.User_BillingInformation_DownloadInvoice)
                                                       : AccessControlService.UserHasPermission(Permission.Org_BillingInformation_DownloadInvoice);
}

<dl class="row">
    <dt class="col-sm-4 col-lg-3">Account @(Model.BillingInfo.Balance <= 0 ? "Credit" : "Balance")</dt>
    <dd class="col-sm-8 col-lg-9">@Math.Abs(Model.BillingInfo.Balance).ToString("C")</dd>

    <dt class="col-sm-4 col-lg-3">Invoices</dt>
    <dd class="col-sm-8 col-lg-9">
        @if(Model.BillingHistoryInfo.Invoices?.Any() ?? false)
        {
            <table class="table">
                <tbody>
                    @foreach(var invoice in Model.BillingHistoryInfo.Invoices)
                    {
                        <tr>
                            <td>@invoice.Date</td>
                            <td><a target="_blank" rel="noreferrer" href="@invoice.Url" title="View Invoice">@invoice.Number</a>
                            </td>
                            <td>@invoice.Amount.ToString("C")</td>
                            <td>@(invoice.Paid ? "Paid" : "Unpaid")</td>
                            @if (canDownloadInvoice)
                            {
                                <td>
                                    <a target="_blank" rel="noreferrer" href="@invoice.PdfUrl" title="Download Invoice">
                                        <i class="fa fa-file-pdf-o"></i>
                                    </a>
                                </td>
                            }
                        </tr>
                    }
                </tbody>
            </table>
        }
        else
        {
            @: No invoices.
        }
    </dd>

    <dt class="col-sm-4 col-lg-3">Transactions</dt>
    <dd class="col-sm-8 col-lg-9">
        @if(Model.BillingHistoryInfo.Transactions?.Any() ?? false)
        {
            <table class="table">
                <tbody>
                    @foreach(var transaction in Model.BillingHistoryInfo.Transactions)
                    {
                        <tr>
                            <td>@transaction.CreatedDate</td>
                            <td>@transaction.Type.ToString()</td>
                            <td>@transaction.PaymentMethodType.ToString()</td>
                            <td>@transaction.Details</td>
                            <td>@transaction.Amount.ToString("C")</td>
                            @if (canManageTransactions)
                            {
                                <td>
                                    <a title="Edit Transaction" asp-controller="Tools" asp-action="EditTransaction"
                                    asp-route-id="@transaction.Id"><i class="fa fa-edit"></i></a>
                                </td>
                            }
                        </tr>
                    }
                </tbody>
            </table>
        }
        else
        {
            <p>No transactions.</p>
        }
        @if (canManageTransactions)
        {
            <a asp-action="CreateTransaction" asp-controller="Tools" asp-route-organizationId="@Model.OrganizationId"
            asp-route-userId="@Model.UserId" class="btn btn-sm btn-outline-primary">
                <i class="fa fa-plus"></i> New Transaction
            </a>
        }
    </dd>
</dl>
