unit tpPartTenderSetFme;

interface

uses
  tpPartTenderSet, tpPartTenderFme,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, sdGridDBA, StdCtrls, JimLabels, ZJGrid, ExtCtrls, ActnList,
  dxBar;

type
  TtpPartTenderSetFrame = class(TFrame)
    pnlPartTenders: TPanel;
    zgPartTendersInfo: TZJGrid;
    labGatherTenders: TJimGradLabel;
    pnlCurPartTender: TPanel;
    sprTenderData: TSplitter;
    dxpmPartTenders: TdxBarPopupMenu;
    alPartTenders: TActionList;
    actnExportTpExcel: TAction;
    procedure zgPartTendersInfoCurrentChanged(Sender: TObject; Col,
      Row: Integer);
    procedure dxpmPartTendersPopup(Sender: TObject);
    procedure actnExportTpExcelExecute(Sender: TObject);
    procedure zgPartTendersInfoMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure actnExportTpExcelUpdate(Sender: TObject);
  private
    FPartTenderSetData: TtpPartTenderSet;
    FCurPartTenderFrame: TtpPartTenderFrame;

    procedure AssignTenderInfo;

    procedure ExportPegGclExcel;
    procedure ExportGclPegExcel;
  public
    constructor Create(APartTenderSetData: TtpPartTenderSet);
    destructor Destroy; override;

    procedure RefreshData;
  end;

implementation

uses
  UtilMethods, tpPartTender, MainFrm, ExportExcel;

{$R *.dfm}

{ TtpPartTenderSetFrame }

procedure TtpPartTenderSetFrame.AssignTenderInfo;
var
  i: Integer;
  vPartTender: TtpPartTender;
begin
  zgPartTendersInfo.RowCount := FPartTenderSetData.TenderCount + 1;
  for i := 0 to FPartTenderSetData.TenderCount - 1 do
  begin
    vPartTender := FPartTenderSetData.PartTender[i];
    zgPartTendersInfo.Cells[1, i+1].Text := vPartTender.BeginPeg + '~' + vPartTender.EndPeg;
    zgPartTendersInfo.Cells[1, i+1].Align := gaCenterLeft;
  end;
end;

constructor TtpPartTenderSetFrame.Create(
  APartTenderSetData: TtpPartTenderSet);
begin
  inherited Create(nil);
  zgPartTendersInfo.Cells[1, 0].Text := '标段';

  FPartTenderSetData := APartTenderSetData;
  FPartTenderSetData.SetFrame := Self;
  FCurPartTenderFrame := TtpPartTenderFrame.Create;
  AlignControl(FCurPartTenderFrame, pnlCurPartTender, alClient);
end;

destructor TtpPartTenderSetFrame.Destroy;
begin
  FCurPartTenderFrame.Free;
  inherited;
end;

procedure TtpPartTenderSetFrame.RefreshData;
begin
  AssignTenderInfo;
  FCurPartTenderFrame.PartTenderData := FPartTenderSetData.PartTender[0];
end;

procedure TtpPartTenderSetFrame.zgPartTendersInfoCurrentChanged(
  Sender: TObject; Col, Row: Integer);
var
  iTender: Integer;
begin
  iTender := zgPartTendersInfo.CurRow - 1;
  FCurPartTenderFrame.PartTenderData := FPartTenderSetData.PartTender[iTender];
end;

procedure TtpPartTenderSetFrame.dxpmPartTendersPopup(Sender: TObject);
begin
  SetDxBtnAction(actnExportTpExcel, MainForm.dxbtnExportTpExcel);
end;

procedure TtpPartTenderSetFrame.actnExportTpExcelExecute(Sender: TObject);
begin
  if (FCurPartTenderFrame.tobtnInPegs.Down) then
    ExportPegGclExcel
  else if (FCurPartTenderFrame.tobtnInGcls.Down) then
    ExportGclPegExcel;
end;

procedure TtpPartTenderSetFrame.ExportGclPegExcel;
var
  sFileName: string;
  Exportor: TMasterExcelExportor;
begin
  if SaveExcelFile(sFileName) then
  begin
    Exportor := TMasterExcelExportor.Create;
    try
      Exportor.DefineCol(@ciTpGclPeg_Gcl, Length(ciTpGclPeg_Gcl), @ciTpGclPeg_Peg);
      with FCurPartTenderFrame.PartTenderData.InGclGatherData do
      begin
        Exportor.DefineMasterDataSet(sddGclGather, 'ID');
        Exportor.DefineRelaDataSet(sddGclRela, 'GclID');
      end;
      Exportor.ExportToFile(sFileName);
    finally
      Exportor.Free;
    end;
  end;
end;

procedure TtpPartTenderSetFrame.ExportPegGclExcel;
var
  sFileName: string;
  Exportor: TMasterExcelExportor;
begin
  if SaveExcelFile(sFileName) then
  begin
    Exportor := TMasterExcelExportor.Create;
    try
      Exportor.DefineCol(@ciTpPegGcl, Length(ciTpPegGcl));
      with FCurPartTenderFrame.PartTenderData.InPegData do
      begin
        Exportor.DefineMasterDataSet(sddPeg, 'ID');
        Exportor.DefineRelaDataSet(sddGclBills, 'PegID');
      end;
      Exportor.ExportToFile(sFileName);
    finally
      Exportor.Free;
    end;
  end;
end;

procedure TtpPartTenderSetFrame.zgPartTendersInfoMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if (Button = mbRight) and (FPartTenderSetData.TenderCount > 0) and
      (FCurPartTenderFrame.tobtnInPegs.Down or FCurPartTenderFrame.tobtnInGcls.Down) then
    dxpmPartTenders.PopupFromCursorPos;
end;

procedure TtpPartTenderSetFrame.actnExportTpExcelUpdate(Sender: TObject);
begin
  if (FCurPartTenderFrame.tobtnInPegs.Down) then
    TAction(Sender).Caption := '导出Excel(范围内桩号)'
  else if (FCurPartTenderFrame.tobtnInGcls.Down) then
    TAction(Sender).Caption := '导出Excel(范围内清单)';
end;

end.
