unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Unit2, Unit3, Unit4, Unit5;


type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

function KCheckinByMobile(userName: PChar; userPasswd: PChar; lockID: PChar;owner: PChar; ownerID: PChar; mobile: PChar; reserved: PChar; expiryDate: PChar; options: Integer; returnKeyID: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KCheckinByMobileWithPinCode(userName: PChar; userPasswd: PChar; lockID: PChar;owner: PChar; ownerID: PChar; mobile: PChar; reserved: PChar; expiryDate: PChar; withPinCode: Integer; options: Integer; returnKeyID: PChar; returnVerifyCode: PChar; returnPassword: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KCheckinByUserId(userName: PChar; userPasswd: PChar; lockID: PChar;owner: PChar; ownerID: PChar; userId: PChar; sdkClientID: PChar; sdkClientSecret: PChar; reserved: PChar; expiryDate: PChar; options: Integer; returnKeyID: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KCheckinByPassword(userName: PChar; userPasswd: PChar; lockID: PChar;owner: PChar; ownerID: PChar; reserved:PChar; expiryDate: PChar; options: Integer; returnKeyID: PChar; returnPinCode: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KCheckinByKeycard(userName: PChar; userPasswd: PChar; lockID: PChar;owner: PChar; ownerID: PChar; expiryDate: PChar; options: Integer; returnKeyID: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KCancelKeyByKeyID(userName: PChar; userPasswd: PChar; keyID: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KCancelKeyByLockID(userName: PChar; userPasswd: PChar; lockID: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KCancelKeyByKeycard(userName: PChar; userPasswd: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KReadKeycard(userName: PChar; userPasswd: PChar; returnKeyID: PChar; returnLockID: PChar; returnOwner: PChar; returnMakeDate: PChar; returnEndDate: PChar): Integer; stdcall;
  external 'libLockman.dll';

function KFireBeepSuccess(;): Integer; stdcall;
  external 'libLockman.dll';

function KFireBeepFailure(;): Integer; stdcall;
  external 'libLockman.dll';

{$R *.dfm}

procedure TForm1.Button3Click(Sender: TObject);
var
  ret: Integer;
  returnKeyID: array[0..64] of char;
  returnVerifyCode: array[0..64] of char;
  returnPassword: array[0..64] of char;
begin
  ret := Form2.ShowModal;

  if (ret <> mrOK) then begin
    Exit;
  end;
  
  ret := KCheckinByMobileWithPinCode(
      PChar(Edit1.Text),
      PChar(Edit2.Text),
      PChar(Edit3.Text),
      PChar(Form2.Edit1.Text),
      '',
      PChar(Form2.Edit2.Text),
      '',
      PChar(Form2.Edit3.Text),
      1,
      0,
      returnKeyID,
      returnVerifyCode,
      returnPassword
      );

  if (ret <> 0) then begin
    KFireBeepFailure();
    Application.MessageBox(PChar(Format('Operate fail:%d',[ret])), 'err', MB_OK);
    Exit;
  end;

  KFireBeepSuccess();
  Application.MessageBox(PChar(Format('Operate done, keyID=%s,verifyCode=%s,pinCode=%s', [returnKeyID, returnVerifyCode, returnPassword])), 'info', MB_OK);
end;                          

procedure TForm1.Button1Click(Sender: TObject);
var
  ret: Integer;
  returnKeyID: array[0..64] of char;
begin
  ret := Form4.ShowModal;

  if (ret <> mrOK) then begin
    Exit;
  end;
  
  ret := KCheckinByKeycard(
      PChar(Edit1.Text),
      PChar(Edit2.Text),
      PChar(Edit3.Text),
      PChar(Form4.Edit1.Text),
      '',
      PChar(Form4.Edit3.Text),
      0,
      returnKeyID
      );

  if (ret <> 0) then begin
    KFireBeepFailure();
    Application.MessageBox(PChar(Format('Operate fail:%d',[ret])), 'err', MB_OK);
    Exit;
  end;

  KFireBeepSuccess();
  Application.MessageBox(PChar(Format('Operate done, keyID=%s', [returnKeyID])), 'info', MB_OK);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  ret: Integer;
  returnKeyID: array[0..64] of char;
  returnPinCode: array[0..64] of char;
begin
   ret := Form4.ShowModal;

  if (ret <> mrOK) then begin
    Exit;
  end;
  
  ret := KCheckinByPassword(
      PChar(Edit1.Text),
      PChar(Edit2.Text),
      PChar(Edit3.Text),
      PChar(Form4.Edit1.Text),
      '',
      '',
      PChar(Form4.Edit3.Text),
      0,
      returnKeyID,
      returnPinCode
      );

  if (ret <> 0) then begin
    KFireBeepFailure();
    Application.MessageBox(PChar(Format('Operate fail:%d',[ret])), 'err', MB_OK);
    Exit;
  end;

  KFireBeepSuccess();
  Application.MessageBox(PChar(Format('Operate done, keyID=%s, pinCode=%s', [returnKeyID, returnPinCode])), 'info', MB_OK);
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  ret: Integer;
  returnKeyID: array[0..64] of char;
begin
 ret := Form5.ShowModal;
                   
  if (ret <> mrOK) then begin
    Exit;
  end;

  ret := KCheckinByUserId(
      PChar(Edit1.Text),
      PChar(Edit2.Text),
      PChar(Edit3.Text),
      PChar(Form5.Edit1.Text),
      '',
      PChar(Form5.Edit2.Text),
      PChar(Form5.Edit4.Text),
      PChar(Form5.Edit5.Text),
      '',
      PChar(Form5.Edit3.Text),
      0,
      returnKeyID
      );

  if (ret <> 0) then begin
    KFireBeepFailure();
    Application.MessageBox(PChar(Format('Operate fail:%d',[ret])), 'err', MB_OK);
    Exit;
  end;

  KFireBeepSuccess();
  Application.MessageBox(PChar(Format('Operate done, keyID=%s', [returnKeyID])), 'info', MB_OK);
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  ret: Integer;
  returnKeyID: array[0..64] of char;
  returnLockID: array[0..64] of char;
  returnOwner: array[0..64] of char;
  returnMakeDate: array[0..64] of char;
  returnEndDate: array[0..64] of char;
begin
  ret := KReadKeycard(
          PChar(Edit1.Text),
          PChar(Edit2.Text),
          returnKeyID,
          returnLockID,
          returnOwner,
          returnMakeDate,
          returnEndDate);
  if (ret <> 0) then begin
    KFireBeepFailure();
    Application.MessageBox(PChar(Format('Operate fail:%d',[ret])), 'err', MB_OK);
    Exit;
  end;

  KFireBeepSuccess();
  Application.MessageBox(PChar(Format('keyID=%s,lockID=%s,owner=%s,make date:%s,end date:%s',[returnKeyID, returnLockID, returnOwner, returnMakeDate, returnEndDate])), 'info', MB_OK);

end;

procedure TForm1.Button6Click(Sender: TObject);
var
  ret: Integer;
begin
  ret := KCancelKeyByKeycard(
          PChar(Edit1.Text),
          PChar(Edit2.Text));
  if (ret <> 0) then begin
    KFireBeepFailure();
    Application.MessageBox(PChar(Format('Operate fail:%d',[ret])), 'err', MB_OK);
    Exit;
  end;

  KFireBeepSuccess();
  Application.MessageBox(PChar(Format('Operate done',[])), 'info', MB_OK);

end;

procedure TForm1.Button7Click(Sender: TObject);
var
  ret: Integer;
  lockID: String;
begin
   lockID := InputBox('Enter the lock id', '', '13112');
   if (lockID = '') then begin
    Exit;
   end;

   ret := KCancelKeyByLockID(
          PChar(Edit1.Text),
          PChar(Edit2.Text),
          PChar(lockID));
  if (ret <> 0) then begin
    KFireBeepFailure();
    Application.MessageBox(PChar(Format('Operate fail:%d',[ret])), 'err', MB_OK);
    Exit;
  end;

  KFireBeepSuccess();
  Application.MessageBox(PChar(Format('Operate done',[])), 'info', MB_OK);
end;

procedure TForm1.Button8Click(Sender: TObject);
var
  ret: Integer;
  keyID: String;
begin
  keyID := InputBox('Enter the key id', '', '');
   if (keyID = '') then begin
    Exit;
   end;

   ret := KCancelKeyByKeyID(
          PChar(Edit1.Text),
          PChar(Edit2.Text),
          PChar(keyID));
  if (ret <> 0) then begin
    KFireBeepFailure();
    Application.MessageBox(PChar(Format('Operate fail:%d',[ret])), 'err', MB_OK);
    Exit;
  end;

  KFireBeepSuccess();
  Application.MessageBox(PChar(Format('Operate done',[])), 'info', MB_OK);
end;

end.
