unit U_GroupStuffHelp;

interface

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

type
  TfrmGroupStuffHelp = class(TForm)
    ToolBar1: TToolBar;
    BtnOk: TToolButton;
    BtnClose: TToolButton;
    ImageList24: TImageList;
    DataSource1: TDataSource;
    ADOQueryHelp: TADOQuery;
    ADOQueryHelpCode: TStringField;
    ADOQueryHelpName: TStringField;
    ADOConnection1: TADOConnection;
    frameLeftHelpTree1: TframeLeftHelpTree;
    cxGrid2: TcxGrid;
    tv2: TcxGridDBTableView;
    tv2code: TcxGridDBColumn;
    tv2Name: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    EditName: TEdit;
    Edit1: TEdit;
    procedure BtnCloseClick(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure frameLeftHelpTree1tt1FocusedRecordChanged(
      Sender: TcxCustomGridTableView; APrevFocusedRecord,
      AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure tv2DblClick(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    procedure InitGrid();
    procedure DoFilter();
  public
    fFrameNo:string;
  end;

var
  frmGroupStuffHelp: TfrmGroupStuffHelp;

implementation
uses
  U_global;
{$R *.dfm}

procedure TfrmGroupStuffHelp.BtnCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmGroupStuffHelp.BtnOkClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty  then
    ModalResult:=-1
  else
    ModalResult:=1;
end;

procedure TfrmGroupStuffHelp.FormCreate(Sender: TObject);
begin
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;


end;
///////////////////////////////////////////////////
  //
///////////////////////////////////////////////////
procedure TfrmGroupStuffHelp.InitGrid();
begin
  try
    ADOQueryHelp.DisableControls ;
    with ADOQueryHelp do
    begin
      close;
      sql.clear;
      filtered:=false;
      sql.Add('select A.* from yc_stuff A');
      if not frameLeftHelpTree1.ADOQueryLTree.IsEmpty then
      begin
        sql.Add('where exists(select * from mc_group_user B where A.code=B.code');
        if trim(frameLeftHelpTree1.fLeftSelValue)<>'' then
          sql.Add('and B.groupNo='+quotedStr(trim(frameLeftHelpTree1.fLeftSelValue)));
        sql.Add(')');
      end
      else
      begin
        if trim(fFrameNO)<>'' then
          sql.Add('where A.dept='+quotedStr(fFrameNO))
        else
          sql.Add('where 1=1');
      end;
      sql.Add('and A.valid=''Y''');
      sql.Add('order by A.dept,A.Name'); 
      Open;
    end;
  finally
    ADOQueryHelp.EnableControls;
  end;
end;

procedure TfrmGroupStuffHelp.frameLeftHelpTree1tt1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  frameLeftHelpTree1.tt1FocusedRecordChanged(Sender,
  APrevFocusedRecord, AFocusedRecord, ANewItemRecordFocusingChanged);
  InitGrid();
end;

procedure TfrmGroupStuffHelp.tv2DblClick(Sender: TObject);
begin
  btnOk.Click ;
end;
//////////////////////////////////////////
  //函数：过滤
//////////////////////////////////////////
procedure TfrmGroupStuffHelp.DoFilter();
var
  mfilter:string;
begin
  if trim(edit1.text)<>'' then
    mfilter:='code like '+quotedStr('%'+trim(edit1.text)+'%');
  if trim(editName.text)<>'' then
    mfilter:='name like '+quotedStr('%'+trim(editName.text)+'%');
  try
    ADOQueryHelp.DisableControls ;
    with ADOQueryHelp do
    begin
      filtered:=false;
      filter:=mfilter;
      filtered:=true;
    end;
  finally
    ADOQueryHelp.EnableControls ;
  end;

end;
procedure TfrmGroupStuffHelp.Edit1Change(Sender: TObject);
begin
  DoFilter();
end;

procedure TfrmGroupStuffHelp.FormShow(Sender: TObject);
begin
  //业务员树
  frameLeftHelpTree1.InitLTree(ADOQueryHelp,'','GROUP','印花部',fFrameNo,true);
  //if trim(fFrameNo)<>'' then
  //  frameLeftHelpTree1.ADOQueryLTree.Locate('code',fFrameNo,[]);
  InitGrid();
end;

end.
