unit U_KqGroupHelp;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxPC, cxControls, ImgList, ComCtrls, ToolWin, StdCtrls, ExtCtrls,
   DB, ADODB, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses,
  cxGridCustomView, cxGrid, cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL,
  cxTLData;
type
PMyRec = ^TMyRec;
 TMyRec = record
    wbcode: string;
    Tvtem_str: string;
end;
type
  TfrmKqGroupHelp = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    ImageList24: TImageList;
    ADOQueryTmp: TADOQuery;
    ADOConnection1: TADOConnection;
    ADOQueryHelp: TADOQuery;
    DS_Help: TDataSource;
    tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    tv1GroupNo: TcxGridDBColumn;
    tv1GroupName: TcxGridDBColumn;
    ToolBar1: TToolBar;
    BtnOk: TToolButton;
    BtnCancel: TToolButton;
    tv1Name: TcxGridDBColumn;
    DataSource1: TDataSource;
    ADOQueryChild: TADOQuery;
    tv1Column1: TcxGridDBColumn;
    tv1Column2: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    v2Name: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    LoadFlag:Boolean;
    fSqlStr:string;
    procedure InitTree();
    procedure InitGrid(mSql:string);
  public
  end;

var
  frmKqGroupHelp: TfrmKqGroupHelp;

implementation
uses
  U_global;
{$R *.dfm}

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

  fSqlStr:='select a.GroupNo,a.GroupName,a.WorkBeg,a.WorkEnd,B.Name,a.KqDept '+
           'from Mc_KqGroup A,XC_CustCode B  where B.Code=A.kqdept and B.flag=''kqdept'' ';
  InitTree();
  cxGrid1.Align :=alClient;
  InitGrid(fSqlStr);
end;


procedure TfrmKqGroupHelp.BtnCancelClick(Sender: TObject);
begin
  close;
end;
procedure TfrmKqGroupHelp.InitTree();
var
  fsj:String;
begin
  fsj:=' select Code,name '+
       ' from XC_CustCode A  '+
       ' where A.Flag=''KQDept'' and A.Valid=''Y'' ';

  with ADOQueryChild do
  begin
    Close;
    sql.Clear;
    sql.Add(fsj);
    Open;
  end;
end;

procedure TfrmKqGroupHelp.BtnOkClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
  ModalResult := 1;
end;
procedure TfrmKqGroupHelp.InitGrid(mSql:string);
begin
  try
    with ADOQueryHelp do
    begin
      close;
      sql.Clear ;
      sql.Add(mSql);
      sql.Add('order by KQDept');
      Open;
    end;
  finally
  end;
end;
procedure TfrmKqGroupHelp.Edit1Change(Sender: TObject);
var
  TmpSql:string;
begin
   TmpSql := 'select a.GroupNo,a.GroupName,a.WorkBeg,a.WorkEnd,B.Name,a.KQDept from Mc_KqGroup A,XC_CustCode B  where B.Code=A.kqdept and B.flag=''kqdept'' '
              + ' and (A.GroupNo Like '+quotedStr('%'+trim(edit1.Text)+'%')+
                ' or A.GroupName Like '+quotedStr('%'+trim(edit1.Text)+'%')+
                ' )';
    InitGrid(TmpSql);
end;

procedure TfrmKqGroupHelp.tv1DblClick(Sender: TObject);
begin
  BtnOk.Click ;
end;

procedure TfrmKqGroupHelp.Tv2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fSqlStr:string;
begin
   if ADOQueryHelp.Active then
   begin
     if Trim(ADOQueryChild.fieldbyname('Code').AsString)<>'' then
     begin
       fSqlStr:=' KqDept='+quotedStr(Trim(ADOQueryChild.fieldbyname('Code').AsString));
       try
         ADOQueryHelp.DisableControls;
         with ADOQueryHelp do
         begin
           if Trim(fSqlStr)='' then
           begin
             Filtered:=False;
           end else
           begin
             Filtered:=False;
             Filter:=fSqlStr;
             Filtered:=True;
           end;
         end;
       finally
         ADOQueryHelp.EnableControls;
       end;
     end;
   end;

end;

end.
