@inject Nop.Services.Common.IGenericAttributeService genericAttributeService
@inject IWorkContext workContext
@{
    const string cardId = "latest-orders-card";
    const string hideCardAttributeName = "Reports.HideLatestOrdersCard";
    var hideCard = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideCardAttributeName);
}

<div class="card card-primary card-outline @if(hideCard){<text>collapsed-card</text>}" id="@cardId">
    <div class="card-header with-border">
        <h3 class="card-title">
            <i class="fas fa-cart-plus"></i>
            @T("Admin.Dashboard.LatestOrders")
            <a asp-controller="Order" asp-action="List" class="btn btn-xs btn-info btn-flat margin-l-10">@T("Admin.Dashboard.LatestOrders.ViewAll")</a>
        </h3>
        <div class="card-tools float-right">
            <button class="btn btn-tool" data-card-widget="collapse">
                @if (hideCard)
                {
                    <text><i class="fas fa-plus"></i></text>
                }
                else
                {
                    <text><i class="fas fa-minus"></i></text>
                }
            </button>
        </div>
    </div>
    <script>
        $(function() {
            var collapsed = !$('#@cardId').hasClass('collapsed-card');
            if (collapsed) {
                $('#latestOrdersReport').load('@Url.Action("GetLatestOrders", "Home")');
            }

            $('#@cardId').on('click', 'button[data-card-widget="collapse"]', function () {
                collapsed = !$('#@cardId').hasClass('collapsed-card');
                saveUserPreferences('@(Url.Action("SavePreference", "Preferences"))', '@hideCardAttributeName', collapsed);
                if (!collapsed) {
                    $('#latestOrdersReport').load('@Url.Action("GetLatestOrders", "Home")');
                }
            });
        });
    </script>
    <div class="card-body">
        <div id="latestOrdersReport"></div>
        <script asp-location="Footer">
            function renderColumnCustomerEmail(data, type, row, meta) {
                var textRenderer = $.fn.dataTable.render.text().display;
                return textRenderer(row.CustomerFullName) + ' (' + textRenderer(row.CustomerEmail) + ')';
            }

            function renderColumnOrderStatus(data, type, row, meta) {
                var color;
                switch (row.OrderStatusId) {
                    case 10:
                        color = 'yellow';
                        break;
                    case 20:
                        color = 'blue';
                        break;
                    case 30:
                        color = 'green';
                        break;
                    case 40:
                        color = 'red';
                        break;
                }
                return '<span class="grid-report-item ' + color + '">' + data + '</span >';
            }
        </script>
    </div>
</div>