@page "/"
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.DropDowns

<SfGrid DataSource="@Orders" AllowFiltering="true">
    <GridEvents OnActionBegin="OnActionBegin" TValue="Order"></GridEvents>
    <GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Excel"></GridFilterSettings>
    <GridColumns>
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
    </GridColumns>
</SfGrid>

@code{
    public List<Order> Orders { get; set; }
    public List<EmployeeData> ProjectResults { get; set; }
    public string FilterCol { get; set; }
    public void OnActionBegin(ActionEventArgs<Order> Args)
    {
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.FilterBeforeOpen)
        {
            //to find the current filtering column name
            FilterCol = Args.ColumnName;
        }
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.FilterSearchBegin)
        {
            Console.WriteLine("Triggered");
            if (string.Equals(FilterCol, "OrderDate", StringComparison.Ordinal))
            {
                if (Args.SearchString != "")
                {
                    //pass the searchstring to custommethod and return the data
                    var lists = GetSearchedList(Args.SearchString, "OrderDate");
                    Args.CheckboxListData = lists.AsEnumerable<Order>();
                }
            }
            if (string.Equals(FilterCol, "CustomerID", StringComparison.Ordinal))
            {
                if (Args.SearchString != "")
                {
                    //pass the searchstring to custommethod and return the data
                    var lists = GetSearchedList(Args.SearchString, "CustomerID");
                    //bind resultant list to CheckboxListData.
                    Args.CheckboxListData = lists.AsEnumerable<Order>();
                }
            }
        }
    }
    public List<Order> GetSearchedList(string searchstring, string col)
    {
        var data = new List<Order>();
        if (col == "OrderDate")
        {
            var SearchedValues = Orders.Where(x => x.OrderDate.ToString().Contains(searchstring));
            Console.WriteLine(SearchedValues.Count());

            foreach (var search in SearchedValues)
            {
                if (data.Count == 0)
                {
                    data.Add(search);
                }
                else
                {
                    foreach (var d in data)
                    {
                        if (search.OrderDate != d.OrderDate)
                        {
                            data.Add(d);
                        }
                    }
                }
            }
        }
        else
        {
            var SearchedValues = Orders.Where(x => x.CustomerID.Contains(searchstring));
            Console.WriteLine(SearchedValues.Count());

            foreach (var search in SearchedValues)
            {
                if (data.Count == 0)
                {
                    data.Add(search);
                }
                else
                {
                    foreach (var d in data)
                    {
                        if (search.CustomerID != d.CustomerID)
                        {
                            data.Add(d);
                        }
                    }

                }
            }
        }
        return data;
    }
    protected override void OnInitialized()
    {
        ProjectResults = Enumerable.Range(1, 75).Select(x => new EmployeeData()
        {
            Id = Guid.NewGuid(),
            Name = (new string[] { "ALFKI" + x.ToString(), "ANANTR" + x.ToString(), "ANTON" + x.ToString(), "BLONP" + x.ToString(), "BOLID" + x.ToString() })[new Random().Next(5)],

        }).ToList();

        Orders = Enumerable.Range(1, 75).Select(x => new Order()
        {
            OrderID = 1000 + x,
            CustomerID = (new string[] { "Nancy", "Andrew", "Janet", "Margaret", "Steven" })[new Random().Next(5)],
            Tested = ProjectResults[x - 1].Id,
            Freight = 2.1 * x,
            OrderDate = (new DateTime[] { new DateTime(2010, 5, 1), new DateTime(2010, 5, 2), new DateTime(2010, 5, 3), new DateTime(2010, 5, 4), new DateTime(2010, 5, 8), new DateTime(2010, 5, 9), new DateTime(2010, 5, 7), new DateTime(2010, 5, 6), new DateTime(2010, 5, 5), })[new Random().Next(9)],
        }).ToList();

    }
    public class EmployeeData
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }
    public class Order
    {
        public int? OrderID { get; set; }
        public string CustomerID { get; set; }
        public Guid Tested { get; set; }
        public DateTime? OrderDate { get; set; }
        public double? Freight { get; set; }
    }
}