unit stgResultFrm;

interface

uses
  stgGatherDm, sdIDTree, UtilMethods, stgExcelExport, stgSubGatherFile,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JimTabs, JimPages, sdGridDBA, sdGridTreeDBA, ZJGrid, ExtCtrls,
  StdCtrls, dxBar, ActnList;

type
  TstgResultForm = class(TForm)
    jpsResult: TJimPages;
    jpsResultErrorDetail: TJimPage;
    jpsResultBillsDetail: TJimPage;
    jtsGatherData: TJimTabSet;
    pnlGatherTree: TPanel;
    zgGatherTree: TZJGrid;
    saGatherTree: TsdGridTreeDBA;
    pnlDetail: TPanel;
    zgBillsDetail: TZJGrid;
    Splitter1: TSplitter;
    sdBillsDetail: TsdGridDBA;
    pnlError: TPanel;
    pnlErrorInfo: TPanel;
    zgErrorInfo: TZJGrid;
    sdErrorInfo: TsdGridDBA;
    pnlResult: TPanel;
    lblResult: TLabel;
    Splitter2: TSplitter;
    pnlErrorDetail: TPanel;
    zgErrorDetail: TZJGrid;
    sdErrorDetail: TsdGridDBA;
    dxpmError: TdxBarPopupMenu;
    dxpmGatherTree: TdxBarPopupMenu;
    alStgResult: TActionList;
    actnExportAllError: TAction;
    actnExportTenderError: TAction;
    actnExportStgResultExcel: TAction;
    actnExportStgResult: TAction;
    procedure jtsGatherDataChange(Sender: TObject; NewTab: Integer;
      var AllowChange: Boolean);
    procedure zgGatherTreeCellGetColor(Sender: TObject; ACoord: TPoint;
      var AColor: TColor);
    procedure zgErrorInfoMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure zgGatherTreeMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure dxpmErrorPopup(Sender: TObject);
    procedure actnExportAllErrorExecute(Sender: TObject);
    procedure actnExportTenderErrorExecute(Sender: TObject);
    procedure dxpmGatherTreePopup(Sender: TObject);
    procedure actnExportStgResultExcelExecute(Sender: TObject);
    procedure actnExportStgResultExecute(Sender: TObject);
  private
    FGatherData: TstgGatherData;
    FExcelExportor: TstgErrorExcelExport;
    function GetExcelExportor: TstgErrorExcelExport;
  public
    destructor Destroy; override;

    procedure SetGatherData(AGatherData: TstgGatherData);

    property ExcelExportor: TstgErrorExcelExport read GetExcelExportor; 
  end;

implementation

uses
  MainFrm;

{$R *.dfm}

procedure TstgResultForm.jtsGatherDataChange(Sender: TObject;
  NewTab: Integer; var AllowChange: Boolean);
begin
  jpsResult.ActivePageIndex := NewTab;
end;

procedure TstgResultForm.SetGatherData(AGatherData: TstgGatherData);
begin
  FGatherData := AGatherData;
  saGatherTree.DataView := AGatherData.sdvGatherTree;
  sdBillsDetail.DataView := AGatherData.sdvBillsDetail;
  sdErrorInfo.DataView := AGatherData.sdvSubTenders;
  sdErrorDetail.DataView := AGatherData.sdvErrorDetail;
end;

procedure TstgResultForm.zgGatherTreeCellGetColor(Sender: TObject;
  ACoord: TPoint; var AColor: TColor);
var
  vNode: TsdIDTreeNode;
begin
  vNode := saGatherTree.IDTree.Items[ACoord.Y - zgGatherTree.FixedRowCount];
  if Assigned(vNode) and vNode.Rec.ValueByName('IsSubTender').AsBoolean then
    AColor := $00D5D5D5;
end;

procedure TstgResultForm.zgErrorInfoMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmError.PopupFromCursorPos;
end;

procedure TstgResultForm.zgGatherTreeMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmGatherTree.PopupFromCursorPos;
end;

procedure TstgResultForm.dxpmErrorPopup(Sender: TObject);
begin
  SetDxBtnAction(actnExportAllError, MainForm.dxbtnExportAllError);
  SetDxBtnAction(actnExportTenderError, MainForm.dxbtnExportTenderError);
end;

procedure TstgResultForm.actnExportAllErrorExecute(Sender: TObject);
var
  sFileName: string;
begin
  if SaveExcelFile(sFileName) then
    ExcelExportor.ExportAll(sFileName);
end;

procedure TstgResultForm.actnExportTenderErrorExecute(Sender: TObject);
var
  sFileName: string;
begin
  if SaveExcelFile(sFileName) then
    ExcelExportor.ExportSubTender(sdErrorInfo.DataView.Current, sFileName);
end;

function TstgResultForm.GetExcelExportor: TstgErrorExcelExport;
begin
  if Assigned(FGatherData) then
  begin
    if not Assigned(FExcelExportor) then
      FExcelExportor := TstgErrorExcelExport.Create(FGatherData);
    Result := FExcelExportor;
  end
  else
    ErrorMessage('当前无汇总数据，无法导出');
end;

destructor TstgResultForm.Destroy;
begin
  if Assigned(FExcelExportor) then
    FExcelExportor.Free;
  inherited;
end;

procedure TstgResultForm.dxpmGatherTreePopup(Sender: TObject);
begin
  SetDxBtnAction(actnExportStgResult, MainForm.dxbtnExportStgResult);
  SetDxBtnAction(actnExportStgResultExcel, MainForm.dxbtnExportStgResultExcel);
end;

procedure TstgResultForm.actnExportStgResultExcelExecute(Sender: TObject);
var
  vExportor: TstgGatherExcelExport;
  sFileName: string;
begin
  vExportor := TstgGatherExcelExport.Create(FGatherData);
  try
    if SaveExcelFile(sFileName) then
      vExportor.ExportGather(sFileName);
  finally
    vExportor.Free;
  end;
end;

procedure TstgResultForm.actnExportStgResultExecute(Sender: TObject);
var
  sFileName: string;
  vExportor: TstgSubGatherFileExportor;
begin
  if SaveFile(sFileName, '.sgf') then
  begin
    vExportor := TstgSubGatherFileExportor.Create;
    try
      vExportor.ExportGatherDataTo(FGatherData, sFileName);
    finally
      vExportor.Free;
    end;
  end;
end;

end.
