unit IPFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CslButton, IniFiles, ComCtrls, ScPageControl, jpeg,
  ExtCtrls, JimPages;

type
  TIPForm = class(TForm)
    jpsIP: TJimPages;
    jpsIPPageEdit: TJimPage;
    jpsIPPageNo: TJimPage;
    jpsIPPageYes: TJimPage;
    imgEdit: TImage;
    lblTitleEdit: TLabel;
    lblTitleEdit2: TLabel;
    shpIP: TShape;
    lblConSvr: TLabel;
    edtIP: TEdit;
    btnConSvr: TCslButton;
    imgNo: TImage;
    lblTitleNo: TLabel;
    lblIPNo: TLabel;
    lblHintNo: TLabel;
    btnReturn: TCslButton;
    imgYes: TImage;
    lblCompanyName: TLabel;
    lblTitleYes: TLabel;
    lblIPYes: TLabel;
    CslButton1: TCslButton;
    btnLogin: TCslButton;
    procedure btnConSvrClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure shpIPMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnReturnClick(Sender: TObject);
    procedure btnLoginClick(Sender: TObject);
    procedure CslButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function HasIP: Boolean;
    procedure ConnectSever;
  end;

implementation

uses ConstUnit, PHPWebDm;

{$R *.dfm}

{ TIPForm }

procedure TIPForm.ConnectSever;
var sIP, sN: string;
  iResult: Integer;
  ini: TIniFile;
begin
  sIP := Trim(edtIP.Text);
  if sIP = '' then Exit;
  if Pos('http://', sIP) =1 then
    Delete(sIP, 1, 7);

  iResult := PHPWeb.ConnectServer(sIP, sN);
  if iResult = 1 then
  begin
    jpsIP.ActivePage := jpsIPPageYes;
    lblIPYes.Caption := sIP;
    lblIPYes.Update;
    lblCompanyName.Caption := sN;
    lblCompanyName.Update;

    G_Server := sIP;
    G_CompanyName := sN;

    ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Cloud.ini');
    try
      ini.WriteString('URL', 'Server', sIP);
      ini.WriteString('URL', 'CompanyName', sN);
      ini.WriteString('URL', 'LoginCloudURL', 'http://' + sIP);
      PHPWeb.LoginCloudURL := 'http://' + sIP;
    finally
      ini.Free;
    end;
    PHPWeb.ReadIniValues;
  end
  else
  begin
    jpsIP.ActivePage := jpsIPPageNo;
    lblIPNo.Caption := sIP;
    lblIPNo.Update;
  end;
end;

function TIPForm.HasIP: Boolean;
var ini: TIniFile;
begin
  ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Cloud.ini');
  try
    G_Server := Trim(ini.ReadString('URL', 'Server', ''));
    Result := G_Server <> '';

    if Result then
    begin
      G_ServerType := ini.ReadString('URL', 'ServerType', '');
      G_CompanyName := ini.ReadString('URL', 'CompanyName', '');
      G_MeasureURL := 'http://' + G_Server + ini.ReadString('URL', 'MeasureURL', '');
    end;
  finally
    ini.Free;
  end;
end;

procedure TIPForm.btnConSvrClick(Sender: TObject);
begin
  lblConSvr.Visible := True;
  Application.ProcessMessages;

  Screen.Cursor := crHourGlass;
  try
    ConnectSever;
  finally
    lblConSvr.Visible := False;
    Screen.Cursor := crDefault;
  end;
end;

procedure TIPForm.FormCreate(Sender: TObject);
begin
  Caption := G_ProductName;
  lblTitleEdit.Caption := G_ProductName;
  lblTitleNo.Caption := G_ProductName;
  lblTitleYes.Caption := G_ProductName;
end;

procedure TIPForm.shpIPMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  edtIP.SetFocus;
end;

procedure TIPForm.btnReturnClick(Sender: TObject);
begin
  jpsIP.ActivePage := jpsIPPageEdit;
end;

procedure TIPForm.btnLoginClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

procedure TIPForm.CslButton1Click(Sender: TObject);
begin
  jpsIP.ActivePage := jpsIPPageEdit;
end;

end.
