unit U_GlideHelp;

interface

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

type
  TfrmGlideHelp = class(TForm)
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    ADOQuery1GlideNo: TStringField;
    ADOQuery1GlideName: TStringField;
    ADOQuery1GlidePara: TStringField;
    ADOQuery1GlideNote: TStringField;
    AD_sub: TADODataSet;
    Ds_sub: TDataSource;
    AD_GY_Fllb: TADODataSet;
    Ds_Gy_Fllb: TDataSource;
    Splitter2: TSplitter;
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    ToolBar1: TToolBar;
    BtnFind: TToolButton;
    BtnOk: TToolButton;
    BtnClose: TToolButton;
    TreeView1: TTreeView;
    Panel2: TPanel;
    Splitter1: TSplitter;
    Panel5: TPanel;
    dbGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    dbGrid1Level1: TcxGridLevel;
    tv1glideNo: TcxGridDBColumn;
    tv1glideName: TcxGridDBColumn;
    tv1glidePara: TcxGridDBColumn;
    tv1glideNote: TcxGridDBColumn;
    Panel3: TPanel;
    Panel6: TPanel;
    DG_Sub: TDBGrid;
    ADOConnection1: TADOConnection;
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnCloseClick(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure BtnFindClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure DG_SubDblClick(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
  private
    { Private declarations }
    load_flag :Boolean;
    this_code :string;
    procedure DoQuery();
    procedure IntiTree();
    procedure DoFilter(mDept:string);
  public
    { Public declarations }
  end;

var
  frmGlideHelp: TfrmGlideHelp;

implementation

{$R *.dfm}

uses
  U_global;

type
PMyRec = ^TMyRec;
TMyRec = record
  wbcode: string;
  Tvtem_str: string;
end;

procedure TfrmGlideHelp.FormCreate(Sender: TObject);
begin
   load_flag := False;
   //inti_node();
   IntiTree();
   load_flag := True;
   Panel2.Align := AlClient;
   DoQuery();
   with AD_Sub do
   begin
      Disablecontrols;
      Active:=False;
      Active:=True;
      Enablecontrols;
   end;

end;

procedure TfrmGlideHelp.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    BtnFindClick(Sender);
end;

procedure TfrmGlideHelp.BtnCloseClick(Sender: TObject);
begin
   Close;
end;

procedure TfrmGlideHelp.BtnOkClick(Sender: TObject);
var
   i :integer;
begin
  if AD_sub.IsEmpty then
  begin
    application.MessageBox('请选择您要的工序!','提示信息',0);
    exit;
  end;
  ModalResult:=1;
end;

procedure TfrmGlideHelp.BtnFindClick(Sender: TObject);
begin
  DoFilter('');
end;

procedure TfrmGlideHelp.DBGrid1DblClick(Sender: TObject);
var
   i :integer;
begin
   if not ADOQuery1.Active then exit;
   if ADOQuery1.RecordCount = 0 then exit;
   with Ad_Sub,dg_Sub do
   begin
      DisableControls;
      SetFocus;
      append;
      for i := 0 to Columns.Count -1 do
      begin
         Columns[i].Field.ReadOnly := False;
         Fieldvalues[Columns[i].FieldName] := ADOQuery1.Fieldbyname(Columns[i].FieldName).Value;
      end;
      SelectedField.FocusControl;
      update();
      EnableControls;
   end;
    Ad_Sub.Post;
   dg_Sub.Refresh;
end;

procedure TfrmGlideHelp.DG_SubDblClick(Sender: TObject);
begin
   if  Ad_Sub.IsEmpty  then exit;
   try
      dg_Sub.SetFocus;
      Ad_Sub.Delete;
   except
      //
   end;
end;

procedure TfrmGlideHelp.IntiTree();
var
   MyRecPtr: PMyRec;
   tmpstr :string;
begin
  try
  	New(MyRecPtr);
  	MyRecPtr^.wbcode := 'R';
  	MyRecPtr^.Tvtem_str := '全部';
  	with TreeView1 do
  	begin
      Items.Clear;
      Items.AddObject(nil, MyRecPtr^.Tvtem_str, MyRecPtr);
  	end;
  	with TreeView1,AD_GY_Fllb do
  	begin
      tmpstr := 'select distinct isnull(glideDept,''其它'') as glideDept,'+
                'isnull(glideDeptName,''其它'') as glideDeptName  from V_MC_glide';
      tmpstr := tmpstr +' order by glidedept';
      active := false;
      CommandText := tmpstr;
      active := true;
      if recordcount = 0 then exit;
      first;
      while not Eof do
      begin
  			New(MyRecPtr);
  			MyRecPtr^.wbcode := trim(fieldbyname('glidedept').Asstring);
        MyRecPtr^.Tvtem_str := trim(fieldbyname('glideDeptName').Asstring);
        Items.AddChildObject(Items[0],MyRecPtr^.Tvtem_str, MyRecPtr);
        Next;
      end;
      close;
      Items[0].Selected := True;
      Items[0].Expand(true);
   end;
  except
    //
  end;

end;


procedure TfrmGlideHelp.TreeView1Click(Sender: TObject);
begin
   try
      this_code := trim(PMyRec(TreeView1.Selected.Data).wbcode);
      Dofilter(this_code);
   except
      //
   end;
end;

procedure TfrmGlideHelp.tv1DblClick(Sender: TObject);
var
   i :integer;
begin
   if  ADOQuery1.IsEmpty  then exit;
   with Ad_Sub,dg_Sub do
   begin
      DisableControls;
      SetFocus;
      append;
      for i := 0 to Columns.Count -1 do
      begin
         Columns[i].Field.ReadOnly := False;
         Fieldvalues[Columns[i].FieldName] := ADOQuery1.Fieldbyname(Columns[i].FieldName).Value;
      end;
      SelectedField.FocusControl;
      update();
      EnableControls;
   end;
    Ad_Sub.Post;
   dg_Sub.Refresh;
end;
////////////////////////////////////////////////////////////
  //
////////////////////////////////////////////////////////////
procedure TfrmGlideHelp.DoQuery();
begin
  try
    ADOQuery1.DisableControls ;
    with ADOQuery1 do
    begin
      close;
      sql.clear;
      filtered:=false;
      sql.Add('select *,isnull(glideDept,''其它'') as dept from V_MC_glide');
      sql.Add('order By glideNo');
      Open;
    end;
  finally
    ADOQuery1.EnableControls ;
  end;
end;
/////////////////////////////////////////
procedure TfrmGlideHelp.DoFilter(mDept:string);
var
  filterStr:string;
begin
  if mDept<>'R' then
    filterStr:=' and Dept= '+quotedStr(trim(mDept));

  if trim(edit1.Text)<>'' then
    filterStr:=filterStr+' and glideName like '+quotedStr('%'+trim(edit1.Text)+'%');

  try
    ADOQuery1.DisableControls ;
    filterStr:=trim(RightBStr(filterStr,length(filterStr)-4));
    with ADOQuery1 do
    begin
      filtered:=false;
      if trim(filterStr)<>'' then
      begin
        filter:=filterStr;
        filtered:=true;
      end;
    end;
  finally
    ADOQuery1.EnableControls ;
  end;
end;

end.
