using C1.Web.Wijmo.Controls.C1GridView;
using Newtonsoft.Json;
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;

namespace C1WebForms_GridView
{
    public partial class _Default : Page
    {
        readonly string dataSourcePath = Path.Combine(HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data"), "Data.json");
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            var dt = GetData(dataSourcePath);
            C1GridView1.DataSource = dt;
            C1GridView1.DataBind();

        }

        public void C1GridView1_PageIndexChanging(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewPageEventArgs e)
        {
            // Set the new page index and rebind the grid
            C1GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        static DataTable GetData(string filePath)
        {
            string json = System.IO.File.ReadAllText(filePath);
            var dataTable = JsonConvert.DeserializeObject<DataTable>(json);

            return dataTable;
        }

        protected void C1GridView1_ColumnGrouped(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnGroupedEventArgs e)
        {
            // Loop through all columns currently in the grid
            foreach (C1BoundField col in C1GridView1.Columns)
            {
                // If the column in the loop is NOT the one the user just grouped...
                if (col != e.Drag)
                {
                    col.GroupInfo.Position = C1.Web.Wijmo.Controls.C1GridView.GroupPosition.None;
                }
            }
            // Rebind the grid to reflect the updated, single-column grouping
            BindGrid();
        }

        protected void C1GridView1_ColumnMoved(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnMovedEventArgs e)
        {
            BindGrid();
        }

        protected void C1GridView1_ColumnUngrouped(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnUngroupedEventArgs e)
        {
            // Logic: Reset expand/collapse states or log the change
            // This triggers when a user drags a column out of the group area
        }

        protected void C1GridView1_Sorting(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewSortEventArgs e)
        {
        }

        protected void C1GridView1_RowDataBound(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewRowEventArgs e)
        {
            // We target the GroupHeader row type specifically
            if (e.Row.RowType == C1.Web.Wijmo.Controls.C1GridView.C1GridViewRowType.DataRow)
            {
                // Set the background color
                e.Row.BackColor = System.Drawing.Color.White;

                // Optional: Set text color and font weight
                e.Row.ForeColor = System.Drawing.Color.Black;
                e.Row.Font.Bold = true;
            }
        }
    }
}