unit BookmarkFme;

interface

uses
  ProjectData, sdDB,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JimPages, ZJGrid, StdCtrls, JimLabels, ExtCtrls, sdGridDBA,
  ActnList, dxBar;

type
  TBookmarkType = (bmtCompile, bmtMeasure);

  TBookmarkFrame = class(TFrame)
    jpsBookmark: TJimPages;
    jpsBookmarkBills: TJimPage;
    jpsBookmarkStage: TJimPage;
    zgBillsBmk: TZJGrid;
    labBillsBmkTitle: TJimGradLabel;
    labStageBmkTitle: TJimGradLabel;
    zgStageBmk: TZJGrid;
    saBillsBMK: TsdGridDBA;
    saStageBMK: TsdGridDBA;
    pnlBillsMarkMemo: TPanel;
    lblBillsMarkMemo: TLabel;
    pnlStageMarkMemo: TPanel;
    lblStageMarkMemo: TLabel;
    mBillsMarkMemo: TMemo;
    mStageMarkMemo: TMemo;
    dxpmBookmark: TdxBarPopupMenu;
    alBookmark: TActionList;
    actnLocateBookmark: TAction;
    procedure zgBillsBmkCurrentChanged(Sender: TObject; Col, Row: Integer);
    procedure zgStageBmkCurrentChanged(Sender: TObject; Col, Row: Integer);
    procedure mBillsMarkMemoExit(Sender: TObject);
    procedure mStageMarkMemoExit(Sender: TObject);
    procedure actnLocateBookmarkUpdate(Sender: TObject);
    procedure actnLocateBookmarkExecute(Sender: TObject);
    procedure zgBillsBmkMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure dxpmBookmarkPopup(Sender: TObject);
  private
    FProjectData: TProjectData;
    FBMType: TBookmarkType;

    procedure AssignedBillsMarkMemo(ARow: Integer);
    procedure AssignedStageMarkMemo(ARow: Integer);

    procedure LocateLedgerBookmark;
    procedure LocateMeasureBookmark;

    function GetMemoText(AMemo: TMemo): string;

    procedure SetBMType(const Value: TBookmarkType);
  public
    constructor Create(AProjectFrame: TFrame; AProjectData: TProjectData);
    destructor Destroy; override;

    procedure RefreshMarkMemo;

    property BMType: TBookmarkType read FBMType write SetBMType;
  end;

implementation

uses
  Math, PhaseData, MainFrm, UtilMethods;

{$R *.dfm}

{ TBookmarkFrame }

constructor TBookmarkFrame.Create(AProjectFrame: TFrame;
  AProjectData: TProjectData);
begin
  inherited Create(AProjectFrame);
  FProjectData := AProjectData;
  saBillsBMK.DataView := FProjectData.BillsBookmarkData.sdvBillsBookmark;
  AssignedBillsMarkMemo(zgBillsBmk.CurRow);
  saStageBMK.DataView := FProjectData.BillsBookmarkData.sdvStageBookmark;
  AssignedStageMarkMemo(zgStageBmk.CurRow);
end;

destructor TBookmarkFrame.Destroy;
begin
  inherited;
end;

procedure TBookmarkFrame.zgBillsBmkCurrentChanged(Sender: TObject; Col,
  Row: Integer);
begin
  AssignedBillsMarkMemo(Row);
end;

function TBookmarkFrame.GetMemoText(AMemo: TMemo): string;
var
  i: Integer;
begin
  Result := '';
  for i := 0 to AMemo.Lines.Count - 1 do
    Result := Result + AMemo.Lines.Strings[i];
end;

procedure TBookmarkFrame.AssignedBillsMarkMemo(ARow: Integer);
var
  Rec: TsdDataRecord;
begin
  Rec := saBillsBMK.DataView.Records[ARow - 1];
  if (ARow > 0) and Assigned(Rec) then
    mBillsMarkMemo.Text := Rec.ValueByName('MarkMemo').AsString
  else
    mBillsMarkMemo.Text := '';

  mBillsMarkMemo.ReadOnly := not ((ARow > 0) and Assigned(Rec));
end;

procedure TBookmarkFrame.AssignedStageMarkMemo(ARow: Integer);
var
  Rec, StageRec: TsdDataRecord;
begin
  Rec := saStageBMK.DataView.Records[ARow - 1];
  if (ARow > 0) and Assigned(Rec) then
  begin
    StageRec := FProjectData.PhaseData.StageData.StageRecord(Rec.ValueByName('ID').AsInteger);
    mStageMarkMemo.Text := StageRec.ValueByName('MarkMemo').AsString;
  end
  else
    mStageMarkMemo.Text := '';

  mStageMarkMemo.ReadOnly := not ((ARow > 0) and Assigned(Rec));
end;

procedure TBookmarkFrame.zgStageBmkCurrentChanged(Sender: TObject; Col,
  Row: Integer);
begin
  AssignedStageMarkMemo(Row);
end;

procedure TBookmarkFrame.RefreshMarkMemo;
begin
  AssignedBillsMarkMemo(zgBillsBmk.CurRow);
  AssignedStageMarkMemo(zgStageBmk.CurRow);
end;

procedure TBookmarkFrame.mBillsMarkMemoExit(Sender: TObject);
var
  Rec: TsdDataRecord;
begin
  if zgBillsBmk.CurRow = 0 then Exit;
  Rec := saBillsBMK.DataView.Records[zgBillsBmk.CurRow - 1];
  if Assigned(Rec) then
    Rec.ValueByName('MarkMemo').AsString := GetMemoText(mBillsMarkMemo);
end;

procedure TBookmarkFrame.mStageMarkMemoExit(Sender: TObject);
var
  Rec, StageRec: TsdDataRecord;
begin
  if zgStageBmk.CurRow = 0 then Exit;
  Rec := saStageBMK.DataView.Records[zgStageBmk.CurRow - 1];
  if Assigned(Rec) then
  begin
    StageRec := FProjectData.PhaseData.StageData.StageRecord(Rec.ValueByName('ID').AsInteger);
    StageRec.ValueByName('MarkMemo').AsString := GetMemoText(mStageMarkMemo);
  end;
end;

procedure TBookmarkFrame.SetBMType(const Value: TBookmarkType);
begin
  FBMType := Value;
  jpsBookmark.ActivePageIndex := Integer(FBMType);
end;

procedure TBookmarkFrame.actnLocateBookmarkUpdate(Sender: TObject);
begin
  if FBMType = bmtCompile then
    TAction(Sender).Caption := '定位至台账分解'
  else if FBMType = bmtMeasure then
    TAction(Sender).Caption := '定位至计量台账';
end;

procedure TBookmarkFrame.actnLocateBookmarkExecute(Sender: TObject);
begin
  if FBMType = bmtCompile then
    LocateLedgerBookmark
  else if FBMType = bmtMeasure then
    LocateMeasureBookmark;
end;

procedure TBookmarkFrame.LocateLedgerBookmark;
var
  Rec: TsdDataRecord;
begin
  Rec := saBillsBMK.DataView.Current;
  TProjectData(FProjectData).BillsCompileData.sdvBillsCompile.LocateInControl(Rec);
end;

procedure TBookmarkFrame.LocateMeasureBookmark;
var
  Rec: TsdDataRecord;
begin
  Rec := saStageBMK.DataView.Current;
  TProjectData(FProjectData).BillsMeasureData.sdvBillsMeasure.LocateInControl(Rec);
end;

procedure TBookmarkFrame.zgBillsBmkMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmBookmark.PopupFromCursorPos;
end;

procedure TBookmarkFrame.dxpmBookmarkPopup(Sender: TObject);
begin
  SetDxBtnAction(actnLocateBookmark, MainForm.dxbtnLocateBookmark);
end;

end.
