ildar-shaymukhametov_grandnode-refactorings/Grand.Web/Areas/Admin/Views/Order/List.cshtml
@model OrderListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = T("Admin.Orders").Text;
}
<form asp-area="@Constants.AreaAdmin" asp-controller="Order" asp-action="List" method="post">
    
    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="icon-basket"></i>
                        @T("Admin.Orders")
                    </div>
                    <div class="actions btn-group btn-group-devided util-btn-margin-bottom-5">
                        @if (!Model.IsLoggedInAsVendor)
                        {
                            <div class="btn-group btn-group-devided">
                                <a class="btn default" href="javascript:;" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
                                    <i class="fa fa-download"></i>
                                    @T("Admin.Common.Export")
                                    <i class="fa fa-angle-down"></i>
                                </a>
                                <ul class="dropdown-menu pull-right">
                                    <li>
                                        <input type="submit" name="exportxml-all" class="btn default" style="width: 100%;" value="@T("Admin.Common.ExportToXml.All")" />
                                    </li>
                                    <li>
                                        <input type="button" id="exportxml-selected" class="btn default" style="width: 100%;" value="@T("Admin.Common.ExportToXml.Selected")" />
                                    </li>
                                    <li>
                                        <input type="submit" name="exportexcel-all" class="btn default" style="width: 100%;" value="@T("Admin.Common.ExportToExcel.All")" />
                                    </li>
                                    <li>
                                        <input type="button" id="exportexcel-selected" class="btn default" style="width: 100%;" value="@T("Admin.Common.ExportToExcel.Selected")" />
                                    </li>
                                </ul>
                                <vc:admin-widget widget-zone="order_list_buttons" additional-data="null" />
                            </div>
                        }
                        <button type="submit" name="pdf-invoice-all" class="btn green">
                            <i class="fa fa-file-pdf-o"></i>
                            @T("Admin.Orders.PdfInvoice.All")
                        </button>
                        <button type="button" id="pdf-invoice-selected" class="btn green">
                            <i class="fa fa-file-pdf-o"></i>
                            @T("Admin.Orders.PdfInvoice.Selected")
                        </button>
                        <button type="button" name="delete-selected" id="delete-selected" class="btn red" onclick=" return confirm('@T("Admin.Common.AreYouSure")'); ">
                            <i class="fa fa-trash-o"></i>
                            @T("Admin.Common.Delete.Selected")
                        </button>
                    </div>

                </div>
                <div class="x_content form">
                    <div class="form-horizontal">

                        <div class="form-body col-md-6">
                            <div class="form-group">
                                <admin-label asp-for="BillingEmail" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-input asp-for="BillingEmail" />
                                </div>
                            </div>
                        </div>
                        <div class="form-body col-md-6">
                            <div class="form-group form-inline">
                                <admin-label asp-for="GoDirectlyToNumber" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <div class="input-group">
                                        <admin-input asp-for="GoDirectlyToNumber" />
                                        <input type="submit" name="go-to-order-by-number" id="gotoorderbynumber" class="input-group btn btn-default" value="@T("Admin.Common.Go")" />
                                    </div>  
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>

                        <div class="form-body col-md-6 extend-collapse">
                            <div class="form-group">
                                <admin-label asp-for="StartDate" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-input asp-for="StartDate" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="EndDate" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-input asp-for="EndDate" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="BillingLastName" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-input asp-for="BillingLastName" />
                                </div>
                            </div>


                            <div class="form-group">
                                <admin-label asp-for="BillingCountryId" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="BillingCountryId" asp-items="Model.AvailableCountries" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="ProductId" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <input class="form-control" type="text" id="search-product-name" autocomplete="off" />
                                    <span id="search-product-friendly-name"></span>
                                    <input type="button" id="search-product-clear" class="k-button" style="display:none;" value="@T("Admin.Common.Clear")" />
                                    @Html.TextBoxFor(model => model.ProductId, new { autocomplete = "off", style = "display:none;" })
                                    <script>
                                            $(document).ready(function() {
                                                $('#search-product-name').autocomplete({
                                                    delay: 500,
                                                    minLength: 3,
                                                    source: '@(Url.Action("ProductSearchAutoComplete"))',
                                                    select: function(event, ui) {
                                                        $('#@Html.FieldIdFor(model => model.ProductId)').val(ui.item.productid);
                                                        $('#search-product-friendly-name').text(ui.item.label);

                                                        $('#search-product-clear').show();
                                                        return false;
                                                    }
                                                });

                                                //remove button
                                                $('#search-product-clear').click(function () {
                                                    $('#@Html.FieldIdFor(model => model.ProductId)').val('0');
                                                    $('#search-product-friendly-name').text('');
                                                    $('#search-product-clear').hide();
                                                    return false;
                                                });
                                            });
                                    </script>
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="OrderGuid" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-input asp-for="OrderGuid" />
                                </div>
                            </div>
                        </div>
                        <div class="form-body col-md-6 extend-collapse">
                            <div class="form-group" @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                <admin-label asp-for="OrderStatusId" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="OrderStatusId" asp-items="Model.AvailableOrderStatuses" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="PaymentStatusId" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="PaymentStatusId" asp-items="Model.AvailablePaymentStatuses" />
                                </div>
                            </div>
                            <div class="form-group" @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                <admin-label asp-for="ShippingStatusId" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="ShippingStatusId" asp-items="Model.AvailableShippingStatuses" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="StoreId" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="StoreId" asp-items="Model.AvailableStores" />
                                </div>
                            </div>
                            <div class="form-group" @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                <admin-label asp-for="VendorId" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="VendorId" asp-items="Model.AvailableVendors" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="WarehouseId" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="WarehouseId" asp-items="Model.AvailableWarehouses" />
                                </div>
                            </div>
                            <div class="form-group" @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                <admin-label asp-for="PaymentMethodSystemName" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="PaymentMethodSystemName" asp-items="Model.AvailablePaymentMethods" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="OrderTag" class="col-4 col-sm-4 col-md-4 control-label" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="OrderTag" asp-items="Model.AvailableOrderTags" />
                                </div>
                            </div>
                        </div>
                        <div class="form-actions d-flex flex-column w-100">
                            <div class="row">
                                <div class="offset-md-5 offset-sm-5 col-md-7 col-sm-7">
                                    <button class="btn btn-success filter-submit" id="search-orders">
                                        <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                    </button>
                                    <button type="button" class="btn btn-default filter-extend-button">
                                        <i class="fa fa-filter"></i>
                                    </button>
                                </div>
                            </div>
                        </div>

                        <div class="x_content">
                            <div id="orders-grid"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function () {

            $('.extend-collapse').hide();
            $('.filter-extend-button').click(function () {
                $('.extend-collapse').toggle("slow");
                $('.filter-text-close').toggle();
            });


            $("#orders-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("OrderList", "Order"))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors",
                        model: {
                            id: "Id",
                            fields: {
                                Id: { editable: false, type: "string" },
                                CreatedOn: { type: "Date" },
                                StoreName: { editable: false, type: "string" },
                                OrderTotal: { editable: false, type: "string" },
                                OrderStatus: { editable: false, type: "string" },
                                PaymentStatus: { editable: false, type: "string" },
                                ShippingStatus: { editable: false, type: "string" },
                                CustomerEmail: { editable: false, type: "string" },
                                CustomerFullName: { editable: false, type: "string" },
                                OrderStatusId: { editable: false, type: "number" },
                            }
                        }
                    },
                    requestEnd: function (e) {
                        if (e.type == "read") {
                            var response = e.response;
                            if (response) {
                                //store extra data
                                reportAggregates = e.response["ExtraData"];
                            }
                        }
                    },
                    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,
                dataBound: onDataBound,
                columns: [
                    {
                        field: "Id",
                        headerTemplate: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input id='mastercheckbox' type='checkbox'/> <div class='control__indicator'></div></label>",
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:center" },
                        template: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input type='checkbox' value='#=Id#' class='checkboxGroups'/> <div class='control__indicator'></div></label>",
                        width: 40
                    },
                    {
                        field: "OrderNumber",
                        title: "@T("Admin.Orders.Fields.Id")",
                        width: 50,
                        template: '<a class="k-link" href="Edit/#=Id#">#=OrderNumber#</a>'
                    },
                    @if (!Model.IsLoggedInAsVendor)
                    {
                        //a vendor does not have access to this functionality
                        <text>{
                        field: "OrderStatus",
                        title: "@T("Admin.Orders.Fields.OrderStatus")",
                        width: 120,
                        template: '<h4><a class="k-link orderstatuslist" href="Edit/#=Id#"><span class="label label-' +
                            '# if(OrderStatusId == 10) {#warning #} #' +
                            '# if(OrderStatusId == 20) {#info #} #' +
                            '# if(OrderStatusId == 30) {#success #} #' +
                            '# if(OrderStatusId == 40) {#danger #} #' +
                            '">#=OrderStatus#</span></a></h4>'
                        },</text>
                    }
                    {
                        field: "PaymentStatus",
                        title: "@T("Admin.Orders.Fields.PaymentStatus")",
                        width: 120,
                        template: '<a class="k-link" href="Edit/#=Id#">#=PaymentStatus#</a>'
                    },
                    @if (!Model.IsLoggedInAsVendor)
                    {
                        //a vendor does not have access to this functionality
                        <text>{
                        field: "ShippingStatus",
                        title: "@T("Admin.Orders.Fields.ShippingStatus")",
                        width: 120,
                        template: '<a class="k-link" href="Edit/#=Id#">#=ShippingStatus#</a>'
                    },</text>
                                }
                    {
                        field: "CustomerEmail",
                        title: "@T("Admin.Orders.Fields.Customer")",
                        width: 220,
                        @if (!Model.IsLoggedInAsVendor)
                        {
                            <text>
                            template: function(dataItem) {
                                    return "<a class=\"k-link\" href='@Url.Action("Edit", "Customer")/" + dataItem.CustomerId + "'>" + kendo.htmlEncode(dataItem.CustomerFullName) + " (" + dataItem.CustomerEmail + ")</a>";
                            }
                            </text>
                        }
                        },
                    {
                        field: "StoreName",
                        title: "@T("Admin.Orders.Fields.Store")",
                        width: 150,
                        minScreenWidth: 770,
                    }, {
                        field: "CreatedOn",
                        title: "@T("Admin.Orders.Fields.CreatedOn")",
                        width: 180,
                        type: "date",
                        format: "{0:G}",

                    },
                    @if (!Model.IsLoggedInAsVendor)
                    {
                        //a vendor does not have access to this functionality
                        <text>{
                        field: "OrderTotal",
                        title: "@T("Admin.Orders.Fields.OrderTotal")",
                        width: 150,
                        template: '#=OrderTotal#',
                        headerAttributes: { style: "text-align:right" },
                        attributes: { style: "text-align:right" },
                        footerTemplate: '<div><strong>@T("Admin.Orders.Report.Summary"):</strong></div>' +
                                                '<div>@T("Admin.Orders.Report.Profit"): <span class=\"pull-right\" id=\"aggregator-profit-block\"></span></div>' +
                                                '<div>@T("Admin.Orders.Report.Shipping"): <span class=\"pull-right\" id=\"aggregator-shipping-block\"></span></div>' +
                                                '<div>@T("Admin.Orders.Report.Tax"): <span class=\"pull-right\" id=\"aggregator-tax-block\"></span></div>' +
                                                '<div>@T("Admin.Orders.Report.Total"): <span class=\"pull-right\" id=\"aggregator-total-block\"></span></div>'
                    },</text>
                    }
                ]
            } );
        });
    </script>

    <script>

        var selectedIds = [];
        var reportAggregates = "";

        $(document).ready(function () {

            //search button
            $('#search-orders').click(function () {
                //search
                var grid = $('#orders-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
                //clear selected checkboxes
                $('.checkboxGroups').prop('checked', false).change();
                selectedIds = [];
                return false;
            });

            $("#@Html.FieldIdFor(model => model.BillingEmail)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-orders").click();
                    return false;
                }
            });

            $("#@Html.FieldIdFor(model => model.BillingLastName)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-orders").click();
                    return false;
                }
            });

            $("#@Html.FieldIdFor(model => model.OrderGuid)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-orders").click();
                    return false;
                }
            });
            $("#@Html.FieldIdFor(model => model.StartDate)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-orders").click();
                    return false;
                }
            });
            $("#@Html.FieldIdFor(model => model.EndDate)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-orders").click();
                    return false;
                }
            });

            $("#@Html.FieldIdFor(model => model.GoDirectlyToNumber)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#gotoorderbynumber").click();
                    return false;
                }
            });

            $("#search-product-name").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-orders").click();
                    return false;
                }
            });

            $('#delete-selected').click(function(e) {

                e.preventDefault();

                var postData = {
                    selectedIds: selectedIds
                };
                addAntiForgeryToken(postData);

                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@(Url.Action("DeleteSelected", "Order"))",
                    data: postData,
                    complete: function(data) {
                        //reload grid
                        var grid = $('#orders-grid').data('kendoGrid');
                        grid.dataSource.read();
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError);
                    },
                    traditional: true
                });
                return false;
            });

            $('#mastercheckbox').click(function () {
                $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
            });

            //wire up checkboxes.
            $('#orders-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                var $check = $(this);
                if ($check.is(":checked") == true) {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked == -1) {
                        //add id to selectedIds.
                        selectedIds.push($check.val());
                    }
                }
                else {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked > -1) {
                        //remove id from selectedIds.
                        selectedIds = $.grep(selectedIds, function (item, index) {
                            return item != $check.val();
                        });
                    }
                }
                updateMasterCheckbox();
            });
        });

        function additionalData() {
            var data = {
                StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
                EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
                OrderStatusId: $('#@Html.FieldIdFor(model => model.OrderStatusId)').val(),
                PaymentStatusId: $('#@Html.FieldIdFor(model => model.PaymentStatusId)').val(),
                ShippingStatusId: $('#@Html.FieldIdFor(model => model.ShippingStatusId)').val(),
                StoreId: $('#@Html.FieldIdFor(model => model.StoreId)').val(),
                VendorId: $('#@Html.FieldIdFor(model => model.VendorId)').val(),
                WarehouseId: $('#@Html.FieldIdFor(model => model.WarehouseId)').val(),
                BillingEmail: $('#@Html.FieldIdFor(model => model.BillingEmail)').val(),
                BillingLastName: $('#@Html.FieldIdFor(model => model.BillingLastName)').val(),
                BillingCountryId: $('#@Html.FieldIdFor(model => model.BillingCountryId)').val(),
                PaymentMethodSystemName: $('#@Html.FieldIdFor(model => model.PaymentMethodSystemName)').val(),
                ProductId: $('#@Html.FieldIdFor(model => model.ProductId)').val(),
                OrderGuid: $('#@Html.FieldIdFor(model => model.OrderGuid)').val(),
                GoDirectlyToNumber: $('#@Html.FieldIdFor(model => model.GoDirectlyToNumber)').val(),
                OrderTag: $('#@Html.FieldIdFor(model => model.OrderTag)').val(),
            };
            addAntiForgeryToken(data);
            return data;
        }

        function onDataBound(e) {

            $('#orders-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                var currentId = $(this).val();
                var checked = jQuery.inArray(currentId, selectedIds);
                //set checked based on if current checkbox's value is in selectedIds.
                $(this).prop('checked', checked > -1);
            });

            updateMasterCheckbox();

            //update order totals summary
            if (reportAggregates != "") {
                for (var key in reportAggregates) {
                    $('#aggregator-profit-block').text(reportAggregates['aggregatorprofit']);
                    $('#aggregator-shipping-block').text(reportAggregates['aggregatorshipping']);
                    $('#aggregator-tax-block').text(reportAggregates['aggregatortax']);
                    $('#aggregator-total-block').text(reportAggregates['aggregatortotal']);
                }
            }
        }

        function updateMasterCheckbox() {
            var numChkBoxes = $('#orders-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#orders-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
            $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }
    </script>
</form>


@*export selected (XML). We don't use GET approach because it's limited to 2K-4K chars and won't work for large number of entities*@
<form asp-area="@Constants.AreaAdmin" asp-controller="Order" asp-action="ExportXmlSelected" method="post" id="export-xml-selected-form">
    
    <input type="hidden" id="selectedIds" name="selectedIds" value="" />
</form>

<script>

    $(document).ready(function () {
        $('#exportxml-selected').click(function (e) {
            e.preventDefault();
            var ids = selectedIds.join(",");
            $('#export-xml-selected-form #selectedIds').val(ids);
            $('#export-xml-selected-form').submit();
            return false;
        });
    });

</script>

@*export selected (Excel). We don't use GET approach because it's limited to 2K-4K chars and won't work for large number of entities*@
<form asp-area="@Constants.AreaAdmin" asp-controller="Order" asp-action="ExportExcelSelected" method="post" id="export-excel-selected-form">
    
    <input type="hidden" id="selectedIds" name="selectedIds" value="" />
</form>
<script>

    $(document).ready(function () {
        $('#exportexcel-selected').click(function (e) {
            e.preventDefault();
            var ids = selectedIds.join(",");
            $('#export-excel-selected-form #selectedIds').val(ids);
            $('#export-excel-selected-form').submit();
            return false;
        });
    });

</script>

@*Print packaging slips selected (XML). We don't use GET approach because it's limited to 2K-4K chars and won't work for large number of entities*@
<form asp-area="@Constants.AreaAdmin" asp-controller="Order" asp-action="PdfInvoiceSelected" method="post" id="pdf-invoice-selected-form">    
    
    <input type="hidden" id="selectedIds" name="selectedIds" value="" />
</form>

<script>

    $(document).ready(function () {
        $('#pdf-invoice-selected').click(function (e) {
            e.preventDefault();
            var ids = selectedIds.join(",");
            $('#pdf-invoice-selected-form #selectedIds').val(ids);
            $('#pdf-invoice-selected-form').submit();
            return false;
        });
    });

</script>
