unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids,
  FormOsoba, DataModule;

type
  TFMain = class(TForm)
    DBGrid: TDBGrid;
    Panel1: TPanel;
    Shape: TShape;
    Label1: TLabel;
    btnINSERT: TSpeedButton;
    btnEDIT: TSpeedButton;
    btnDELETE: TSpeedButton;
    btnCLOSE: TBitBtn;
    procedure DBGridDblClick(Sender: TObject);
    procedure DBGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure btnDELETEClick(Sender: TObject);
    procedure btnEDITClick(Sender: TObject);
    procedure btnINSERTClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure btnCLOSEClick(Sender: TObject);

  private
	 procedure My_delete();
	 procedure My_insert();
	 procedure My_edit();

  public

  end;

var
  FMain: TFMain;

implementation
{$R *.dfm}
{------------------------------------------------------------------------------}
procedure TFMain.btnCLOSEClick(Sender: TObject);
begin
	Close();
end;

procedure TFMain.FormActivate(Sender: TObject);
begin
	Shape.Brush.Color := clRed;
   if DM.ADOConnection1.Connected then
   begin
      Shape.Brush.Color := clGreen;
   end;
end;

procedure TFMain.FormShow(Sender: TObject);
begin
   DBGrid.Align := alClient;
end;

procedure TFMain.btnINSERTClick(Sender: TObject);
begin
	My_insert();
end;

procedure TFMain.DBGridDblClick(Sender: TObject);
begin
	My_edit();
end;

procedure TFMain.DBGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
	if Key = VK_RETURN then
   begin
		My_edit();
   end;

	if Key = VK_INSERT then
   begin
		My_insert();
   end;

   if Key = VK_DELETE then
   begin
		My_delete();
   end;
end;

procedure TFMain.btnEDITClick(Sender: TObject);
begin
	My_edit();
end;

procedure TFMain.btnDELETEClick(Sender: TObject);
begin
	My_delete();
end;

procedure TFMain.My_edit();
var
	res :	TModalResult;

begin
   res := mrNone;

	(*
	if DM.ADODataSetBrowseidosoby.IsNull then
   begin
		exit;
   end;


   lub:
   *)
	// ma sens tylko gdy są jakiekolwiek rekordy
	if DM.ADODataSetBrowse.RecordCount = 0 then
	begin
   	exit;
	end;
 
	FOsoba := TFOsoba.Create(Application, true);
	try
		res := FOsoba.ShowModal();
		if res = mrOk then
		begin
			// empty
		end;
	finally
   	FreeAndNil(FOsoba);

		DBGrid.SetFocus();
	end;
end;

procedure TFMain.My_insert();
var
	res : TModalResult;

begin
	res := mrNone;
	
	FOsoba := TFOsoba.Create(Application, false);
	try
		res := FOsoba.ShowModal();
		if res = mrOk then
		begin
			// empty
		end;
	finally
   	FreeAndNil(FOsoba);
		DBGrid.SetFocus();
	end;
end;

procedure TFMain.My_delete();
var
	id  : Integer;
   res : Integer;

begin
	if DM.ADODataSetBrowse.RecordCount = 0 then
	begin
		// ma sens jeżeli istnieją rekordy
   	exit;
	end;

	res := 0;
	res := MessageBox(0, 'Usunąć rekord', 'Ostrzeżenie', MB_YESNO);

	if res  = IDYES then
	begin
		// odczytuję dane aktualnego rekordu
		id := DM.ADODataSetBrowse.FieldByName('IDOSOBY').AsInteger;

		try
			DM.adocDELETE.Parameters.ParamByName('IDOSOBY').Value := id;
			DM.adocDELETE.Execute();
		finally
			DM.ADODataSetBrowse.Close();
			DM.ADODataSetBrowse.Open();
			DBGrid.SetFocus();
		end;
	end;
end;


end.
