unit Unit19;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DBGridEhGrouping, ToolCtrlsEh,
  DBGridEhToolCtrls, DynVarsEh, Data.DB, Vcl.ComCtrls, Vcl.ToolWin,
  Vcl.ExtCtrls, EhLibVCL, GridsEh, DBAxisGridsEh, DBGridEh, Vcl.StdCtrls,
  Data.Win.ADODB, Vcl.Menus, ListUtils;

type
  TfValue = class(TForm)
    DBGridEh1: TDBGridEh;
    pTop: TPanel;
    tbMain: TToolBar;
    tbAdd: TToolButton;
    tbEdit: TToolButton;
    tbDelete: TToolButton;
    dsValue: TDataSource;
    TV: TTreeView;
    Splitter1: TSplitter;
    qValue: TADOQuery;
    qValueid: TAutoIncField;
    qValuedeviceType_id: TSmallintField;
    qValuevendor: TWideStringField;
    qValuename: TWideStringField;
    PopupMenu: TPopupMenu;
    N1: TMenuItem;
    qDevType: TADOQuery;
    qDevTypeid: TAutoIncField;
    qDevTypename: TWideStringField;
    qDevTypeparent_id: TIntegerField;
    qValueisFolder: TSmallintField;
    qValuequantityIn: TFMTBCDField;
    qValuequantityOut: TFMTBCDField;
    qValuetotal: TFMTBCDField;
    qRequest: TADOQuery;
    qRequestid: TAutoIncField;
    qRequestname: TWideStringField;
    qRequestparent_id: TIntegerField;
    procedure tbEditClick(Sender: TObject);
    procedure tbDeleteClick(Sender: TObject);
    procedure tbAddClick(Sender: TObject);
    procedure TVDblClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ExpandLevel( Node : TTreeNode);
    procedure TVExpanding(Sender: TObject; Node: TTreeNode; var AllowExpansion: Boolean);
    procedure TVChange(Sender: TObject; Node: TTreeNode);
    procedure FormShow(Sender: TObject);
    Procedure RebuildTreeCompanies;
  private
    List : TLists;
    GroupID : integer;
    function getChildren(ids: string): string;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fValue: TfValue;

implementation

{$R *.dfm}

uses Unit2, Unit20, Unit1;

procedure TfValue.RebuildTreeCompanies;
begin
    TV.Items.Clear;
    ExpandLevel(nil);
    TV.Selected:=TV.Items[0];
end;

procedure TfValue.ExpandLevel( Node : TTreeNode);
   var
      id, i   : Integer;
      TreeNode : TTreeNode;
begin
    if Node = nil then id := 0 else id := integer(Node.Data);
    qDevType.Close;
    qDevType.Parameters[0].Value := id;
    qDevType.Open;
    TV.Items.BeginUpdate;
    for i := 1 to qDevType.RecordCount do begin
       TreeNode := TV.Items.AddChildObject(Node, qDevTypename.AsString, Pointer(qDevTypeid.AsInteger));
       TreeNode.ImageIndex := 12;
       TreeNode.SelectedIndex := 12;
       TV.Items.AddChildObject(TreeNode,'',nil);
       qDevType.Next;
    end;
    TV.Items.EndUpdate;
end;

procedure TfValue.FormShow(Sender: TObject);
begin
   GroupID := 0;
   List:=Tlists.Create(TListsItem);
   RebuildTreeCompanies;
end;

procedure TfValue.N1Click(Sender: TObject);
begin
   qValue.Close;
   qValue.SQL.Strings[37] := '';
   qValue.Open;
   List:=Tlists.Create(TListsItem);
   RebuildTreeCompanies;
end;

procedure TfValue.tbAddClick(Sender: TObject);
begin
  qValue.Append;
  if GroupID > 0 then
     qValuedeviceType_id.AsInteger := GroupID;
  if fValueEdit.ShowModal = mrOk then begin
     if qValue.Modified then
        qValue.Post;
   end;
   qValue.Cancel;
   qValue.Requery();
   qValue.Last;
end;

procedure TfValue.tbDeleteClick(Sender: TObject);
begin
  if Application.MessageBox('Удалить запись?', 'Подтверждение', MB_ICONQUESTION+MB_YESNO) = idYes then
     qValue.Delete;
end;

procedure TfValue.tbEditClick(Sender: TObject);
  var
    id:integer;
begin
  id := qValue.RecNo;
  qValue.Edit;
  if fValueEdit.ShowModal = mrOk then begin
     if qValue.Modified then
        qValue.Post;
  end;
  qValue.Cancel;
  qValue.Requery();
  qValue.MoveBy(id-1);
end;

procedure TfValue.TVChange(Sender: TObject; Node: TTreeNode);
   var
      id : integer;
begin
   if TV.Selected <> nil then begin
      id := Integer(TV.Selected.Data);
      qDevType.Close;
      qDevType.Parameters[0].Value := id;
      qDevType.Open;
   end;
end;

function TfValue.getChildren(ids: string): string;
   var
      chilren : string;
begin
   if qRequest.Active then
      qRequest.Close;
   qRequest.SQL.Text := 'SELECT * FROM devtype WHERE parent_id IN(' + ids + ')';
   qRequest.Open;
   if qRequest.RecordCount > 0 then begin
      qRequest.First;
      while not qRequest.Eof do begin
         chilren := chilren + ',' + qRequestid.AsString;
         qRequest.Next;
      end;
   end;
   qRequest.Close;
   result := chilren;
end;

procedure TfValue.TVDblClick(Sender: TObject);
   var
      chilren : string;
begin
   GroupID := Integer(TV.Selected.Data);
   chilren := IntToStr(Integer(TV.Selected.Data)) + getChildren(IntToStr(Integer(TV.Selected.Data)));
   chilren := chilren + getChildren(chilren);
   qValue.Close;
   qValue.SQL.Strings[37] := 'WHERE deviceType_id IN(' + chilren + ')';
   qValue.Open;
end;

procedure TfValue.TVExpanding(Sender: TObject; Node: TTreeNode; var AllowExpansion: Boolean);
begin
   if Node.getFirstChild.Data = nil then begin
      Node.DeleteChildren;
      ExpandLevel(Node);
   end;
end;

end.
