unit rpgZoneProjGatherSelectFrm;

interface

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

type
  TrpgZoneProjGatherSelectForm = class(TForm)
    pnlTop: TPanel;
    pnlPhase: TPanel;
    btnOk: TButton;
    btnCancel: TButton;
    leBeginPhaseIndex: TLabeledEdit;
    leEndPhaseIndex: TLabeledEdit;
    lblPhase: TLabel;
    lblPhaseHint: TLabel;
    procedure leBeginPhaseIndexKeyPress(Sender: TObject; var Key: Char);
    procedure leBeginPhaseIndexChange(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
  private
    FSelectFrame: TProjGatherSelectFrame;
    FError: Integer;

    procedure CheckPhaseIndex;

    procedure RefreshDiffWindows;

    function GetBeginPhaseIndex: Integer;
    function GetEndPhaseIndex: Integer;
    procedure SetBeginPhaseIndex(const Value: Integer);
    procedure SetEndPhaseIndex(const Value: Integer);
  public
    constructor Create(AProjectID: Integer; AProjs: TList);
    destructor Destroy; override;

    property BeginPhaseIndex: Integer read GetBeginPhaseIndex write SetBeginPhaseIndex;
    property EndPhaseIndex: Integer read GetEndPhaseIndex write SetEndPhaseIndex;
  end;

function SelectGatherProject(AProjectID: Integer; AProjs: TList; var ABeginPhaseIndex, AEndPhaseIndex: Integer): Boolean;

implementation

uses
  UtilMethods, Math;

{$R *.dfm}

function SelectGatherProject(AProjectID: Integer; AProjs: TList; var ABeginPhaseIndex, AEndPhaseIndex: Integer): Boolean;
var
  vForm: TrpgZoneProjGatherSelectForm;
begin
  vForm := TrpgZoneProjGatherSelectForm.Create(AProjectID, AProjs);
  vForm.BeginPhaseIndex := ABeginPhaseIndex;
  vForm.EndPhaseIndex := AEndPhaseIndex;
  try
    Result := vForm.ShowModal = mrOk;
    if Result then
    begin
      ABeginPhaseIndex := vForm.BeginPhaseIndex;
      AEndPhaseIndex := vForm.EndPhaseIndex;
      vForm.FSelectFrame.AssignResult(AProjs);
    end;
  finally
    vForm.Free;
  end;
end;

constructor TrpgZoneProjGatherSelectForm.Create(AProjectID: Integer; AProjs: TList);
begin
  inherited Create(nil);

  FSelectFrame := TProjGatherSelectFrame.Create(AProjectID, nil);
  AlignControl(FSelectFrame, pnlTop, alClient);
  FSelectFrame.LoadHistorySelects(AProjs);
  FSelectFrame.AfterSelectProject := CheckPhaseIndex;

  RefreshDiffWindows;
end;

destructor TrpgZoneProjGatherSelectForm.Destroy;
begin
  FSelectFrame.Free;
  inherited;
end;

function TrpgZoneProjGatherSelectForm.GetBeginPhaseIndex: Integer;
begin
  Result := StrToIntDef(leBeginPhaseIndex.Text, -1);
end;

function TrpgZoneProjGatherSelectForm.GetEndPhaseIndex: Integer;
begin
  Result := StrToIntDef(leEndPhaseIndex.Text, -1);
end;

procedure TrpgZoneProjGatherSelectForm.leBeginPhaseIndexKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', #8, #13]) then
    Key := #0;
end;

procedure TrpgZoneProjGatherSelectForm.SetBeginPhaseIndex(const Value: Integer);
begin
  if (Value >= 0) and (Value <= 50) then
    leBeginPhaseIndex.Text := IntToStr(Value)
  else
    leBeginPhaseIndex.Text := '';
end;

procedure TrpgZoneProjGatherSelectForm.SetEndPhaseIndex(const Value: Integer);
begin
  if (Value >= 0) and (Value <= 50) then
    leEndPhaseIndex.Text := IntToStr(Value)
  else
    leEndPhaseIndex.Text := '';
end;

procedure TrpgZoneProjGatherSelectForm.leBeginPhaseIndexChange(
  Sender: TObject);
begin
  CheckPhaseIndex;
end;

procedure TrpgZoneProjGatherSelectForm.CheckPhaseIndex;
begin
  FError := 0;
  if EndPhaseIndex < BeginPhaseIndex then
    FError := 1
  else if (EndPhaseIndex > 0) and (not FSelectFrame.CheckProjPhaseValid(EndPhaseIndex)) then
    FError := 2
  else
    FSelectFrame.ClearMarkedProj;
  lblPhaseHint.Visible := FError <> 0;
  case FError of
    1: lblPhaseHint.Caption := '截止期应大于起始期。';
    2: lblPhaseHint.Caption := '标记为红色的标段，未达第'+ leEndPhaseIndex.Text +'期';
  end;
end;

procedure TrpgZoneProjGatherSelectForm.RefreshDiffWindows;
begin
  ClientHeight := 529;
  ClientWidth := 827;
end;

procedure TrpgZoneProjGatherSelectForm.btnOkClick(Sender: TObject);
begin
  case FError of
    0: ModalResult := mrOk;
    1: WarningMessage('截止期应大于起始期。');
    2: if QuestMessageYesNo('部分选中标段未达第'+leEndPhaseIndex.Text+'期，是否继续汇总？', Handle) then
         ModalResult := mrOk;
  end;
end;

end.
