unit ReportManagerFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, DB, DBClient, ZJGrid, StdCtrls, ExtCtrls, ZjGridDBA,
  cxLookAndFeelPainters, cxButtons, pngimage;

type
  TReportManagerForm = class(TForm)
    pcReport: TPageControl;
    tsLocal: TTabSheet;
    tsOnline: TTabSheet;
    tsCustom: TTabSheet;
    Panel1: TPanel;
    pnlInputCode: TPanel;
    edtCode: TEdit;
    Label2: TLabel;
    zaLocal: TZjGridDBA;
    Label1: TLabel;
    zgOnline: TZJGrid;
    zaOnline: TZjGridDBA;
    btnGet: TcxButton;
    pnlCustom: TPanel;
    pnlNoReport: TPanel;
    zgCustom: TZJGrid;
    zaCustom: TZjGridDBA;
    cbCategory: TComboBox;
    pnlGrid: TPanel;
    pnlFilter: TPanel;
    Label3: TLabel;
    imgfilter: TImage;
    chkFilter: TCheckBox;
    zgLoacl: TZJGrid;
    zgCode: TZJGrid;
    zaCode: TZjGridDBA;
    Label4: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure zgLoaclCellGetFont(Sender: TObject; ACoord: TPoint;
      AFont: TFont);
    procedure zgOnlineCellGetFont(Sender: TObject; ACoord: TPoint;
      AFont: TFont);
    procedure zgLoaclMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure zgOnlineMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure cbCategoryChange(Sender: TObject);
    procedure btnGetClick(Sender: TObject);
    procedure zgCustomCellGetFont(Sender: TObject; ACoord: TPoint;
      AFont: TFont);
    procedure zgCustomMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pcReportChange(Sender: TObject);
    procedure zgOnlineShowHint(var HintStr: String; var CanShow: Boolean;
      var HintInfo: THintInfo; const ACoord: TPoint);
    procedure zgLoaclShowHint(var HintStr: String; var CanShow: Boolean;
      var HintInfo: THintInfo; const ACoord: TPoint);
    procedure zgCustomShowHint(var HintStr: String; var CanShow: Boolean;
      var HintInfo: THintInfo; const ACoord: TPoint);
    procedure chkFilterClick(Sender: TObject);
  private
    { Private declarations }
    FReports: TObject;
    FReportsForm: TObject;
    function CurValue(AGridDBA: TZjGridDBA; FieldName: string): Variant;
    function Col(AGridDBA: TZjGridDBA; FieldName: string): Integer;
    procedure GridGetFont(Sender: TObject; ACoord: TPoint; AFont: TFont; AGrid: TZjGrid; AGridDBA: TZjGridDBA);
    procedure GridMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer; AGrid: TZjGrid; AGridDBA: TZjGridDBA);
    procedure GridShowHint(var HintStr: String; var CanShow: Boolean; var HintInfo: THintInfo; const ACoord: TPoint; AGrid: TZjGrid; AGridDBA: TZjGridDBA);
    procedure SetReportsForm(const Value: TObject);
  public
    { Public declarations }
    property ReportsForm: TObject read FReportsForm write SetReportsForm;
  end;


implementation

uses ReportManagerDM, ReportHistoryFrm, ReportsFrm;

{$R *.dfm}

function TReportManagerForm.Col(AGridDBA: TZjGridDBA; FieldName: string): Integer;
begin
  Result := AGridDBA.ColumnIndex(FieldName) + 1;
end;

function TReportManagerForm.CurValue(AGridDBA: TZjGridDBA; FieldName: string): Variant;
begin
  Result := AGridDBA.DataSet.FieldByName(FieldName).AsVariant;
end;

procedure TReportManagerForm.FormCreate(Sender: TObject);
begin
  FReportsForm := nil;
  FReports := TReports.Create(nil);
  zaLocal.DataSet := TReports(FReports).cdsLocal;
  zaLocal.Grid := zgLoacl;
  zaOnline.DataSet := TReports(FReports).cdsOnline;
  zaOnline.Grid := zgOnline;
  zaCustom.DataSet := TReports(FReports).cdsCustom;
  zaCustom.Grid := zgCustom;

  pnlNoReport.Visible := zaLocal.DataSet.RecordCount = 0;
  TReports(FReports).CategoryList(cbCategory.Items);
  cbCategory.ItemIndex := 0;
  pcReport.ActivePage := tsLocal;
  pcReport.OnChange(Sender);
end;

procedure TReportManagerForm.FormDestroy(Sender: TObject);
begin
  FReports.Free;
  if (FReportsForm <> nil) then
    TReportsForm(FReportsForm).RefreshReportTemplets;
end;

procedure TReportManagerForm.GridGetFont(Sender: TObject; ACoord: TPoint;
  AFont: TFont; AGrid: TZjGrid; AGridDBA: TZjGridDBA);
var
  OldActiveRecd: Integer;
begin
  if ACoord.X = Col(AGridDBA, 'State') then
  begin
    if AGridDBA.ChangeActiveRecord(ACoord.Y - AGrid.FixedRowCount, OldActiveRecd)then
    begin
      try
        if (CurValue(AGridDBA, 'State') = '更新') or (CurValue(AGridDBA, 'State') = '下载') then
          AFont.Color := clBlue;
      finally
        AGridDBA.ChangeActiveRecord(OldActiveRecd, OldActiveRecd);
      end;
    end;
  end
  else if ACoord.X = Col(AGridDBA, 'History') then
    AFont.Color := clBlue;
end;

procedure TReportManagerForm.GridMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer; AGrid: TZjGrid;
  AGridDBA: TZjGridDBA);
var
  vForm: TReportHistoryForm;
  vReport: TReport;
  iID: Integer;
begin
  if CurValue(AGridDBA, 'ID') = null then Exit;

  Screen.Cursor := crHourGlass;
  try
    iID := CurValue(AGridDBA, 'ID');
    vReport := TReports(FReports).FindReprot(iID);

    if AGrid.CurCol = Col(AGridDBA, 'State') then
    begin
      if not SameText(vReport.LocalVer, vReport.NewestVer) then
        vReport.Update(vReport.NewestVer);
    end
    else if AGrid.CurCol = Col(AGridDBA, 'History') then
    begin
      vReport.GetHistoryVer;
      vForm := TReportHistoryForm.Create(vReport);
      try
        vForm.ShowModal;
      finally
        vForm.Free;
      end;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TReportManagerForm.zgLoaclCellGetFont(Sender: TObject;
  ACoord: TPoint; AFont: TFont);
begin
  GridGetFont(Sender, ACoord, AFont, zgLoacl, zaLocal);
end;

procedure TReportManagerForm.zgOnlineCellGetFont(Sender: TObject;
  ACoord: TPoint; AFont: TFont);
begin
  GridGetFont(Sender, ACoord, AFont, zgOnline, zaOnline);
end;

procedure TReportManagerForm.zgLoaclMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  GridMouseDown(Sender, Button, Shift, X, Y, zgLoacl, zaLocal);
end;

procedure TReportManagerForm.zgOnlineMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  GridMouseDown(Sender, Button, Shift, X, Y, zgOnline, zaOnline);
end;

procedure TReportManagerForm.cbCategoryChange(Sender: TObject);
begin
  if cbCategory.ItemIndex = 0 then
    TReports(FReports).CancelFilterCategory
  else
    TReports(FReports).FilterCategory(cbCategory.Text);
end;

procedure TReportManagerForm.btnGetClick(Sender: TObject);
begin
  if Trim(edtCode.Text) = '' then Exit;
  if TReports(FReports).LoadCustom(Trim(edtCode.Text)) then
  begin
    pnlInputCode.Visible := False;
  end;
end;

procedure TReportManagerForm.zgCustomCellGetFont(Sender: TObject;
  ACoord: TPoint; AFont: TFont);
begin
  GridGetFont(Sender, ACoord, AFont, zgCustom, zaCustom);
end;

procedure TReportManagerForm.zgCustomMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  GridMouseDown(Sender, Button, Shift, X, Y, zgCustom, zaCustom);
end;

procedure TReportManagerForm.pcReportChange(Sender: TObject);
begin
  if pcReport.ActivePage = tsLocal then
  begin
    pnlNoReport.Visible := not TReports(FReports).HasReport;
    pnlFilter.Visible := TReports(FReports).HasNeedUpdateReport or chkFilter.Checked;
  end;
end;

procedure TReportManagerForm.GridShowHint(var HintStr: String; var CanShow: Boolean;
  var HintInfo: THintInfo; const ACoord: TPoint; AGrid: TZjGrid; AGridDBA: TZjGridDBA);
var vReport: TReport;
  OldActiveRecd, iID: Integer;
begin
  if ACoord.X = 4 then
  begin
    if AGridDBA.ChangeActiveRecord(ACoord.Y - AGrid.FixedRowCount, OldActiveRecd) then
    begin
      try
        if CurValue(AGridDBA, 'ID') = null then Exit;

        iID := CurValue(AGridDBA, 'ID');
        vReport := TReports(FReports).FindReprot(iID);
        if vReport.NewestMemo = '' then
          HintInfo.HintStr := '[暂无更新说明]'
        else
          HintInfo.HintStr := vReport.NewestMemo;
        CanShow := True;
        HintInfo.HintMaxWidth := 250;
        HintInfo.HideTimeout := 30000;
      finally
        AGridDBA.ChangeActiveRecord(OldActiveRecd, OldActiveRecd);
      end;
    end;
  end;
end;

procedure TReportManagerForm.zgOnlineShowHint(var HintStr: String;
  var CanShow: Boolean; var HintInfo: THintInfo; const ACoord: TPoint);
begin
  GridShowHint(HintStr, CanShow, HintInfo, ACoord, zgOnline, zaOnline);
end;

procedure TReportManagerForm.zgLoaclShowHint(var HintStr: String;
  var CanShow: Boolean; var HintInfo: THintInfo; const ACoord: TPoint);
begin
  GridShowHint(HintStr, CanShow, HintInfo, ACoord, zgLoacl, zaLocal);
end;

procedure TReportManagerForm.zgCustomShowHint(var HintStr: String;
  var CanShow: Boolean; var HintInfo: THintInfo; const ACoord: TPoint);
begin
  GridShowHint(HintStr, CanShow, HintInfo, ACoord, zgCustom, zaCustom);
end;

procedure TReportManagerForm.SetReportsForm(const Value: TObject);
begin
  FReportsForm := Value;
end;

procedure TReportManagerForm.chkFilterClick(Sender: TObject);
begin
  TReports(FReports).FilterNeedUpdates(chkFilter.Checked);
end;

end.
