unit PhaseCompareFme;

interface

uses
  PhaseCompareDm, BillsTree,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ZJGrid, ExtCtrls, sdGridDBA, sdGridTreeDBA;

type
  TPhaseCompareFrame = class(TFrame)
    stdPhaseCompare: TsdGridTreeDBA;
    pnlPhaseCompare: TPanel;
    zgPhaseCompare: TZJGrid;
    procedure zgPhaseCompareCellGetColor(Sender: TObject; ACoord: TPoint;
      var AColor: TColor);
  private
    FPhaseCompareData: TPhaseCompareData;
    FCompareStageCount: Integer;

    procedure ClearStageColumns;
    procedure InitStageColumns;

    procedure BeginExpandNode;
    procedure EndExpandNode;

    procedure SetCompareStageCount(const Value: Integer);
  public
    constructor Create(AProjectFrame: TFrame; PhaseCompareData: TPhaseCompareData);

    procedure ExpandNodeTo(ALevel: Integer);
    procedure ExpandXmjNode;
    procedure ExpandCurPhase;

    property CompareStageCount: Integer read FCompareStageCount write SetCompareStageCount;
  end;

implementation

uses
  ProjectData, PhaseData, sdIDTree, UtilMethods;

{$R *.dfm}

{ TPhaseCompareFrame }

procedure TPhaseCompareFrame.BeginExpandNode;
begin
  zgPhaseCompare.OnCellGetColor := nil;
  BeginUpdateWindow(zgPhaseCompare.Handle);
  stdPhaseCompare.DisableControl;
end;

procedure TPhaseCompareFrame.ClearStageColumns;
var
  iStage: Integer;
  vCol: TsdGridColumn;
begin
  for iStage := 0 to FCompareStageCount - 1 do
  begin
    vCol := stdPhaseCompare.Column('Quantity' + IntToStr(iStage));
    if Assigned(vCol) then
      stdPhaseCompare.Columns.Delete(vCol.Index);
    vCol := stdPhaseCompare.Column('TotalPrice' + IntToStr(iStage));
    if Assigned(vCol) then
      stdPhaseCompare.Columns.Delete(vCol.Index);
  end;
end;

constructor TPhaseCompareFrame.Create(AProjectFrame: TFrame;
  PhaseCompareData: TPhaseCompareData);
begin
  inherited Create(AProjectFrame);
  FPhaseCompareData := PhaseCompareData;
  FCompareStageCount := 0;
  with TProjectData(FPhaseCompareData.ProjectData).PhaseData do
    if Active then
      CompareStageCount := PhaseProperty.AuditCount + 1;
  stdPhaseCompare.IDTree := FPhaseCompareData.CompareTree;
end;

procedure TPhaseCompareFrame.EndExpandNode;
begin
  stdPhaseCompare.EnableControl;
  EndUpdateWindow(zgPhaseCompare.Handle);
  zgPhaseCompare.OnCellGetColor := zgPhaseCompareCellGetColor;
  zgPhaseCompare.Invalidate;
end;

procedure TPhaseCompareFrame.ExpandXmjNode;
begin
  BeginExpandNode;
  try
    FPhaseCompareData.ExpandXmjNode;
  finally
    EndExpandNode;
  end;
end;

procedure TPhaseCompareFrame.ExpandNodeTo(ALevel: Integer);
begin
  BeginExpandNode;
  try
    FPhaseCompareData.ExpandNodeTo(ALevel);
  finally
    EndExpandNode;
  end;
end;

procedure TPhaseCompareFrame.InitStageColumns;

  function StageName(AStageIndex: Integer): string;
  begin
    if AStageIndex = 0 then
      Result := '原报'
    else
      Result := Format('%d 审', [AStageIndex]);
  end;

  procedure AddStageColumns(AStageIndex: Integer);
  var
    vColumn: TsdGridColumn;
  begin
    vColumn := stdPhaseCompare.Columns.Add;
    vColumn.FieldName := Format('Quantity%d', [AStageIndex]);
    vColumn.Title.Caption := Format('%s|数量', [StageName(AStageIndex)]);
    vColumn.Title.CaptionAcrossCols := '2';
    vColumn.Width := 60;
    vColumn.Alignment :=taRightJustify;
    vColumn.ReadOnly := True;
    
    vColumn := stdPhaseCompare.Columns.Add;
    vColumn.FieldName := Format('TotalPrice%d', [AStageIndex]);
    vColumn.Title.Caption := Format('|金额', [StageName(AStageIndex)]);
    vColumn.Width := 60;
    vColumn.Alignment := taRightJustify;
    vColumn.ReadOnly := True;
  end;

var
  iStage: Integer;
begin
  for iStage := 0 to CompareStageCount - 1 do
    AddStageColumns(iStage);
end;

procedure TPhaseCompareFrame.SetCompareStageCount(const Value: Integer);
begin
  ClearStageColumns;
  FCompareStageCount := Value;
  InitStageColumns;
end;

procedure TPhaseCompareFrame.zgPhaseCompareCellGetColor(Sender: TObject;
  ACoord: TPoint; var AColor: TColor);

  function CheckSameQuantity: Boolean;
  var
    iCol: Integer;
  begin
    iCol := 5;
    Result := True;
    while Result and (iCol + 2 < zgPhaseCompare.ColCount) do
    begin
      with zgPhaseCompare do
        Result := Cells[iCol, ACoord.Y].Text = Cells[iCol + 2, ACoord.Y].Text;
      Inc(iCol, 2);
    end;
  end;

  function CheckSameTotalPrice: Boolean;
  var
    iCol: Integer;
  begin
    iCol := 6;
    Result := True;
    while Result and (iCol + 2 < zgPhaseCompare.ColCount) do
    begin
      with zgPhaseCompare do
        Result := Cells[iCol, ACoord.Y].Text = Cells[iCol + 2, ACoord.Y].Text;
      Inc(iCol, 2);
    end;
  end;

var
  stnNode: TsdIDTreeNode;
  iCreatePhaseID: Integer;
begin
  if ACoord.Y > stdPhaseCompare.IDTree.Count + 1 then Exit;
  stnNode := stdPhaseCompare.IDTree.Items[ACoord.Y - 2];
  iCreatePhaseID := stnNode.Rec.ValueByName('CreatePhaseID').AsInteger;
  // 根据层次底色不同
  if stnNode.ParentID = 1 then
    AColor := $00FBCAC4
  else if (stnNode.Rec.ValueByName('B_Code').AsString = '') and (stnNode.Level > 0) then
    AColor := $00F9E8DF;
  // 根据节点创建期数底色不同
  if iCreatePhaseID > 0 then
  begin
    if iCreatePhaseID > TProjectData(FPhaseCompareData.ProjectData).PhaseIndex then
      AColor := clSilver
    else if iCreatePhaseID = TProjectData(FPhaseCompareData.ProjectData).PhaseIndex then
      AColor := clInfoBk;
  end;
  // 各阶段数量或者金额不等，该行底色标红
  if (zgPhaseCompare.ColCount > 7) and not stnNode.HasChildren then
  begin
    if not CheckSameQuantity or not CheckSameTotalPrice then
      AColor := $00646AFE;
  end;
end;

procedure TPhaseCompareFrame.ExpandCurPhase;
begin
  BeginExpandNode;
  try
    FPhaseCompareData.ExpandCurPhase;
  finally
    EndExpandNode;
  end;
end;

end.
