@model BestsellersReportModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = Loc["Admin.Reports.Bestsellers"];
}

<div class="row">
    <div class="col-md-12">
        <div class="x_panel light form-fit popup-window">
            <div class="x_title">
                <div class="caption level-caption">
                    <i class="fa fa-list-alt"></i>
                    @Loc["Admin.Reports.Bestsellers"]
                </div>

            </div>
            <div class="x_content form">
                <vc:admin-widget widget-zone="bestsellers_report" additional-data="Model" />
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="x_content">
                            <div class="form-horizontal">
                                <div class="form-body">
                                    <div class="main-header col-12 px-0">
                                        <div class="row align-items-end">
                                            <div class="col-md-4 col-ms-12 col-12">
                                                <div class="form-group mb-0">
                                                    <admin-label asp-for="StoreId" class="control-label" />
                                                    <admin-select asp-for="StoreId" asp-items="Model.AvailableStores" />
                                                </div>
                                            </div>
                                            <div class="col-md-4 col-ms-12 col-12" @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                                <div class="form-group mb-0">
                                                    <admin-label asp-for="OrderStatusId" class="control-label" />
                                                    <admin-select asp-for="OrderStatusId" asp-items="Model.AvailableOrderStatuses" />
                                                </div>
                                            </div>
                                            <div class="col-md-4 col-sm-12 col-12">
                                                <div class="form-actions">
                                                    <div class="btn-group">
                                                        <button class="btn btn-success filter-submit" id="search-salesreport">
                                                            <i class="fa fa-search"></i> @Loc["Admin.Reports.Bestsellers.RunReport"]
                                                        </button>
                                                        <button class="btn btn-info" type="button" data-toggle="collapse" data-target="#filterCollapse" aria-expanded="false" aria-controls="filterCollapse">
                                                            <i class="fa fa-filter"></i>&nbsp; @Loc["Admin.Common.Filters"]
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="collapse" id="filterCollapse">
                                        <div class="drop-filters-container w-100">
                                            <div class="form-group">
                                                <admin-label asp-for="StartDate" class="col-sm-3 control-label" />
                                                <div class="col-md-9 col-sm-9">
                                                    <admin-input asp-for="StartDate" />
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <admin-label asp-for="EndDate"  class="col-sm-3 control-label" />
                                                <div class="col-md-9 col-sm-9">
                                                    <admin-input asp-for="EndDate" />
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <admin-label asp-for="PaymentStatusId" class="col-sm-3 control-label" />
                                                <div class="col-md-9 col-sm-9">
                                                    <admin-select asp-for="PaymentStatusId" asp-items="Model.AvailablePaymentStatuses" />
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <admin-label asp-for="BillingCountryId" class="col-sm-3 control-label" />
                                                <div class="col-md-9 col-sm-9">
                                                    <admin-select asp-for="BillingCountryId" asp-items="Model.AvailableCountries" />
                                                </div>
                                            </div>
                                            <div class="form-group" @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                                <admin-label asp-for="VendorId"  class="col-sm-3 control-label" />
                                                <div class="col-md-9 col-sm-9">
                                                    <admin-select asp-for="VendorId" asp-items="Model.AvailableVendors" />
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="x_content">
                                    <div id="salesreport-grid"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        $("#salesreport-grid").kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("BestsellersReportList", "Reports", new { area = Constants.AreaAdmin }))",
                        type: "POST",
                        dataType: "json",
                        data: additionalData
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors"
                },
                error: function(e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                pageSize: @(adminAreaSettings.DefaultGridPageSize),
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            pageable: {
                refresh: true,
                pageSizes: [@(adminAreaSettings.GridPageSizes)]
            },
            editable: {
                confirmation: false,
                mode: "inline"
            },
            scrollable: false,
            columns: [{
                field: "ProductName",
                title: "@Loc["Admin.Reports.Bestsellers.Fields.Name"]",
                template: '<a class="k-link" href="@Url.Action("Edit", "Product", new { area = Constants.AreaAdmin })/#=ProductId#">#=kendo.htmlEncode(ProductName)#</a>'
            }, {
                field: "TotalQuantity",
                title: "@Loc["Admin.Reports.Bestsellers.Fields.TotalQuantity"]"
            },{
                field: "TotalAmount",
                title: "@Loc["Admin.Reports.Bestsellers.Fields.TotalAmount"]"
            }]
        });
    });
</script>
<script>

    $(document).ready(function () {

        $('#search-salesreport').click(function () {
            var grid = $('#salesreport-grid').data('kendoGrid');
            grid.dataSource.page(1); //new search. Set page size to 1
            //grid.dataSource.read(); we already loaded the grid above using "page" function
            return false;
        });

    });

    function additionalData() {
        var data = {
            StartDate: $('#@Html.IdFor(model => model.StartDate)').val(),
            EndDate: $('#@Html.IdFor(model => model.EndDate)').val(),
            StoreId: $('#StoreId').val(),
            OrderStatusId: $('#OrderStatusId').val(),
            PaymentStatusId: $('#PaymentStatusId').val(),
            BillingCountryId: $('#BillingCountryId').val(),
            VendorId: $('#VendorId').val()
        };
        addAntiForgeryToken(data);
        return data;
    }

</script>
