// Search pop-up form used to filter the list.
// Author: Cameron Hayes
unit View.Search;

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, WEBLib.Grids,
  Vcl.Grids, VCL.TMSFNCTypes, VCL.TMSFNCUtils, VCL.TMSFNCGraphics,
  VCL.TMSFNCGraphicsTypes, VCL.TMSFNCGridCell, VCL.TMSFNCGridOptions,
  VCL.TMSFNCCustomControl, VCL.TMSFNCCustomScrollControl, VCL.TMSFNCGridData,
  VCL.TMSFNCCustomGrid, VCL.TMSFNCGrid;

type
  TFSearch = class(TWebForm)
    pnlMessage: TWebPanel;
    lblMessage: TWebLabel;
    btnCloseNotification: TWebButton;
    wcbFilterType1: TWebComboBox;
    btnConfirm: TWebButton;
    edtOrderID: TWebEdit;
    dtpStartDate1: TWebDateTimePicker;
    dtpEndDate1: TWebDateTimePicker;
    cbNull1: TWebCheckBox;
    wcbOrderType: TWebComboBox;
    lblStatus1: TWebLabel;
    lblType1: TWebLabel;
    lblStartDate1: TWebLabel;
    lblEndDate1: TWebLabel;
    wcbFilterType2: TWebComboBox;
    dtpStartDate2: TWebDateTimePicker;
    dtpEndDate2: TWebDateTimePicker;
    cbNull2: TWebCheckBox;
    lblStatus2: TWebLabel;
    lblType2: TWebLabel;
    lblStartDate2: TWebLabel;
    lblEndDate2: TWebLabel;
    WebLabel1: TWebLabel;
    WebLabel2: TWebLabel;
    TMSFNCGrid1: TTMSFNCGrid;
    edtSearch: TWebEdit;
    edtCompanyID: TWebEdit;
    lblCompanyID: TWebLabel;
    XDataWebClient1: TXDataWebClient;
    xdwdsCustomers: TXDataWebDataSet;
    xdwdsCustomersID: TIntegerField;
    xdwdsCustomersNAME: TStringField;
    xdwdsCustomersSHORT_NAME: TStringField;
    wdsCustomers: TWebDataSource;
    WebLabel3: TWebLabel;
    edtJobName: TWebEdit;
    WebLabel4: TWebLabel;
    btnCancel: TWebButton;
    btnClear: TWebButton;
    xdwdsCustomersstaff_fields_invoice_to: TStringField;
    WebLabel5: TWebLabel;
    edtCompanyName: TWebEdit;
    procedure btnConfirmClick(Sender: TObject);
    procedure WebFormShow(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure edtSearchChange(Sender: TObject);
    procedure TMSFNCGrid1CellClick(Sender: TObject; ACol, ARow: Integer);
    procedure btnClearClick(Sender: TObject);
    procedure cbNull1Click(Sender: TObject);
    procedure cbNull2Click(Sender: TObject);
  private
    //FJSONProc: TJSONProc;
    [async] procedure getCustomers;
    procedure PopulateGridManually;
    procedure ApplyFilter;
    var
      temp: string;
  public
    class function CreateForm(AElementID: string): TWebForm;
    var
      confirm: boolean;
      searchOptions: string;
  end;

var
  FSearch: TFSearch;

implementation

{$R *.dfm}

class function TFSearch.CreateForm(AElementID: string): TWebForm;
begin
  Application.CreateForm(TFSearch, AElementID, Result,
    procedure(AForm: TObject)
    begin
      with TFSearch(AForm) do
      begin

      end;
    end
  );
end;

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

procedure TFSearch.WebFormShow(Sender: TObject);
// Auto fills information based on previous search
var
  params: TStringList;
  DateFormatSettings: TFormatSettings;
begin
  params := TStringList.Create;
  params.StrictDelimiter := true;
  params.Delimiter := '&';
  params.DelimitedText := searchOptions;
  confirm := false;

  DateFormatSettings := TFormatSettings.Create;
  DateFormatSettings.ShortDateFormat := 'yyyy/mm/dd';

  wcbOrderType.Text := UpperCase(Copy(params.Values['orderType'], 1, 1)) + LowerCase(Copy(params.Values['orderType'], 2, MaxInt));
  edtOrderID.Text := params.Values['orderID'];
  edtCompanyID.Text := params.Values['companyID'];
  edtJobName.Text := params.Values['jobName'];

  // Status 1
  if params.Values['filterType1'] <> '' then
    wcbFilterType1.Text := params.Values['filterType1']
  else
    wcbFilterType1.Text := 'NONE';
  console.log(params.Values['startDate1']);

  if params.Values['startDate1'] = '' then
    dtpStartDate1.Date := 0
  else
  begin
    dtpStartDate1.Date := StrToDateTime(params.Values['startDate1'], DateFormatSettings);
  end;

  if params.Values['endDate1'] = '' then
    dtpEndDate1.Date := 0
  else
    dtpEndDate1.Date := StrToDateTime(params.Values['endDate1'], DateFormatSettings);

  if params.values['null1'] <> '' then
  begin
    cbNull1.Checked := StrToBool(params.Values['null1']);
    if StrToBool(params.Values['null1']) then
    begin
      dtpStartDate1.Visible := false;
      dtpEndDate1.Visible := false;
    end;
  end;

  // Status 2
  if params.Values['filterType2'] <> '' then
    wcbFilterType2.Text := params.Values['filterType2']
  else
    wcbFilterType2.Text := 'NONE';

  console.log(params.Values['startDate2']);
  if params.Values['startDate2'] = '' then
    dtpStartDate2.Date := 0
  else
    dtpStartDate2.Date := StrToDateTime(params.Values['startDate2'], DateFormatSettings);

  if params.Values['endDate2'] = '' then
    dtpEndDate2.Date := 0
  else
    dtpEndDate2.Date := StrToDateTime(params.Values['endDate2'], DateFormatSettings);

  if params.values['null2'] <> '' then
  begin
    cbNull1.Checked := StrToBool(params.Values['null2']);
    if StrToBool(params.Values['null2']) then
    begin
      dtpStartDate2.Visible := false;
      dtpEndDate2.Visible := false;
    end;
  end;


  params.Free;
  getCustomers();
end;

procedure TFSearch.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TFSearch.btnClearClick(Sender: TObject);
begin
  edtOrderID.Text := '';
  edtCompanyID.Text := '';
  edtSearch.Text := '';
  edtJobName.Text := '';
  wcbOrderType.Text := 'Any';
  wcbFilterType1.Text := 'NONE';
  dtpStartDate1.Date := 0;
  dtpEndDate1.Date := 0;
  cbNull1.Checked := False;
  wcbFilterType2.Text := 'NONE';
  dtpStartDate2.Date := 0;
  dtpEndDate2.Date := 0;
  cbNull2.Checked := false;
end;

procedure TFSearch.btnConfirmClick(Sender: TObject);
begin
  confirm := true;
  Close;
end;

procedure TFSearch.cbNull1Click(Sender: TObject);
begin
  if cbNull1.Checked then
  begin
    dtpStartDate1.Visible := false;
    dtpEndDate1.Visible := false;
  end
  else
  begin
    dtpStartDate1.Visible := true;
    dtpEndDate1.Visible := true;
  end;
end;

procedure TFSearch.cbNull2Click(Sender: TObject);
begin
  if cbNull2.Checked then
  begin
    dtpStartDate2.Visible := false;
    dtpEndDate2.Visible := false;
  end
  else
  begin
    dtpStartDate2.Visible := true;
    dtpEndDate2.Visible := true;
  end;
end;

[async] procedure TFSearch.getCustomers();
// Gets a list of customers from the server
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 TFSearch.TMSFNCGrid1CellClick(Sender: TObject; ACol, ARow: Integer);
begin
  edtCompanyID.Text := TMSFNCGrid1.Cells[0, ARow];
  edtCompanyName.Text := TMSFNCGrid1.Cells[2, ARow];
end;

procedure TFSearch.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.Cells[3, 0] := 'Address';

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

      Inc(RowIndex);
      xdwdsCustomers.Next;
    end;

  finally
    TMSFNCGrid1.EndUpdate;
  end;
end;

procedure TFSearch.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;

end.

