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;
    edtName: TWebEdit;
    TMSFNCGrid1: TTMSFNCGrid;
    btnCancel: TWebButton;
    btnConfirm: TWebButton;
    edtNotification: TWebEdit;
    XDataWebClient1: TXDataWebClient;
    xdwdsCustomers: TXDataWebDataSet;
    wdsCustomers: TWebDataSource;
    xdwdsCustomersBillAddr: TStringField;
    xdwdsCustomersCompanyName: TStringField;
    xdwdsCustomersId: TStringField;
    xdwdsCustomersPrimaryPhone: TStringField;
    xdwdsCustomersShipAddr: TStringField;
    xdwdsCustomersShipAddrLine1: TStringField;
    xdwdsCustomersShipAddrCity: TStringField;
    xdwdsCustomersShipAddrState: TStringField;
    xdwdsCustomersShipAddrZip: TStringField;
    xdwdsCustomersBillAddrLine1: TStringField;
    xdwdsCustomersBillAddrCity: TStringField;
    xdwdsCustomersBillAddrState: TStringField;
    xdwdsCustomersBillAddrZip: TStringField;
    WebLabel3: TWebLabel;
    edtID: TWebEdit;
    xdwdsCustomersInKGOrders: TBooleanField;
    procedure WebFormCreate(Sender: TObject);
    procedure WebFormShow(Sender: TObject);
    procedure TMSFNCGrid1CellDblClick(Sender: TObject; ACol, ARow: Integer);
    procedure btnConfirmClick(Sender: TObject);
    procedure TMSFNCGrid1CellClick(Sender: TObject; ACol, ARow: Integer);
  private
    { Private declarations }
    [Async] procedure GetCustomers();
    [Async] procedure SendCustomerToServer();
    procedure PopulateGridManually();
  public
    { Public declarations }
  end;

var
  FSelectCustomer: TFSelectCustomer;

implementation

{$R *.dfm}

uses View.Main, Utils;

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
  Utils.ShowSpinner('spinner');
  getCustomers();
end;

procedure TFSelectCustomer.btnConfirmClick(Sender: TObject);
begin
  if edtID.Text = '' then
    edtNotification.Text := 'Please Select a Customer'
  else
  begin
    xdwdsCustomers.Locate('Id', edtID.Text, []);
    SendCustomerToServer();
  end;
end;

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

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

    // Manually populate the grid
    PopulateGridManually;
  except
    on E: EXDataClientRequestException do
      Utils.ShowErrorModal('Could not retrieve QuickBooks customers: ' + E.ErrorResult.ErrorMessage);
  end;
  Utils.HideSpinner('spinner');
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 Name';
    TMSFNCGrid1.Cells[2, 0] := 'Address';
    TMSFNCGrid1.Cells[3, 0] := 'In KGOrders';

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

  finally
    TMSFNCGrid1.EndUpdate;
  end;
end;

procedure TFSelectCustomer.TMSFNCGrid1CellClick(Sender: TObject; ACol,
  ARow: Integer);
begin
  edtID.Text := TMSFNCGrid1.Cells[0, ARow];
  edtName.Text := TMSFNCGrid1.Cells[1, ARow];
end;

procedure TFSelectCustomer.TMSFNCGrid1CellDblClick(Sender: TObject; ACol,
  ARow: Integer);
begin
  edtID.Text := TMSFNCGrid1.Cells[2, ARow];
  xdwdsCustomers.Locate('Id', TMSFNCGrid1.Cells[0, ARow], [] );
end;

procedure TFSelectCustomer.SendCustomerToServer;
var
  CustomerJSON: TJSONObject;
  Response: TXDataClientResponse;
  notification: TJSObject;
begin
  CustomerJSON := TJSONObject.Create;
  CustomerJSON.AddPair('NAME', xdwdsCustomers.FieldByName('CompanyName').AsString);
  CustomerJSON.AddPair('QB_LIST_ID', xdwdsCustomers.FieldByName('Id').AsString);
  CustomerJSON.AddPair('PHONE', xdwdsCustomers.FieldByName('PrimaryPhone').AsString);
  CustomerJSON.AddPair('BILL_ADDRESS', xdwdsCustomers.FieldByName('BillAddrLine1').AsString);
  CustomerJSON.AddPair('BILL_CITY', xdwdsCustomers.FieldByName('BillAddrCity').AsString);
  CustomerJSON.AddPair('BILL_STATE', xdwdsCustomers.FieldByName('BillAddrState').AsString);
  CustomerJSON.AddPair('BILL_ZIP', xdwdsCustomers.FieldByName('BillAddrZip').AsString);
  CustomerJSON.AddPair('BILL_ADDRESS_BLOCK', xdwdsCustomers.FieldByName('BillAddr').AsString);

  CustomerJSON.AddPair('address', xdwdsCustomers.FieldByName('ShipAddrLine1').AsString);
  CustomerJSON.AddPair('city', xdwdsCustomers.FieldByName('ShipAddrCity').AsString);
  CustomerJSON.AddPair('state', xdwdsCustomers.FieldByName('ShipAddrState').AsString);
  CustomerJSON.AddPair('zip', xdwdsCustomers.FieldByName('ShipAddrZip').AsString);
  CustomerJSON.AddPair('ship_block', xdwdsCustomers.FieldByName('ShipAddr').AsString);
  CustomerJSON.AddPair('name', xdwdsCustomers.FieldByName('CompanyName').AsString);
  CustomerJSON.AddPair('mode', 'ADD');

  Utils.ShowSpinner('spinner');

  Response := await(XDataWebClient1.RawInvokeAsync('ILookupService.ImportQBCustomer',
              [customerJSON.ToString]));

  notification := TJSObject(Response.Result);

  Utils.HideSpinner('spinner');

  FViewMain.ViewAddCustomer(string(notification['CustomerID']), string(notification['status']));
  Close();

end;

end.