unit U_SYDeptUserView;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, ExtCtrls, DBClient, cxFilter, cxData, cxDataStorage, cxEdit,
  cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, cxTextEdit,
  cxCheckBox;

type
  TfrmSYDeptUserView = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_User: TClientDataSet;
    DSUser: TDataSource;
    v4Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure DPNameKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure Tv4DblClick(Sender: TObject);
  private
    { Private declarations }
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitTree();

  public
    { Public declarations }
  end;

var
  frmSYDeptUserView: TfrmSYDeptUserView;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmSYDeptUserView.InitTree();
var
  i:Integer;
begin
  try
    ADOQueryTree.DisableControls;
    with ADOQueryTree do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from SY_Dept order by DPlevel,DPOrder,DPName');
      Open;
    end;
    SCreateCDS20(ADOQueryTree,CDS_Tree);
    SInitCDSData20(ADOQueryTree,CDS_Tree);
    //cxDBTreeList1.Items[0].Expand(false);
    cxDBTreeList1.Items[0].Expand(True);
  finally
    ADOQueryTree.EnableControls;
  end;
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmSYDeptUserView.FormDestroy(Sender: TObject);
begin
  frmSYDeptUserView:=nil;
end;

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

procedure TfrmSYDeptUserView.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmSYDeptUserView.DPNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
    DPNo.SetFocus;
end;

procedure TfrmSYDeptUserView.FormShow(Sender: TObject);
begin
  InitTree();
end;

procedure TfrmSYDeptUserView.cxDBTreeList1DblClick(Sender: TObject);
begin
  if CDS_Tree.FieldByName('DPLevel').Value=0 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select A.*,Dept=(select DPName from SY_Dept where DPID=A.DPID) ');
      SQL.Add(' from SY_User A where UserId not in(''Admin'',''Test'') ');
      SQL.Add(' and Valid=''Y'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_User);
    SInitCDSData20(ADOQueryTemp,CDS_User);
  end else
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Select_Dept_User :CYType');
      Parameters.ParamByName('CYType').Value:=Trim(CDS_Tree.fieldbyname('DPID').AsString);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_User);
    SInitCDSData20(ADOQueryTemp,CDS_User);
  end;
end;

procedure TfrmSYDeptUserView.Tv4DblClick(Sender: TObject);
begin
  ModalResult:=1;
end;

end.
