unit Profile_editName;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;

type
  TfrmProfile_editName = class(TForm)
    Panel1: TPanel;
    txtEditName: TEdit;
    btnOk: TBitBtn;
    btnCancel: TBitBtn;
    lbIDNum: TLabel;

    procedure btnCancelClick(Sender: TObject);
    procedure Clear();
    procedure btnOkClick(Sender: TObject);
    procedure EditName();
   // procedure EditPosition();
    procedure EditAddress();
    procedure EditCNum();
    procedure EditUName();

  private
    { Private declarations }
  public
    { Public declarations }
    id:String;
  end;

var
  frmProfile_editName: TfrmProfile_editName;
  token : String;
implementation

uses Profile, jvuib, DM, AccountInformation;

{$R *.dfm}

procedure TfrmProfile_editName.btnCancelClick(Sender: TObject);
begin
      Clear();
      Close;
end;

procedure TfrmProfile_editName.btnOkClick(Sender: TObject);
begin
  if token = 'name' then begin
    frmProfile.txtName.Caption := Trim(txtEditName.Text);
    EditName();
  end
  else if token = 'address' then begin
    frmProfile.txtAddress.Caption := Trim(txtEditName.Text);
    EditAddress();
  end
  else if token = 'cnum' then begin
    frmProfile.txtConNum.Caption := Trim(txtEditName.Text);
    EditCNum();
  end
  else if token = 'uname' then begin
    frmProfile.txtUName.Caption := Trim(txtEditName.Text);
    EditUName();
  end
end;

//............dirty works here
procedure TfrmProfile_editName.Clear();
begin
    txtEditName.Clear;
end;

procedure TfrmProfile_editName.EditName();
begin
      if (txtEditName.Text ='') then begin
         MessageDlg('Please enter new name',mtError,[mbOK],0);
         txtEditName.SetFocus;
      end
      else begin
         if MessageDlg('Save new name?',mtConfirmation,[mbYes,mbNo],0)= mrYes then begin
              id:= lbIDNum.Caption;

            if DModule.TR.InTransaction then
                DModule.TR.Commit;

                DModule.QR.SQL.Clear;
                DModule.QR.SQL.Add('UPDATE usersinfo SET FULLNAME=:A, MODIFIEDON=:B  WHERE UI_ID =:C');
                  DModule.QR.Params.AsString[0] := Trim(txtEditName.Text);
                  DModule.QR.Params.AsString[1] := DateToStr(Now);
                  DModule.QR.Params.AsString[2] := id;
                    MessageDlg('Your name has been successfully updated',mtInformation,[mbOK],0);
                DModule.QR.Prepare;
                DModule.QR.Open();

                if DModule.TR.InTransaction then
                    DModule.TR.Commit;
                    frmUserInformation.loadAccountInfo();
                    Close;
         end
         else begin
           MessageDlg('Cancelled changes',mtInformation,[mbOK],0);
           Clear();
         end
      end
end;

procedure TfrmProfile_editName.EditAddress();
begin
      if (txtEditName.Text ='') then begin
         MessageDlg('Please enter new address',mtError,[mbOK],0);
         txtEditName.SetFocus;
      end
      else begin
         if MessageDlg('Update address?',mtConfirmation,[mbYes,mbNo],0)= mrYes then begin
              id:= lbIDNum.Caption;
            if DModule.TR.InTransaction then
                DModule.TR.Commit;

                DModule.QR.SQL.Clear;
                DModule.QR.SQL.Add('UPDATE usersinfo SET ADDRESS=:A, MODIFIEDON=:B  WHERE UI_ID =:C');
                  DModule.QR.Params.AsString[0] := Trim(txtEditName.Text);
                  DModule.QR.Params.AsString[1] := DateToStr(Now);
                  DModule.QR.Params.AsString[2] := id;
                    MessageDlg('Address has been successfully updated',mtInformation,[mbOK],0);
                DModule.QR.Prepare;
                DModule.QR.Open();

                if DModule.TR.InTransaction then
                    DModule.TR.Commit;
                    frmUserInformation.loadAccountInfo;
                    Close;
         end
         else begin
           MessageDlg('Cancelled changes',mtInformation,[mbOK],0);
           Clear();
         end
      end
end;

procedure TfrmProfile_editName.EditCNum();
begin
      if (txtEditName.Text ='') then begin
         MessageDlg('Please enter new contact #',mtError,[mbOK],0);
         txtEditName.SetFocus;
      end
      else begin
         if MessageDlg('Update new contact #?',mtConfirmation,[mbYes,mbNo],0)= mrYes then begin
              id:= lbIDNum.Caption;
            if DModule.TR.InTransaction then
                DModule.TR.Commit;

                DModule.QR.SQL.Clear;
                DModule.QR.SQL.Add('UPDATE usersinfo SET CONTACTNUM=:A, MODIFIEDON=:B  WHERE UI_ID =:C');
                  DModule.QR.Params.AsString[0] := Trim(txtEditName.Text);
                  DModule.QR.Params.AsString[1] := DateToStr(Now);
                  DModule.QR.Params.AsString[2] := id;
                    MessageDlg('Contact # has been successfully updated',mtInformation,[mbOK],0);
                DModule.QR.Prepare;
                DModule.QR.Open();

                if DModule.TR.InTransaction then
                    DModule.TR.Commit;
                    frmUserInformation.loadAccountInfo;
                    Close;
         end
         else begin
           MessageDlg('Cancelled changes',mtInformation,[mbOK],0);
           Clear();
         end
      end
end;

procedure TfrmProfile_editName.EditUName();
begin
     if (txtEditName.Text ='') then begin
         MessageDlg('Please enter new username',mtError,[mbOK],0);
         txtEditName.SetFocus;
      end
      else begin
         if MessageDlg('Update username?',mtConfirmation,[mbYes,mbNo],0)= mrYes then begin
              id:= lbIDNum.Caption;
            if DModule.TR.InTransaction then
                DModule.TR.Commit;

                DModule.QR.SQL.Clear;
                DModule.QR.SQL.Add('UPDATE usersinfo SET USERNAME=:A, MODIFIEDON=:B  WHERE UI_ID =:C');
                  DModule.QR.Params.AsString[0] := Trim(txtEditName.Text);
                  DModule.QR.Params.AsString[1] := DateToStr(Now);
                  DModule.QR.Params.AsString[2] := id;
                    MessageDlg('Username has been successfully updated',mtInformation,[mbOK],0);
                DModule.QR.Prepare;
                DModule.QR.Open();

                if DModule.TR.InTransaction then
                    DModule.TR.Commit;
                    frmUserInformation.loadAccountInfo;
                    Close;
         end
         else begin
           MessageDlg('Cancelled changes',mtInformation,[mbOK],0);
           Clear();
         end
      end
end;
end.
