using C1.Web.Wijmo.Controls.C1GridView;
using System;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web.UI;

namespace GridView_RowsSample
{
    public partial class Testing : Page
    {
        private static DataTable GetData()
        {
            var dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Desc");
            dt.Columns.Add("Check", typeof(bool));

            // 50 rows to simulate a heavier grid like 
            for (int i = 1; i <= 50; i++)
            {
                dt.Rows.Add(i, "name-" + i, "desc-" + i, i % 2 == 0);
            }
            return dt;
        }

		private DataTable GetSampleData()
		{
			DataTable dt = new DataTable();
			// All rows share the same CATEGORY so they form one flat group
			dt.Columns.Add("CATEGORY", typeof(string));
			dt.Columns.Add("TEXT", typeof(string));
			dt.Columns.Add("UNITS", typeof(int));
			dt.Columns.Add("AMOUNT", typeof(decimal));
			dt.Columns.Add("Checkbox", typeof(bool));

			// All rows same category = one group = one Total footer
			dt.Rows.Add("ALL", "Moneda 5¢", 1, 5.00m, true);
			dt.Rows.Add("ALL", "Moneda 10¢", 2, 20.00m, true);
			dt.Rows.Add("ALL", "Moneda 20¢", 0, 0.00m, true);
			dt.Rows.Add("ALL", "Moneda 50¢", 0, 0.00m, true);
			dt.Rows.Add("ALL", "Moneda $1", 0, 80.00m, false);
			dt.Rows.Add("ALL", "Moneda $2", 0, 0.00m, false);
			dt.Rows.Add("ALL", "Moneda $5", 0, 0.00m, false);
			dt.Rows.Add("ALL", "Moneda $10", 0, 0.00m, false);
			dt.Rows.Add("ALL", "Moneda $20", 0, 0.00m, false);
			dt.Rows.Add("ALL", "Billete $20", 0, 0.00m, false);
			dt.Rows.Add("ALL", "Moneda $50", 0, 0.00m, false);
			dt.Rows.Add("ALL", "Billete $50", 0, 0.00m, false);

			return dt;
		}

		private DataTable GetSampleData2()
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("ID", typeof(int));
			dt.Columns.Add("DESCRIPTION", typeof(string));
			dt.Columns.Add("AMOUNT", typeof(decimal));

            // All rows same category = one group = one Total footer
            var i = 0;
            dt.Rows.Add(i++, "Moneda 5¢", 5.00m);
			dt.Rows.Add(i++, "Moneda 10¢", 20.00m);
			dt.Rows.Add(i++, "Moneda 20¢", 0.00m);
			dt.Rows.Add(i++, "Moneda 50¢", 0.00m);
			dt.Rows.Add(i++, "Moneda $1",  80.00m);

			return dt;
		}


		protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(null);

                string initScript = @"
                    Sys.Application.add_load(function () {
                        console.log('Sys.Application.add_load fired (registered on initial load only)');
                        ReadRows();
                    });";
                ClientScript.RegisterStartupScript(
                    GetType(), "readRowsInit", initScript, true);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            // Simulates service call + rebind on partial postback
            string filter = TextBoxFilter.Text.Trim().ToLower();
            BindGrid(string.IsNullOrEmpty(filter) ? null : filter);
        }

        private void BindGrid(string nameFilter)
        {
            var dt = GetSampleData(); // GetData();
            var column = "TEXT"; // "Name";
			if (nameFilter != null)
            {
                var filtered = dt.AsEnumerable()
                    .Where(r => r[column].ToString().ToLower().Contains(nameFilter));
                dt = filtered.Any() ? filtered.CopyToDataTable() : dt.Clone();
            }
            //C1GridView1.DataSource = dt;
            //C1GridView1.DataBind();
            FaultyControl1.DataSource = dt;
			FaultyControl1.DataBind();

			InPlaceTotalsGrid.DataSource =  GetSampleData2(); //PlazaFundService.Instance.GetInPlaceTotalAmounts(placeId, new CultureInfo(loggeduser.LanguageId));
			InPlaceTotalsGrid.DataBind();
			UpdatePanelInPlaceTotalsGrid.Update();
		}

        protected void C1GridView1_RowDataBound(object sender,
            C1.Web.Wijmo.Controls.C1GridView.C1GridViewRowEventArgs e)
        {
            if (e.Row.RowType == C1.Web.Wijmo.Controls.C1GridView.C1GridViewRowType.DataRow)
            {
                var rowData = (DataRowView)e.Row.DataItem;
                var tripJson = "{\"id\":" + rowData["Id"] +
                    ",\"name\":\"" + rowData["Name"] + "\"}";
                e.Row.Attributes["trip"] = tripJson;

                // Simulate heavier server-side rendering like  complex grid
                System.Threading.Thread.Sleep(50);
            }
        }

		protected void InPlaceTotalsGrid_DataBound(object sender, EventArgs e)
		{
			if (InPlaceTotalsGrid.Columns.Count > 0)
			{
				var col = (C1TemplateField)InPlaceTotalsGrid.Columns[0];
				col.GroupInfo.GroupSingleRow = false;
				col.GroupInfo.Position = GroupPosition.Footer;
				col.GroupInfo.FooterText = "Total:";
				col.GroupInfo.OutlineMode = OutlineMode.StartExpanded;
				col.RowMerge = RowMerge.Free;
			}
		}



	}
}