unit U_UserInfo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzButton, ExtCtrls, RzPanel, Buttons,
  U_Main, U_SCSComponent, U_SCSClasses, siComp, siLngLnk;

type
  TF_UserInfo = class(TForm)
    pnOk: TRzPanel;
    btClose: TRzBitBtn;
    pnProjUser: TRzPanel;
    Label4: TLabel;
    Label5: TLabel;
    lbProjUser: TLabel;
    lbRightsProj: TLabel;
    lng_Forms: TsiLangLinked;
    pnPMUser: TRzPanel;
    Label1: TLabel;
    lbPMUser: TLabel;
    Label2: TLabel;
    lbRightsPM: TLabel;
    Label3: TLabel;
    lbRightsNB: TLabel;
    btPMUserChangePass: TSpeedButton;
    btProjUserChangePass: TSpeedButton;
    procedure pnOkResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btPMUserChangePassClick(Sender: TObject);
    procedure btProjUserChangePassClick(Sender: TObject);
  private
    GForm: TForm;
    FMain: TF_Main;
    FProjID: Integer;
  public
    Constructor Create(AOwner: TComponent; AForm: TForm);
    Destructor Destroy; override;

    procedure Execute(AFMain: TF_Main; AProjID: Integer);
  end;

  procedure ShowFMainUserInfo(AFMain: TF_Main; AProjID: Integer);

//var
//  Form1: TForm1;

implementation
Uses U_BaseCommon, U_MakeEditPass;
{$R *.dfm}

constructor TF_UserInfo.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_UserInfo.Destroy;
begin
  inherited;
end;

procedure TF_UserInfo.pnOkResize(Sender: TObject);
begin
  SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;

procedure TF_UserInfo.FormCreate(Sender: TObject);
begin
  pnOkResize(pnOk);
end;

procedure TF_UserInfo.btPMUserChangePassClick(Sender: TObject);
var
  NewPass: String;
  UserInfo: TUserInfo;
begin
  UserInfo := FMain.DM.UsersInfoPM.LoggedUserInfo;
  if UserInfo <> nil then
  begin
    NewPass := GetNewPass(UserInfo.Pass);
    if NewPass <> '' then
    begin
      UserInfo.Pass := NewPass;
      FMain.FCurrUserInfo.Pass := NewPass;
      FMain.DM.SaveUsersInfoPMToBase;
    end;
  end;
end;

procedure TF_UserInfo.btProjUserChangePassClick(Sender: TObject);
var
  NewPass: String;
  ProjectUsersInfo: TUsersInfo;
  ProjUserInfo: TUserInfo;
begin
  try
    if (FMain.FProjUserInfo.ID <> 0) and (FProjID <> 0) then
    begin
      ProjectUsersInfo := FMain.DM.GetUsersInfoFromProject(FProjID);
      try
        ProjUserInfo := ProjectUsersInfo.GetUserInfoByName(FMain.FProjUserInfo.Name);
        if ProjUserInfo <> nil then
        begin
          NewPass := GetNewPass(FMain.FProjUserInfo.Pass);
          if NewPass <> '' then
          begin
            ProjUserInfo.Pass := NewPass;
            FMain.FProjUserInfo.Pass := NewPass;
            FMain.DM.SaveUsersInfoToProject(FProjID, ProjectUsersInfo);
          end;
        end;
      finally
        FreeAndNil(ProjectUsersInfo);
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_UserInfo.btProjUserChangePassClick', E.Message);
  end;
end;


procedure TF_UserInfo.Execute(AFMain: TF_Main; AProjID: Integer);
begin
  FMain := AFMain;
  FProjID := AProjID;

  lbPMUser.Caption := FMain.FCurrUserInfo.Name;
  lbRightsPM.Caption := GetUserRightCaption(FMain.FCurrUserInfo.RightsPM);
  lbRightsNB.Caption := GetUserRightCaption(FMain.FCurrUserInfo.RightsNB);

  pnProjUser.Visible := FMain.FProjUserInfo.ID <> 0;
  if pnProjUser.Visible then
  begin
    lbProjUser.Caption := FMain.FProjUserInfo.Name;
    lbRightsProj.Caption := GetUserRightCaption(FMain.FProjUserInfo.RightsPM);
  end;
  Self.AutoSize := false;
  Self.AutoSize := true;

  ShowModal;
end;

procedure ShowFMainUserInfo(AFMain: TF_Main; AProjID: Integer);
var
  F_UserInfo: TF_UserInfo;
begin
  try
    F_UserInfo := TF_UserInfo.Create(Application, nil);
    try
      F_UserInfo.Execute(AFMain, AProjID);
    finally
      FreeAndNil(F_UserInfo);
    end;
  except
    on E: Exception do AddExceptionToLogEx('ShowFMainUserInfo', E.Message);
  end;
end;

end.