unit ZJJLFme;

interface

uses
  ZJJLDm,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, DBCtrls, Mask, JimLabels, DB, ComCtrls,
  ToolWin, ActnList, ZjGridDBA, ZJGrid, dxBar;

type
  TZJJLFrame = class(TFrame)
    labTitle: TJimGradLabel;
    alBGL: TActionList;
    actnGenerate: TAction;
    pnlTop: TPanel;
    pnlButton: TPanel;
    tobaTop: TToolBar;
    tobtnGenerate: TToolButton;
    lePreText: TLabeledEdit;
    zaZJJL: TZjGridDBA;
    pnlZJJL: TPanel;
    zgZJJL: TZJGrid;
    pnlListTitle: TPanel;
    pnlListBar: TPanel;
    lblListTitle: TLabel;
    pnlZJJLProperty: TPanel;
    pnlDataTitle: TPanel;
    lblDataTiel: TLabel;
    pnlDataBar: TPanel;
    dxpmZJJL: TdxBarPopupMenu;
    actnLocateBills: TAction;
    dxpmAutoGenerate: TdxBarPopupMenu;
    actnGenerateFxZJJL: TAction;
    actnGenerateGclZJJL: TAction;
    procedure actnGenerateExecute(Sender: TObject);
    procedure lePreTextExit(Sender: TObject);
    procedure zgZJJLMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure dxpmZJJLPopup(Sender: TObject);
    procedure actnLocateBillsExecute(Sender: TObject);
    procedure actnLocateBillsUpdate(Sender: TObject);
    procedure dxpmAutoGeneratePopup(Sender: TObject);
    procedure actnGenerateFxZJJLExecute(Sender: TObject);
    procedure actnGenerateGclZJJLExecute(Sender: TObject);
    procedure actnGenerateUpdate(Sender: TObject);
  private
    FZJJLData: TZJJLData;
    FDataReadOnly: Boolean;
    procedure SetDataReadOnly(const Value: Boolean);

    procedure RefreshTitle;
    procedure GenerateZJJL;
  public
    constructor Create(AParent: TFrame; AZJJLData: TZJJLData);
    destructor Destroy; override;

    procedure ResetFrameLink(AZJJLData: TZJJLData);

    property DataReadOnly: Boolean read FDataReadOnly write SetDataReadOnly;
  end;

implementation

uses
  MergeTextFrm, PhaseData, MainFrm, ProjectData, UtilMethods,
  PhaseProperty;

{$R *.dfm}

{ TZJJLFrame }

constructor TZJJLFrame.Create(AParent: TFrame; AZJJLData: TZJJLData);
begin
  inherited Create(AParent);
  ResetFrameLink(AZJJLData);
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
    lePreText.Text := ProjProperties.ZJJLPreText;
  RefreshTitle;
end;

destructor TZJJLFrame.Destroy;
begin
  inherited;
end;

procedure TZJJLFrame.ResetFrameLink(AZJJLData: TZJJLData);
begin
  FZJJLData := AZJJLData;
  zaZJJL.DataSet := FZJJLData.cdsZJJLView;
  AlignControl(FZJJLData.DetailGrid, pnlZJJLProperty, alClient);
end;

procedure TZJJLFrame.actnGenerateExecute(Sender: TObject);
var
  bIsRefer: Boolean; 
  P: TPoint;
begin
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
    bIsRefer := ProjProperties.AuditStatus = 0;
  if bIsRefer then
  begin
    P := pnlButton.ClientToScreen(Point(tobtnGenerate.Left, tobtnGenerate.Top + tobtnGenerate.Height));
    dxpmAutoGenerate.Popup(P.X, P.Y);
  end
  else
    GenerateZJJL;
end;

procedure TZJJLFrame.lePreTextExit(Sender: TObject);
begin
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
    ProjProperties.ZJJLPreText := lePreText.Text;
end;

procedure TZJJLFrame.SetDataReadOnly(const Value: Boolean);
begin
  FDataReadOnly := Value;
  actnGenerate.Enabled := not FDataReadOnly;
  lePreText.Enabled := not FDataReadOnly;
  zaZJJL.Column('Code').ReadOnly := FDataReadOnly;
  zaZJJL.Column('CertificateCode').ReadOnly := FDataReadOnly;
end;

procedure TZJJLFrame.zgZJJLMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmZJJL.PopupFromCursorPos
  else if (ssDouble in Shift) and (zgZJJL.CurCol = 3) then
  begin
    if FZJJLData.cdsZJJLView.RecordCount > 0 then
      FZJJLData.LocateBills;
  end;
end;

procedure TZJJLFrame.dxpmZJJLPopup(Sender: TObject);
begin
  SetDxBtnAction(actnLocateBills, MainForm.dxbtnLocateBills);
end;

procedure TZJJLFrame.actnLocateBillsExecute(Sender: TObject);
begin
  FZJJLData.LocateBills;
end;

procedure TZJJLFrame.actnLocateBillsUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := FZJJLData.cdsZJJLView.RecordCount > 0;
end;

procedure TZJJLFrame.RefreshTitle;
var
  iType: Integer;
begin
  if not TPhaseData(FZJJLData.PhaseData).Active then Exit;
  iType := TPhaseData(FZJJLData.PhaseData).PhaseProperty.ZJJLType;
  case iType of
    0: labTitle.Caption := '中间计量(0号台账)';
    1: labTitle.Caption := '中间计量(总量控制)';
  end;
end;

procedure TZJJLFrame.dxpmAutoGeneratePopup(Sender: TObject);
begin
  SetDxBtnAction(actnGenerateFxZJJL, MainForm.dxbtnFxZJJL);
  SetDxBtnAction(actnGenerateGclZJJL, MainForm.dxbtnGclZJJL);
end;

procedure TZJJLFrame.actnGenerateFxZJJLExecute(Sender: TObject);
begin
  with TPhaseData(FZJJLData.PhaseData).PhaseProperty do
  begin
    if (ZJJLType = 0) or QuestMessage('当前为“总量控制”格式，切换至“0号台账”将清空数据并重新生成，确定要切换吗？') then
    begin
      ZJJLType := 0;
      GenerateZJJL;
    end;
  end;
end;

procedure TZJJLFrame.GenerateZJJL;
var
  iType: Integer;
begin
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
    ProjProperties.ZJJLPreText := lePreText.Text;

  iType := TPhaseData(FZJJLData.PhaseData).PhaseProperty.ZJJLType;
  case iType of
    0: FZJJLData.GenerateAll;
    1: FZJJLData.GenerateAllByB_Code;
  end;
  FZJJLData.AssignedCurData;
  RefreshTitle;
end;

procedure TZJJLFrame.actnGenerateGclZJJLExecute(Sender: TObject);
begin
  with TPhaseData(FZJJLData.PhaseData).PhaseProperty do
  begin
    if (ZJJLType = 1) or (QuestMessage('当前为“0号台账”格式，切换至“总量控制”将清空数据并重新生成，确定要切换吗？')) then
    begin
      ZJJLType := 1;
      GenerateZJJL;
    end;
  end;
end;

procedure TZJJLFrame.actnGenerateUpdate(Sender: TObject);
begin
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
    TAction(Sender).Enabled := not StageDataReadOnly;
end;

end.
