unit tpMainFrm;

interface

uses
  tpMainData, tpPartTenderFme, tpPartTenderSetFme,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JimTabs, JimPages, dxBarExtItems, dxBar, ZJGrid, sdGridDBA,
  ActnList, StdCtrls, JimLabels, sdGridTreeDBA, ExtCtrls;

const
  sGatherResult = ' 汇总后，共含桩号节点%d个，起点桩号为%s，终点桩号为%s，桩号节点总金额为%s';

type
  TtpMainForm = class(TForm)
    jtsMainDatas: TJimTabSet;
    jpsMain: TJimPages;
    dxBarManager: TdxBarManager;
    dxsiPart: TdxBarSubItem;
    dxbtnTrial: TdxBarButton;
    dxbtnPartByLength: TdxBarButton;
    dxbtnPartByPrice: TdxBarButton;
    dxbtnReGather: TdxBarButton;
    jpsMainBaseTender: TJimPage;
    zgSelectTenders: TZJGrid;
    saSelectTenders: TsdGridDBA;
    jpsMainTrial: TJimPage;
    labGatherResult: TJimGradLabel;
    jpsMainPart: TJimPage;
    dxbtnGatherGclBills: TdxBarButton;
    sprNoPegData: TSplitter;
    pnlNoPegData: TPanel;
    labNoPegData: TJimGradLabel;
    zgNoPeg: TZJGrid;
    staNoPeg: TsdGridTreeDBA;
    pnlGatherTenders: TPanel;
    labGatherTenders: TJimGradLabel;
    procedure jtsMainDatasChange(Sender: TObject; NewTab: Integer;
      var AllowChange: Boolean);
    procedure dxbtnTrialClick(Sender: TObject);
    procedure dxbtnReGatherClick(Sender: TObject);
    procedure dxbtnPartByPriceClick(Sender: TObject);
    procedure dxbtnPartByLengthClick(Sender: TObject);
  private
    FMainData: TtpMainData;

    FProjects: TList; // ID类型，缓存用户选择，再次选择时加载
    FTenders: TList; // TtpSelectTenderNode类型，缓存用户选择的标段，汇总时加载部分后期展示数据

    FtpPartTrialFrame: TtpPartTenderFrame;
    FtpPartTenderSetFrame: TtpPartTenderSetFrame;
  public
    constructor Create;
    destructor Destroy; override;

    function GatherBaseTenders: Boolean;
  end;

procedure PartTendersModel;

implementation

uses
  ZhAPI, tpSelectTendersFrm, sdIDTree, Globals, tpSelectTenderNode,
  mPegFilter, UtilMethods, tpNoPegDm;

procedure PartTendersModel;
var
  tpForm: TtpMainForm;
begin
  tpForm := TtpMainForm.Create;
  try
    if tpForm.GatherBaseTenders then
      tpForm.ShowModal; 
  finally
    tpForm.Free;
  end;
end;

{$R *.dfm}

constructor TtpMainForm.Create;
begin
  Inherited Create(nil);
  FMainData := TtpMainData.Create;
  saSelectTenders.DataView := FMainData.SelectTenderData.sdvTenders;
  staNoPeg.IDTree := FMainData.BaseGatherData.NoPegData.NoPegTree;

  FtpPartTrialFrame := TtpPartTenderFrame.Create;
  FtpPartTrialFrame.PartTenderData := FMainData.PartTrial;
  AlignControl(FtpPartTrialFrame, jpsMainTrial, alClient);

  FtpPartTenderSetFrame := TtpPartTenderSetFrame.Create(FMainData.PartTenderSet);
  AlignControl(FtpPartTenderSetFrame, jpsMainPart, alClient);

  FProjects := TList.Create;
end;

destructor TtpMainForm.Destroy;
begin
  FtpPartTenderSetFrame.Free;
  FtpPartTrialFrame.Free;
  FProjects.Free;
  FMainData.Free;
  inherited;
end;

function TtpMainForm.GatherBaseTenders: Boolean;
begin
  Result := False;
  if SelectTendersToPart(FProjects) then
  begin
    FMainData.GatherProjects(FProjects);
    with FMainData.BaseGatherData.Pegs do
      labGatherResult.Caption := Format(sGatherResult, [Count, BeginPeg, EndPeg, FormatFloat('##,###,###,###.##', TotalPrice)]);
    Result := True;
  end;
end;

procedure TtpMainForm.jtsMainDatasChange(Sender: TObject; NewTab: Integer;
  var AllowChange: Boolean);
begin
  if (NewTab > -1) and (NewTab < jtsMainDatas.Tabs.Count) then
    jpsMain.ActivePageIndex := NewTab;
end;

procedure TtpMainForm.dxbtnTrialClick(Sender: TObject);
begin
  FMainData.TrialPeg;
  FtpPartTrialFrame.RefreshResult;
end;

procedure TtpMainForm.dxbtnReGatherClick(Sender: TObject);
begin
  if QuestMessage('切换汇总项目将清空当前的桩号试算、标段划分结果，是否继续？') then
    GatherBaseTenders;
end;

procedure TtpMainForm.dxbtnPartByPriceClick(Sender: TObject);
begin
  FMainData.PartTenderSet.PricePart(FMainData.BaseGatherData.Pegs);
  if jtsMainDatas.TabIndex <> 1 then
    jtsMainDatas.TabIndex := 1;
end;

procedure TtpMainForm.dxbtnPartByLengthClick(Sender: TObject);
begin
  FMainData.PartTenderSet.PegPart(FMainData.BaseGatherData.Pegs);
  if jtsMainDatas.TabIndex <> 2 then
    jtsMainDatas.TabIndex := 2;
end;

end.
