unit uNodeUpDown;

interface

  uses
  System.SysUtils, System.Classes,vcl.dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
  FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQLDef, FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
  FireDAC.Phys.MySQL, Data.DB, FireDAC.Comp.Client,Vcl.ComCtrls,uTreeView,uDBConnectionBPL,uSQLQueries;

type TNodeUpDown=class(TComponent)

 private

 FDBTReeView:TDBTreeView;
 FDBConnection:TDBConnectionBPL;

 FSQLQueries:TSQLQueries;

 procedure UpdateStateIndex(Node:TTreeNode);

 public

  constructor Create(AOwner:TComponent); override;
  destructor Destroy; override;

property DBTReeView:TDBTReeView read FDBTReeView write FDBTReeView;

  procedure NodeUp;
  procedure NodeDown;


end;



implementation

{ NodeUpDown }

//-------------------------Конструктор, деструктор--------------------------------------------------
constructor TNodeUpDown.Create(AOwner: TComponent);
begin
inherited;
 FDBConnection:=TDBConnectionBPL.Create(Self);
 FSQLQueries:=TSQLQueries.Create(Self);
end;

destructor TNodeUpDown.Destroy;
begin
FreeAndNil(FDBConnection);
FreeAndNil(FSQLQueries);
  inherited;
end;
//--------------------------------------------------------------------------------------------------


procedure TNodeUpDown.NodeDown;

//Процедура опускания узла дерева
var CurrentNode,AfterNode:TTreeNode;
    DBreeViewLocal:TDBTreeView;
    qEditIndex:TFDQuery;

begin

   qEditIndex:=TFDQuery.Create(Self);
   qEditIndex.Connection:=FDBConnection.FDConnection;

   qEditIndex.SQL.Text:=FSQLQueries.UpdateIndex.Text;
   //'UPDATE `treeview_db`.`tree` SET `Index`=:Index WHERE `id`=:id;';

   DBreeViewLocal:=FDBTReeView; // << Переприсваиваем ссылку

  if (DBreeViewLocal.Selected=nil) or (DBreeViewLocal.Selected.getNextSibling=nil) then exit;

   CurrentNode:=DBreeViewLocal.Selected;
   AfterNode:=CurrentNode.GetNextChild(CurrentNode);
   DBreeViewLocal.Items.BeginUpdate;
   AfterNode.MoveTo(CurrentNode,naInsert);

   DBreeViewLocal.Items.EndUpdate;

//Держим фокус
   DBreeViewLocal.SetFocus;
   CurrentNode.Selected:=true;

  with qEditIndex.Params do
  begin
    ParamValues['Index']:=CurrentNode.AbsoluteIndex;
    ParamValues['id']:=Integer(CurrentNode.Data^);
  end;
   qEditIndex.ExecSQL;

    with qEditIndex.Params do
  begin
    ParamValues['Index']:=AfterNode.AbsoluteIndex;
    ParamValues['id']:=Integer(AfterNode.Data^);
  end;
  qEditIndex.ExecSQL;

//Обновление картинок-галочек
UpdateStateIndex(CurrentNode);
UpdateStateIndex(AfterNode);

//Освобождение
FreeAndNil(qEditIndex);

end;

//--------------------------------Процедура поднятия узла дерева------------------------------------
procedure TNodeUpDown.NodeUp;

var CurrentNode,BeforeNode:TTreeNode;
    DBTreeView:TDBTreeView;
    qEditIndex:TFDQuery;

begin


   // Создание

   qEditIndex:=TFDQuery.Create(Self);
   qEditIndex.Connection:=FDBConnection.FDConnection;
   qEditIndex.SQL.Text:=FSQLQueries.UpdateIndex.Text;
//   'UPDATE `treeview_db`.`tree` SET `Index`=:Index WHERE `id`=:id;';


   DBtreeView:=FDBTReeView; // << Переприсваиваем ссылку

                       {Собственно алгоритм перестановки узла}

  if (DBTreeView.Selected=nil) or (DBTreeView.Selected.getPrevSibling=nil) then exit;

   CurrentNode:=DBTreeView.Selected;
   BeforeNode:=CurrentNode.GetPrevChild(CurrentNode);
//   TreeView.Items.BeginUpdate;
   CurrentNode.MoveTo(BeforeNode,naInsert);
//   TreeView.Items.EndUpdate;

   DBTreeView.SetFocus;
   CurrentNode.Selected:=true;

  with qEditIndex.Params do
  begin
    ParamValues['Index']:=CurrentNode.AbsoluteIndex;
    ParamValues['id']:=Integer(CurrentNode.Data^);
  end;
   qEditIndex.ExecSQL;

    with qEditIndex.Params do
  begin
    ParamValues['Index']:=BeforeNode.AbsoluteIndex;
    ParamValues['id']:=Integer(BeforeNode.Data^);
  end;
  qEditIndex.ExecSQL;


//Обновление картинки StateIndex после перемещения
//CheckBoxSelection(CurrentNode,TreeView);


UpdateStateIndex(CurrentNode);
UpdateStateIndex(BeforeNode);



{
CheckBoxSelection(BeforeNode,TreeView);
if BeforeNode.StateIndex=2 then BeforeNode.StateIndex:=1
 else BeforeNode.StateIndex:=2;
}
//


//Уничтожение

FreeAndNil(qEditIndex);

end;


//-----------------------Обновление иконки в дереве-------------------------------------------------

procedure TNodeUpDown.UpdateStateIndex(Node:TTreeNode);
var i:integer;
begin

       if Node.StateIndex=2 then

       begin
       Node.StateIndex:=1;
       Node.StateIndex:=2;
       end else

       begin

       Node.StateIndex:=2;
       Node.StateIndex:=1;
       end;


      if Node.HasChildren then
       begin

       for i := 0 to Node.Count-1 do
       UpdateStateIndex(Node[i]);

       end;


end;

end.

