{* Smarty *}

<table>
  <tr>
    <td style="vertical-align: top;">
      <h2>Оформить заявку</h2>
      <div id="demo" class="demo"></div>
    </td>
    <td style="vertical-align: top; padding-top: 60px; padding-left: 20px;">
      <div id="StructureSvt"></div>
    </td>
  </tr>
</table>

{literal}
<script type="text/javascript">

$(function () {
  //alert(e);
	$("#demo")
		.jstree({
			"plugins" : [ "themes", "json_data", "ui", "crrm", "dnd", "types", "hotkeys"],
      "themes" : {
        "theme" : "classic",
        "icons" : false
      },
			"json_data" : {
				"ajax" : {
          "url" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationSelect.ajax.php{literal}",
					"data" : function (n) {
						return {
							"operation" : "get_children",
							"id" : n.attr ? n.attr("id").replace("node_","") : 0
						};
					}
				}
			},
			"types" : {
				"max_depth" : -2,
				"max_children" : -2,
				"valid_children" : [ "default" ],
				"types" : {
					"default" : {
						"valid_children" : "default",
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/file.png{literal}"
						}
					},
					"folder" : {
						"valid_children" : [ "default", "folder" ],
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/folder.png{literal}"
						}
					},
					"drive" : {
						"valid_children" : [ "default", "folder" ],
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/root.png{literal}"
						},
						"start_drag" : false,
						"move_node" : false,
						"delete_node" : false,
						"remove" : false
					}
				}
			},
			"core" : {
				"initially_open" : [ "node_1" ],
        "strings" : {
          "loading" : "Загрузка ...",
          "new_node" : "Новый элемент"
        }
			}
		})
		.bind("create.jstree", function (e, data) {
        $.post(
          "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
          {
            "operation" : "create_node",
            "id" : data.rslt.parent.attr("id").replace("node_",""),
            "position" : data.rslt.position,
            "title" : data.rslt.name,
            "type" : data.rslt.obj.attr("rel")
          },
          function (r) {
            if(r.status) {
              $(data.rslt.obj).attr("id", "node_" + r.id);
            }
            else {
              $.jstree.rollback(data.rlbk);
            }
          }
        );
      //}
		})

		.bind("remove.jstree", function (e, data) {
			data.rslt.obj.each(function () {
				$.ajax({
					async : false,
					type: "POST",
					url : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
					data : {
						"operation" : "remove_node",
						"id" : this.id.replace("node_","")
					},
					success : function (r) {
						if(!r.status) {
							data.inst.refresh();
						}
					}
				});
			});
		})
		.bind("rename.jstree", function (e, data) {
      if('folder' == data.rslt.obj.attr("rel")){
        $.post(
          "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
          {
            "operation" : "rename_node",
            "id" : data.rslt.obj.attr("id").replace("node_",""),
            "title" : data.rslt.new_name
          },
          function (r) {
            if(!r.status) {
              $.jstree.rollback(data.rlbk);
            }
          }
        );
      }
		})
		.bind("move_node.jstree", function (e, data) {
			data.rslt.o.each(function (i) {
				$.ajax({
					async : false,
					type: "POST",
					url : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
					data : {
						"operation" : "move_node",
						"id" : $(this).attr("id").replace("node_",""),
						"ref" : data.rslt.np.attr("id").replace("node_",""),
						"position" : data.rslt.cp + i,
						"title" : data.rslt.name,
						"copy" : data.rslt.cy ? 1 : 0
					},
					success : function (r) {
						if(!r.status) {
							$.jstree.rollback(data.rlbk);
						}
						else {
							$(data.rslt.oc).attr("id", "node_" + r.id);
							if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
								data.inst.refresh(data.inst._get_parent(data.rslt.oc));
							}
						}
					}
				});
			});
		})
});

function StructureSelectionIns1(VarThis) {
  var VarId = $(VarThis).parent('li').attr('id').toString().replace("node_", "");
  //var VarText = $(VarThis).text();
  alert(VarId);
  $.ajax({
    async: false,
    timeout: 3000,
    type: "POST",
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureSvtList.ajax.php{literal}",
    data: {
     "UrlId" : VarId
    },
    //beforeSend: function(){},
    success: function(answ){
      document.getElementById('StructureSvt').innerHTML = answ;
    }
  });
}

function StructureSelectionIns(VarThis) {
  var VarId = $(VarThis).parent('li').attr('id').toString().replace("node_", "");
  $("#StructureSvt").flexOptions({query: VarId}).flexReload();
}

$(function () {
  $("#StructureSvt").flexigrid({
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureSvtList.ajax.php{literal}",
    dataType: "json",
    colModel : [
      {display: "Действие", name : "svt_action", width : 80, sortable : false, align: "left"},
      {display: "Наименование бух.", name : "svt_buxname", width : 200, sortable : true, align: "left"},
      {display: "Тип СВТ", name : "svttype_name", width : 100, sortable : true, align: "left"},
      {display: "Инв. номер", name : "svt_invnum", width : 70, sortable : true, align: "left"},
      {display: "Пользователь", name : "users_fio", width : 80, sortable : true, align: "left"}
    ],
    query: 1,
    sortname: "svt_name",
    sortorder: "asc",
    title: "Свт",
    usepager: true,
    pagetext: "Страница",
    outof: "из",
    findtext: "Искать",
    errormsg: "Нет соединения",
    pagestat: "Отображается с {from} по {to} из {total} записей",
    pagetext: "Страница",
    procmsg: "Идет запрос, подождите пожалуйста ...",
    nomsg: "Нет записей",
    nowrap: false,
    useRp: true,
    rp: 100,
    rpOptions: [100,250,500,1000],
    width: 650,
    height: 400
  });
});

function DemandFormAdd(VarId){
  alert('VarId'+ VarId);
  //var VarId = $(VarThis).parent('li').attr('id').toString().replace("node_", "");
  //var VarText = $(VarThis).text();
  alert(VarId);
  $.ajax({
    async: false,
    timeout: 3000,
    type: "POST",
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureSvtList.ajax.php{literal}",
    data: {
     "UrlId" : VarId
    },
    //beforeSend: function(){},
    success: function(answ){
      document.getElementById('StructureSvt').innerHTML = answ;
    }
  });
}
</script>
{/literal}