unit View.SelectCustomer;

interface

uses
  System.SysUtils, System.Generics.Collections, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
  WEBLib.Forms, WEBLib.Dialogs, WEBLib.Menus, WEBLib.ExtCtrls, WEBLib.StdCtrls,
  WEBLib.JSON, Auth.Service, XData.Web.Client, WebLib.Storage,
  ConnectionModule, App.Types, Vcl.StdCtrls, Vcl.Controls, WEBLib.DBCtrls,
  Data.DB, XData.Web.JsonDataset, XData.Web.Dataset, WEBLib.DB, VCL.TMSFNCTypes, VCL.TMSFNCUtils,
  VCL.TMSFNCGraphics, VCL.TMSFNCGraphicsTypes, VCL.TMSFNCGridCell,
  VCL.TMSFNCGridOptions, VCL.TMSFNCCustomControl, VCL.TMSFNCCustomScrollControl,
  VCL.TMSFNCGridData, VCL.TMSFNCCustomGrid, VCL.TMSFNCGrid;

type
  TFSelectCustomer = class(TWebForm)
    WebLabel1: TWebLabel;
    WebLabel2: TWebLabel;
    edtSearch: TWebEdit;
    edtID: TWebEdit;
    TMSFNCGrid1: TTMSFNCGrid;
    cbCorrugatedPlate: TWebCheckBox;
    cbWebPlate: TWebCheckBox;
    btnCancel: TWebButton;
    btnConfirm: TWebButton;
    cbCuttingDie: TWebCheckBox;
    edtNotification: TWebEdit;
    XDataWebClient1: TXDataWebClient;
    xdwdsCustomers: TXDataWebDataSet;
    wdsCustomers: TWebDataSource;
    xdwdsCustomersBillAddr: TStringField;
    xdwdsCustomersCompanyName: TStringField;
    xdwdsCustomersId: TStringField;
    xdwdsCustomersPrimaryPhone: TStringField;
    xdwdsCustomersShipAddr: TStringField;
    procedure WebFormCreate(Sender: TObject);
    procedure WebFormShow(Sender: TObject);
  private
    { Private declarations }
    [Async] procedure GetCustomers();
    procedure PopulateGridManually();
  public
    { Public declarations }
  end;

var
  FSelectCustomer: TFSelectCustomer;

implementation

{$R *.dfm
}

procedure TFSelectCustomer.WebFormCreate(Sender: TObject);
begin
  if not DMConnection.ApiConnection.Connected then
   begin
    DMConnection.ApiConnection.OpenAsync;
    console.log('report requirements connection open')
   end;
end;

procedure TFSelectCustomer.WebFormShow(Sender: TObject);
begin
  getCustomers();
end;

[async] procedure TFSelectCustomer.getCustomers();
// retrieves customer list from server
var
  xdcResponse: TXDataClientResponse;
  customerList: TJSObject;
  i: integer;
begin
  // Fetch data from XData service
  xdcResponse := await(XDataWebClient1.RawInvokeAsync('ILookupService.getQBCustomers', []));
  customerList := TJSObject(xdcResponse.Result);
  //console.log(customerList);

  // Load data into TXDataWebDataset

  xdwdsCustomers.Close;
  xdwdsCustomers.SetJsonData(customerList);
  xdwdsCustomers.Open;
  console.log(customerList);

  // Manually populate the grid
  PopulateGridManually;
end;

procedure TFSelectCustomer.PopulateGridManually;
// populates the grid with customers manually.
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] := 'Quickbooks ID';
    TMSFNCGrid1.Cells[1, 0] := 'Customer ID';
    TMSFNCGrid1.Cells[2, 0] := 'Customer Name';
    TMSFNCGrid1.Cells[3, 0] := 'Address';

    // 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('CompanyName').AsString;
      TMSFNCGrid1.Cells[3, RowIndex] := xdwdsCustomers.FieldByName('BillAddr').AsString;
      Inc(RowIndex);
      xdwdsCustomers.Next;
    end;

  finally
    TMSFNCGrid1.EndUpdate;
  end;
end;

end.