unit DealBillsFme;

interface

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

type
  TDealBillsFrame = class(TFrame)
    saDealBills: TsdGridDBA;
    pnlDealBills: TPanel;
    zgDealBills: TZJGrid;
    dxpmDealBills: TdxBarPopupMenu;
    alDealBills: TActionList;
    actnChangeDealBillsMode: TAction;
    pnlTop: TPanel;
    labTitle: TJimGradLabel;
    procedure dxpmDealBillsPopup(Sender: TObject);
    procedure zgDealBillsMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure actnChangeDealBillsModeExecute(Sender: TObject);
  private
    FDealBillsData: TDealBillsData;
    FIsEditMode:Boolean;
    FLockedData: Boolean;

    procedure AddCurrentBills;

    procedure SetLockedData(const Value: Boolean);
    procedure SetIsEditMode(const Value: Boolean);
  public
    constructor Create(AParent: TFrame; ADealBillsData: TDealBillsData);

    property LockedData: Boolean read FLockedData write SetLockedData;
    property IsEditMode: Boolean read FIsEditMode write SetIsEditMode;
  end;

implementation

uses
  MainFrm, UtilMethods, ProjectData, BillsCompileDm;

{$R *.dfm}

{ TDealBillsFrame }

constructor TDealBillsFrame.Create(AParent: TFrame;
  ADealBillsData: TDealBillsData);
begin
  inherited Create(AParent);
  FDealBillsData := ADealBillsData;
  saDealBills.DataView := FDealBillsData.sdvDealBills;
  IsEditMode := False;
  LockedData := True;
end;

procedure TDealBillsFrame.SetLockedData(const Value: Boolean);
begin
  FLockedData := Value;
  saDealBills.Columns.ColumnByName('B_Code').ReadOnly := FLockedData;
  saDealBills.Columns.ColumnByName('Name').ReadOnly := FLockedData;
  saDealBills.Columns.ColumnByName('Units').ReadOnly := FLockedData;
  saDealBills.Columns.ColumnByName('Price').ReadOnly := FLockedData;
  saDealBills.Columns.ColumnByName('Quantity').ReadOnly := FLockedData;
  saDealBills.Columns.ColumnByName('TotalPrice').ReadOnly := FLockedData;
end;

procedure TDealBillsFrame.dxpmDealBillsPopup(Sender: TObject);
begin
  SetDxBtnAction(actnChangeDealBillsMode, MainForm.dxbtnChangeDealBillsMode);
end;

procedure TDealBillsFrame.SetIsEditMode(const Value: Boolean);
begin
  FIsEditMode := Value;
  LockedData := not FIsEditMode;
  if FIsEditMode then
  begin
    labTitle.Caption := ' 签约清单 -- 编辑模式（双击单元格修改清单信息）';
    actnChangeDealBillsMode.Caption := '切换至阅读模式';
    actnChangeDealBillsMode.Hint := '阅读模式下可双击添加清单至0号台账';
  end
  else
  begin
    labTitle.Caption := ' 签约清单 -- 阅读模式（双击可添加选中清单至0号台账）';
    actnChangeDealBillsMode.Caption := '切换至编辑模式';
    actnChangeDealBillsMode.Hint := '编辑模式下可双击修改清单信息';
  end;
end;

procedure TDealBillsFrame.zgDealBillsMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmDealBills.PopupFromCursorPos
  else if not FIsEditMode and (Button = mbLeft) and (ssDouble in Shift) then
    AddCurrentBills;
end;

procedure TDealBillsFrame.AddCurrentBills;
begin
  if Assigned(saDealBills.DataView.Current) then
    with TProjectData(FDealBillsData.ProjectData).BillsCompileData do
      AddBillsFromDealBills(saDealBills.DataView.Current);
end;

procedure TDealBillsFrame.actnChangeDealBillsModeExecute(Sender: TObject);
begin
  if IsEditMode or TProjectData(FDealBillsData.ProjectData).CheckPassword then
    IsEditMode := not IsEditMode;
end;

end.
