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)
        {
            // Example: Notify the user which column is now primary for grouping
            string colName = e.Drag.HeaderText;
            // Response.Write($"<script>alert('Data is now grouped by: {colName}');</script>");
        }

        protected void C1GridView1_ColumnMoved(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnMovedEventArgs e)
        {
            // Logic: Persist the new column order to user preferences/database
            int destIndex = e.DestIndex;
            // System.Diagnostics.Debug.WriteLine($"Column moved to {newIndex}");
        }

        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;
            }
        }
    }
}