unit uFlatsFrame;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.Grids, Vcl.DBGrids,
  Vcl.ExtCtrls, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error,
  FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
  FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef,
  FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
  FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Comp.Client, Vcl.DBCtrls, Vcl.StdCtrls;

type
  TFlatsFrame = class(TFrame)
    pTop: TPanel;
    DBGrid: TDBGrid;
    FDConnectionTemp: TFDConnection;
    DSFlats: TDataSource;
    qFlats: TFDQuery;
    cbFlatNumbers: TDBLookupComboBox;
    cbStreets: TDBLookupComboBox;
    cbHouseNumbers: TDBLookupComboBox;
    bFilter: TButton;
    bFlushFilter: TButton;
    qStreets: TFDQuery;
    DSStreets: TDataSource;
    qHouseNumbers: TFDQuery;
    DSHouseNumbers: TDataSource;
    qFlatNumbers: TFDQuery;
    DSFlatNumbers: TDataSource;
    pPanelTop: TPanel;
    procedure cbStreetsCloseUp(Sender: TObject);
    procedure cbHouseNumbersCloseUp(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Init();
  end;

implementation

uses
  uDb;

{$R *.dfm}

{ TFlatsFrame }

procedure TFlatsFrame.cbHouseNumbersCloseUp(Sender: TObject);
begin
  with qFlatNumbers do
  begin
    Connection := Db.FDConnection;
    CachedUpdates := true;
    params.ParamValues['houseNumbers_id'] := qHouseNumbers.FieldByName('id').AsInteger;
    Disconnect();
    Open();
    cbFlatNumbers.KeyValue := qFlatNumbers.FieldByName('id').AsInteger;
  end;
end;

procedure TFlatsFrame.cbStreetsCloseUp(Sender: TObject);
begin
  with qHouseNumbers do
  begin
    Connection := Db.FDConnection;
    CachedUpdates := true;
    params.ParamValues['streets_id'] := qStreets.FieldByName('id').AsInteger;
    Disconnect();
    Open();
    cbHouseNumbers.KeyValue := qHouseNumbers.FieldByName('id').AsInteger;
  end;
  with qFlatNumbers do
  begin
    Connection := Db.FDConnection;
    CachedUpdates := true;
    params.ParamValues['houseNumbers_id'] := qHouseNumbers.FieldByName('id').AsInteger;
    Disconnect();
    Open();
    cbFlatNumbers.KeyValue := qFlatNumbers.FieldByName('id').AsInteger;
  end;
end;

procedure TFlatsFrame.Init;
begin
  FDConnectionTemp.Connected := false;
  with qFlats do
  begin
    Connection := Db.FDConnection;
    Disconnect();
    Open();
  end;
  with qStreets do
  begin
    Connection := Db.FDConnection;
    CachedUpdates := true;
    Disconnect();
    Open();
    cbStreets.KeyValue := qStreets.FieldByName('id').AsInteger;
  end;
  with qHouseNumbers do
  begin
    Connection := Db.FDConnection;
    CachedUpdates := true;
    params.ParamValues['streets_id'] := qStreets.FieldByName('id').AsInteger;
    Disconnect();
    Open();
    cbHouseNumbers.KeyValue := qHouseNumbers.FieldByName('id').AsInteger;
  end;
  with qFlatNumbers do
  begin
    Connection := Db.FDConnection;
    CachedUpdates := true;
    params.ParamValues['houseNumbers_id'] := qHouseNumbers.FieldByName('id').AsInteger;
    Disconnect();
    Open();
    cbFlatNumbers.KeyValue := qFlatNumbers.FieldByName('id').AsInteger;
  end;
end;

end.

