unit View.AddOrder2;

interface

uses
  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
  WEBLib.Forms, WEBLib.Dialogs, WEBLib.StdCtrls, XData.Web.Client, WEBLib.ExtCtrls,
  Data.DB, XData.Web.Dataset, VCL.TMSFNCTypes, VCL.TMSFNCGrid, VCL.TMSFNCGridData,
  VCL.TMSFNCUtils, VCL.TMSFNCGraphics, VCL.TMSFNCGraphicsTypes,
  VCL.TMSFNCGridCell, VCL.TMSFNCGridOptions, VCL.TMSFNCCustomComponent,
  VCL.TMSFNCCustomGrid, VCL.TMSFNCGridDatabaseAdapter, VCL.TMSFNCCustomControl,
  VCL.TMSFNCCustomScrollControl, WEBLib.DB, XData.Web.JsonDataset, Vcl.StdCtrls,
  Vcl.Controls, ConnectionModule;

type
  TFAddOrder = class(TWebForm)
    edtSearch: TWebEdit;
    edtID: TWebEdit;
    btnCancel: TWebButton;
    XDataWebClient1: TXDataWebClient;
    wdsCustomers: TWebDataSource;
    xdwdsCustomers: TXDataWebDataSet;
    xdwdsCustomersID: TIntegerField;
    xdwdsCustomersNAME: TStringField;
    xdwdsCustomersSHORT_NAME: TStringField;
    btnConfirm: TWebButton;
    cbCorrugatedPlate: TWebCheckBox;
    cbWebPlate: TWebCheckBox;
    TMSFNCGrid1: TTMSFNCGrid;
    procedure WebFormShow(Sender: TObject);
    procedure cbCorrugatedPlateClick(Sender: TObject);
    procedure cbWebPlateClick(Sender: TObject);
    procedure btnConfirmClick(Sender: TObject);
    procedure edtSearchChange(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure TMSFNCGrid1SelectedCell(Sender: TObject; ACol, ARow: Integer);
  private
    [async] procedure getCustomers();
    procedure PopulateGridManually;
    procedure ApplyFilter;
  public
  end;

var
  FAddOrder: TFAddOrder;

implementation

{$R *.dfm}

procedure TFAddOrder.WebFormShow(Sender: TObject);
begin
  getCustomers(); // Fetch and populate the grid with customer data
end;

[async]
procedure TFAddOrder.getCustomers();
var
  xdcResponse: TXDataClientResponse;
  customerList: TJSObject;
begin
  // Fetch data from XData service
  xdcResponse := await(XDataWebClient1.RawInvokeAsync('ILookupService.GetCustomers', []));
  customerList := TJSObject(xdcResponse.Result);

  // Load data into TXDataWebDataset
  xdwdsCustomers.Close;
  xdwdsCustomers.SetJsonData(customerList['data']);
  xdwdsCustomers.Open;

  // Manually populate the grid
  PopulateGridManually;
end;

procedure TFAddOrder.PopulateGridManually;
var
  RowIndex: Integer;
begin
  TMSFNCGrid1.BeginUpdate;
  try
    TMSFNCGrid1.Clear; // Clear any existing data

    // Set up column headers
    TMSFNCGrid1.ColumnCount := 4;
    TMSFNCGrid1.RowCount := 1;
    TMSFNCGrid1.Cells[0, 0] := 'ID';
    TMSFNCGrid1.Cells[1, 0] := 'Short Name';
    TMSFNCGrid1.Cells[2, 0] := 'Name';

//    TMSFNCGrid1.ColumnWidths[0] := 40;
//    TMSFNCGrid1.ColumnWidths[1] := 80;
//    TMSFNCGrid1.ColumnWidths[2] := 250;

    // Populate the grid with data from the dataset
    xdwdsCustomers.First;
    RowIndex := 1;

    while not xdwdsCustomers.EOF do
    begin
      TMSFNCGrid1.RowCount := RowIndex + 1;

      TMSFNCGrid1.Cells[0, RowIndex] := xdwdsCustomers.FieldByName('ID').AsString;
      TMSFNCGrid1.Cells[1, RowIndex] := xdwdsCustomers.FieldByName('SHORT_NAME').AsString;
      TMSFNCGrid1.Cells[2, RowIndex] := xdwdsCustomers.FieldByName('NAME').AsString;

      Inc(RowIndex);
      xdwdsCustomers.Next;
    end;

  finally
    TMSFNCGrid1.EndUpdate;
  end;
end;

procedure TFAddOrder.TMSFNCGrid1SelectedCell(Sender: TObject; ACol,
  ARow: Integer);
begin
  edtID.Text := TMSFNCGrid1.Cells[0, ARow];
end;

procedure TFAddOrder.ApplyFilter;
var
  fd: TTMSFNCGridFilterData;
  i: Integer;
  SearchText: string;
begin
  SearchText := Trim(edtSearch.Text);

  TMSFNCGrid1.RemoveFilter;
  TMSFNCGrid1.Filter.Clear;

  // match on first 3 columns
  for i := 0 to 2 do
  begin
    fd := TMSFNCGrid1.Filter.Add;
    fd.Column := i;
    fd.Condition := '*' + SearchText + '*'; // Match text anywhere in the cell
    fd.CaseSensitive := False; // Make the filter case-insensitive

    // Use foOR for "match any column" logic
    if i > 0 then
      fd.Operation := foOR
    else
      fd.Operation := foNONE; // First filter has no logical operation
  end;

  // Apply the filters to the grid
  TMSFNCGrid1.ApplyFilter;
end;


procedure TFAddOrder.cbCorrugatedPlateClick(Sender: TObject);
begin
  cbWebPlate.Checked := False;
end;

procedure TFAddOrder.cbWebPlateClick(Sender: TObject);
begin
  cbCorrugatedPlate.Checked := False;
end;

procedure TFAddOrder.btnConfirmClick(Sender: TObject);
begin
  Close;
end;

procedure TFAddOrder.btnCancelClick(Sender: TObject);
begin
  edtID.Text := '';
  Close;
end;

procedure TFAddOrder.edtSearchChange(Sender: TObject);
begin
  ApplyFilter;
end;

end.




//unit View.AddOrder;
//
//interface
//
//uses
//  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
//  WEBLib.Forms, WEBLib.Dialogs, WEBLib.StdCtrls, XData.Web.Client, WEBLib.ExtCtrls,
//  Data.DB, XData.Web.Dataset, VCL.TMSFNCTypes, VCL.TMSFNCGrid, VCL.TMSFNCGridData,
//  VCL.TMSFNCUtils, VCL.TMSFNCGraphics, VCL.TMSFNCGraphicsTypes,
//  VCL.TMSFNCGridCell, VCL.TMSFNCGridOptions, VCL.TMSFNCCustomComponent,
//  VCL.TMSFNCCustomGrid, VCL.TMSFNCGridDatabaseAdapter, VCL.TMSFNCCustomControl,
//  VCL.TMSFNCCustomScrollControl, WEBLib.DB, XData.Web.JsonDataset, Vcl.StdCtrls,
//  Vcl.Controls, ConnectionModule, WEBLib.WebCtrls;
//
//type
//  TFAddOrder = class(TWebForm)
//    XDataWebClient1: TXDataWebClient;
//    wdsCustomers: TWebDataSource;
//    xdwdsCustomers: TXDataWebDataSet;
//    xdwdsCustomersID: TIntegerField;
//    xdwdsCustomersNAME: TStringField;
//    xdwdsCustomersSHORT_NAME: TStringField;
//    xdwdsCustomersADDRESS: TStringField;
//    cbWebPlate: TWebCheckBox;
//    cbCorrugatedPlate: TWebCheckBox;
//    edtSearch: TWebEdit;
//    edtID: TWebEdit;
//    btnConfirm: TWebButton;
//    btnCancel: TWebButton;
//    TMSFNCGrid1: TTMSFNCGrid;
//    procedure cbCorrugatedPlateClick(Sender: TObject);
//    procedure cbWebPlateClick(Sender: TObject);
//    procedure btnConfirmClick(Sender: TObject);
//    procedure btnCancelClick(Sender: TObject);
//    procedure TMSFNCGrid1SelectedCell(Sender: TObject; ACol, ARow: Integer);
//    procedure edtSearchChange(Sender: TObject);
//  private
//    [async] procedure GetCustomers();
//    procedure PopulateGridManually;
//    procedure ApplyFilter;
//  public
//  class function CreateForm(AElementID: string): TWebForm;
//  end;
//
//var
//  FAddOrder: TFAddOrder;
//
//implementation
//
//{$R *.dfm}
//
//
//class function TFAddOrder.CreateForm(AElementID: string): TWebForm;
//begin
//  Application.CreateForm(TFAddOrder, AElementID, Result,
//    procedure(AForm: TObject)
//    begin
//      with TFAddOrder(AForm) do
//      begin
//        GetCustomers();
//        Console.Log('AddOrder form created and dynamic styles applied.');
//      end;
//    end
//  );
//end;
//
//
//
//procedure TFAddOrder.edtSearchChange(Sender: TObject);
//begin
//  ApplyFilter;
//end;
//
//
//[async] procedure TFAddOrder.GetCustomers();
//var
//  xdcResponse: TXDataClientResponse;
//  customerList: TJSObject;
//begin
//  // Fetch data from XData service
//  xdcResponse := await(XDataWebClient1.RawInvokeAsync('ILookupService.GetCustomers', []));
//  customerList := TJSObject(xdcResponse.Result);
//
//  // Load data into TXDataWebDataset
//  xdwdsCustomers.Close;
//  xdwdsCustomers.SetJsonData(customerList['data']);
//  xdwdsCustomers.Open;
//
//  // Manually populate the grid
//  PopulateGridManually;
//end;
//
//
//procedure TFAddOrder.PopulateGridManually;
//var
//  RowIndex: Integer;
//begin
//  TMSFNCGrid1.BeginUpdate;
//  try
//    TMSFNCGrid1.Clear; // Clear any existing data
//
//    // Set up column headers
//    TMSFNCGrid1.ColumnCount := 3;
//    TMSFNCGrid1.RowCount := 1;
//    TMSFNCGrid1.Cells[0, 0] := 'ID';
//    TMSFNCGrid1.Cells[1, 0] := 'Short Name';
//    TMSFNCGrid1.Cells[2, 0] := 'Name';
//
////    TMSFNCGrid1.ColumnWidths[0] := 40;
////    TMSFNCGrid1.ColumnWidths[1] := 80;
////    TMSFNCGrid1.ColumnWidths[2] := 250;
//
//    // Populate the grid with data from the dataset
//    xdwdsCustomers.First;
//    RowIndex := 1;
//
//    while not xdwdsCustomers.EOF do
//    begin
//      TMSFNCGrid1.RowCount := RowIndex + 1;
//
//      TMSFNCGrid1.Cells[0, RowIndex] := xdwdsCustomers.FieldByName('ID').AsString;
//      TMSFNCGrid1.Cells[1, RowIndex] := xdwdsCustomers.FieldByName('SHORT_NAME').AsString;
//      TMSFNCGrid1.Cells[2, RowIndex] := xdwdsCustomers.FieldByName('NAME').AsString;
//
//      Inc(RowIndex);
//      xdwdsCustomers.Next;
//    end;
//
//  finally
//    TMSFNCGrid1.EndUpdate;
//  end;
//end;
//
//
//procedure TFAddOrder.TMSFNCGrid1SelectedCell(Sender: TObject; ACol,
//  ARow: Integer);
//begin
//  edtID.Text := TMSFNCGrid1.Cells[0, ARow];
//end;
//
//
//procedure TFAddOrder.ApplyFilter;
//var
//  fd: TTMSFNCGridFilterData;
//  i: Integer;
//  SearchText: string;
//begin
//  SearchText := Trim(edtSearch.Text);
//  Console.Log('Filter Applied: ' + SearchText);
//
//  TMSFNCGrid1.RemoveFilter;
//  TMSFNCGrid1.Filter.Clear;
//
//  for i := 0 to 2 do
//  begin
//    fd := TMSFNCGrid1.Filter.Add;
//    fd.Column := i;
//    fd.Condition := '*' + SearchText + '*'; // Match text anywhere in the cell
//    fd.CaseSensitive := False;
//
//    if i > 0 then
//      fd.Operation := foOR
//    else
//      fd.Operation := foNONE;
//  end;
//
//  TMSFNCGrid1.ApplyFilter;
//end;
//
//
//procedure TFAddOrder.cbCorrugatedPlateClick(Sender: TObject);
//begin
//  cbWebPlate.Checked := False;
//end;
//
//
//procedure TFAddOrder.cbWebPlateClick(Sender: TObject);
//begin
//  cbCorrugatedPlate.Checked := False;
//end;
//
//procedure TFAddOrder.btnConfirmClick(Sender: TObject);
//begin
//  edtID.Text := '';
//  window.location.reload(true);
//end;
//
//
//procedure TFAddOrder.btnCancelClick(Sender: TObject);
//var
//  OrdersPanel: TJSHTMLElement;
//begin
//  edtID.Text := '';
//
//  // Get the panel element by its ID
//  OrdersPanel := TJSHTMLElement(document.getElementById('pnl_orders'));
//
//  // Hide the panel
//  if Assigned(OrdersPanel) then
//  begin
//    OrdersPanel.style.setProperty('display', 'none');
//
//    // Remove all child nodes of the container to clear the DOM
//    while OrdersPanel.firstChild <> nil do
//      OrdersPanel.removeChild(OrdersPanel.firstChild);
//  end;
//
//  // Free the current form instance to clean up resources
//  Free;
//
//  Console.Log('Panel hidden, DOM cleared, and form instance destroyed.');
//end;
//
//
//
//
//
//end.

