unit U_ModuleNote;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls, cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxTimeEdit, cxCalendar;

type
  TfrmModuleNote = class(TForm)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    V1Name: TcxGridDBColumn;
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    TBEdit: TToolButton;
    V1OrderNo: TcxGridDBColumn;
    Panel1: TPanel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label2: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure V1NamePropertiesEditValueChanged(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    flag:string;
    { Public declarations }
  end;

var
  frmModuleNote: TfrmModuleNote;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmModuleNote.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmModuleNote.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from SY_Module_Note A where A.MNType='''+flag+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmModuleNote.TBAddClick(Sender: TObject);
begin
  TV1.OptionsData.Editing:=True;
  TV1.OptionsSelection.CellSelect:=True;
  with ClientDataSet1 do
  begin
    Append;
    FieldByName('MNDate').Value:=Now;
    Post;
  end;

end;
procedure TfrmModuleNote.TBDelClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if (Trim(ClientDataSet1.FieldByName('MNID').AsString)<>'') then
  begin
    if application.MessageBox('确定要删除吗?','提示信息',1)=2 then exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SY_Module_Note where MNID='''+Trim(ClientDataSet1.fieldbyname('MNID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet1.Delete;
end;

procedure TfrmModuleNote.TBCloseClick(Sender: TObject);
begin
  ModalResult:=2;
  WriteCxGrid('自定义'+Trim(flag),TV1,'模块说明');
  Close;
end;

procedure TfrmModuleNote.FormShow(Sender: TObject);
var
  fsj,fsj1:string;
begin
  InitGrid();
  ReadCxGrid('自定义'+Trim(flag),TV1,'模块说明');
  frmModuleNote.Caption:=Trim(flag);
end;

procedure TfrmModuleNote.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid('自定义'+Trim(flag),TV1,'模块说明');
  ModalResult:=1;
end;

procedure TfrmModuleNote.TBEditClick(Sender: TObject);
begin
  TV1.OptionsData.Editing:=True;
  TV1.OptionsSelection.CellSelect:=True;
end;

procedure TfrmModuleNote.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if TV1.OptionsData.Editing=False then
  begin
    ModalResult:=1;
  end;
end;

procedure TfrmModuleNote.V1NamePropertiesEditValueChanged(Sender: TObject);
var
  maxno,mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    //Application.MessageBox('名称不能为空！','提示',0);
    Exit;
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('MNNote').Value:=Trim(mvalue);
    //Post;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(ClientDataSet1.FieldByName('MNID').AsString)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'SY','SY_Module_Note',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        //ClientDataSet1.EnableControls;
        Application.MessageBox('取最大编号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(ClientDataSet1.fieldbyname('MNID').AsString);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.add('select * from SY_Module_Note ');
      sql.Add(' where MNID='''+Trim(ClientDataSet1.fieldbyname('MNID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(ClientDataSet1.fieldbyname('MNID').AsString)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillTime').Value:=Now;
      end
      else begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=Now;
      end;
      FieldByName('MNDate').Value:=ClientDataSet1.fieldbyname('MNDate').Value;
      FieldByName('MNID').Value:=Trim(maxno);
      FieldByName('MNNote').Value:=ClientDataSet1.fieldbyname('MNNote').AsString;
      FieldByName('MNType').Value:=flag;
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.Edit;
    ClientDataSet1.FieldByName('MNID').Value:=Trim(maxno);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

end.
