unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, RdpCrypt,
  Vcl.StdCtrls, Vcl.ComCtrls, Vcl.Menus;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListView1: TListView;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
  private
    { Private declarations }
    procedure loadList;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  pub, Unit2, Unit3;

{$R *.dfm}

procedure TForm1.loadList;
var
  I: Integer;
  A: TArray<string>;
  it: TListItem;
begin
  ListView1.Items.Clear;
  for I := 0 to DataLoad.Count - 1 do
  begin
    A := DataLoad[I].Split(['|']);
    if High(A) >= 5 then
    begin
      it := ListView1.Items.Add;
      it.Caption := A[0];
      it.SubItems.Add(A[1] + ':' + A[2])
    end;
    SetLength(A, 0);
  end;
end;

procedure TForm1.N1Click(Sender: TObject);
begin
  Button1Click(Sender);
end;

procedure TForm1.N3Click(Sender: TObject);
begin
 Button2Click(Sender);
end;

procedure TForm1.N4Click(Sender: TObject);
begin
 Button3Click(Sender);
end;

procedure TForm1.N6Click(Sender: TObject);
begin
 Button4Click(Sender);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  A: TArray<string>;
  I: Integer;
begin
  if ListView1.Selected <> nil then
  begin
    I := ListView1.Selected.Index;
    if I < DataLoad.Count then
    begin
      A := DataLoad[I].Split(['|']);
      if High(A) >= 5 then
      begin
        Rdpconnect(A[1], A[2], A[3], A[4], A[5]);
      end;
      SetLength(A, 0);
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Form2.ShowModal;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  I: Integer;
begin

  if ListView1.Selected <> nil then
  begin
    if Application.MessageBox('你确定要删除当前所选主机吗？', '提示', MB_OKCANCEL + MB_ICONQUESTION) = IDOK then
    begin
      I := ListView1.Selected.Index;
      if I < DataLoad.Count then
      begin
        ListView1.Selected.Delete;
        DataLoad.Delete(I);
        DataLoad.SaveToFile(MainDir + 'rdp.dat');
      end;
    end;
  end;

end;

procedure TForm1.Button4Click(Sender: TObject);
var
  I: Integer;
  A: TArray<string>;
begin
  if ListView1.Selected <> nil then
  begin
    I := ListView1.Selected.Index;
    if I < DataLoad.Count then
    begin
      A := DataLoad[I].Split(['|']);
      if High(A) >= 5 then
      begin
        Form3.Edit1.Text := A[0];
        Form3.Edit2.Text := A[1];
        Form3.Edit3.Text := A[2];
        Form3.Edit4.Text := A[3];
        Form3.Edit5.Text := A[4];
        Form3.ComboBox1.ItemIndex:=StrToIntDef(A[5],0);
        Form3.ShowModal;
      end;
      SetLength(A, 0);
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  loadList;
end;

procedure TForm1.ListView1DblClick(Sender: TObject);
begin
  if ListView1.Selected <> nil then
  begin
    Button1Click(Sender);
  end;
end;

end.

