unit Main;

{$mode objfpc}{$H+}

interface

uses
  {$IFNDEF Linux}
  Windows, ShellApi, Messages,
  {$ELSE}
  LCLType, LCLIntf, LMessages, LazHelper,
  {$ENDIF}
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  Menus, frxServer, frxBarcode, frxDCtrl, frxCross, frxChBox, frxChartLaz, frxClass,
  Db, frxDesgn, ImgList, ComCtrls, frxGradient, frxServerUtils, IniFiles,
  frxDBSet, frxServerStat, frxServerConfig, frxTableObject, frxlazdbfcomp, dbf;

type

  { TForm1 }

  TForm1 = class(TForm)
    Close1: TMenuItem;
    CloseBtn: TButton;
    DesignBtn: TButton;
    ECurrentReports: TEdit;
    ECurrentSessions: TEdit;
    EErrors: TEdit;
    EMaxReports: TEdit;
    EMaxSessions: TEdit;
    ETotalReports: TEdit;
    ETotalSessions: TEdit;
    frBarCodeObject1: TfrxBarCodeObject;
    frChartObject1: TfrxChartObject;
    frCrossObject1: TfrxCrossObject;
    frDialogControls1: TfrxDialogControls;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxDesigner1: TfrxDesigner;
    frxReportTableObject1: TfrxReportTableObject;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Image1: TImage;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    LBActive: TLabel;
    LBInactive: TLabel;
    MinimizeBtn: TButton;
    N1: TMenuItem;
    N2: TMenuItem;
    OpenDialog1: TOpenDialog;
    PopupMenu1: TPopupMenu;
    Serv: TfrxReportServer;
    ShowWin: TMenuItem;
    Start1: TMenuItem;
    StartBtn: TButton;
    Stop1: TMenuItem;
    StopBtn: TButton;
    Timer1: TTimer;
    Uptime: TLabel;
    URL: TButton;
    procedure CloseBtnClick(Sender: TObject);
    procedure DesignBtnClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Label3Click(Sender: TObject);
    procedure MinimizeBtnClick(Sender: TObject);
    procedure StartBtnClick(Sender: TObject);
    procedure StopBtnClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure URLClick(Sender: TObject);
  private
    frReport1: TfrxReport;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  rePadding(Form1);
  Label13.Caption := #194#174;
  StartBtnClick(Sender);
  URL.Caption := 'http://localhost:' + ServerConfig.GetValue('server.http.port');
end;

procedure TForm1.DesignBtnClick(Sender: TObject);
begin
  OpenDialog1.InitialDir := frxGetAbsPathDir(ServerConfig.GetValue('server.reports.path'), ServerConfig.ConfigFolder); //Serv.Configuration.ReportPath;
  if OpenDialog1.Execute then
  begin
    try
      frReport1 := TfrxReport.Create(nil);
      frReport1.LoadFromFile(OpenDialog1.FileName);
      frReport1.DesignReport;
    finally
      frReport1.Free;
    end;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if Serv <> nil then
    StopBtnClick(Sender);
end;

procedure TForm1.CloseBtnClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.Label3Click(Sender: TObject);
begin
  {$IFNDEF Linux}
  ShellExecute(GetDesktopWindow, 'open', PChar(Label3.Caption), nil, nil, SW_SHOW);
  {$ELSE}
  LCLIntf.OpenURL(Label3.Caption);
  {$ENDIF}
end;

procedure TForm1.MinimizeBtnClick(Sender: TObject);
begin
  PostMessage(Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
end;

procedure TForm1.StartBtnClick(Sender: TObject);
begin
    Screen.Cursor := crHourGlass;
    try
      Serv.Open;
      if Serv.Active then
      begin
        StartBtn.Enabled := False;
        StopBtn.Enabled := True;
        LBActive.Visible := True;
        LBInactive.Visible := False;
        Timer1.Enabled := True;
        Start1.Enabled := False;
        Stop1.Enabled := True;
      end;
    finally
      Screen.Cursor := crDefault;
    end;
end;

procedure TForm1.StopBtnClick(Sender: TObject);
begin
  Screen.Cursor := crHourGlass;
  try
    Timer1.Enabled := False;
    Serv.Close;
    StartBtn.Enabled := True;
    StopBtn.Enabled := False;
    LBActive.Visible := False;
    LBInactive.Visible := True;
    Start1.Enabled := True;
    Stop1.Enabled := False;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  ETotalSessions.Text := IntToStr(ServerStatistic.TotalSessionsCount);
  ETotalReports.Text := IntToStr(ServerStatistic.TotalReportsCount);
  EMaxSessions.Text := IntToStr(ServerStatistic.MaxSessionsCount);
  EMaxReports.Text := IntToStr(ServerStatistic.MaxReportsCount);
  EErrors.Text := IntToStr(ServerStatistic.TotalErrors);
  ECurrentSessions.Text := IntToStr(ServerStatistic.CurrentSessionsCount);
  ECurrentReports.Text := IntToStr(ServerStatistic.CurrentReportsCount);
  Uptime.Caption := ServerStatistic.FormatUpTime;
  Label2.Caption := 'Version: ' + String(Serv.Variables.GetValue('SERVER_SOFTWARE'));
end;

procedure TForm1.URLClick(Sender: TObject);
begin
  {$IFNDEF Linux}
  ShellExecute(GetDesktopWindow, 'open', PChar(URL.Caption), nil, nil, SW_SHOW);
  {$ELSE}
  LCLIntf.OpenURL(URL.Caption);
  {$ENDIF}
end;

end.

