<?
// angular
$APPLICATION->AddheadScript('https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js');

// semantic ui
CJSCore::RegisterExt("semanticui", Array(
  "js"  => "/local/js/semantic-ui/semantic.js",
  "css" => "/local/js/semantic-ui/semantic.css"
));

// jquery tree plugin
CJSCore::RegisterExt("jqxtree", Array(
  "js"  => array(
  	"/local/js/jqwidgets/jqxcore.js",
  	"/local/js/jqwidgets/jqxbuttons.js",
  	"/local/js/jqwidgets/jqxscrollbar.js",
  	"/local/js/jqwidgets/jqxpanel.js",
  	"/local/js/jqwidgets/jqxtree.js",
  	"/local/js/jqwidgets/jqxcheckbox.js",
  ),
  "css" => array(
  	"/local/js/jqwidgets/styles/jqx.base.css"
  )
));

// init plugins
CJSCore::Init(array("semanticui", "jqxtree"));

// get departments
$filter = array(
	"ACTIVE" 	=> "Y",
	"IBLOCK_ID" => COption::GetOptionString("intranet", "iblock_structure")
);
$select 		= array("ID", "NAME", "IBLOCK_SECTION_ID", "XML_ID");
$rsSections 	= CIBlockSection::GetTreeList($filter, $select);
$arDepartments 	= array();
$arMapDeps 		= array();

while($section = $rsSections->GetNext()) {
	$arMapDeps[$section["XML_ID"]] = $section["ID"];
	
	$section = array(
		"id" 			=> $section["ID"],
		"title" 		=> $section["NAME"],
		"parent" 		=> $section["IBLOCK_SECTION_ID"],
		"department" 	=> true,
		"position" 		=> false

	);
	
	$arDepartments[$section["id"]] = $section;
}


// get positions
$sort 		= array();
$filter 	= array(
	"IBLOCK_ID" => $iblock_id,
	"ACTIVE"	=> "Y"
);
$group_by 	 = false;
$nav_params  = false;
$select 	 = array(
	"ID",
	"PROPERTY_DEPARTMENT_LINK",
	"PROPERTY_POSITION"
);

$arPositions = array();
$rsElements = CIBlockElement::GetList($sort, $filter, $group_by, $nav_params, $select);

while($elem = $rsElements->GetNext()) {
	$dep_xml_id = $elem["PROPERTY_DEPARTMENT_LINK_VALUE"];
	$dep_id 	= $arMapDeps[$dep_xml_id];

	$elem_id = $elem["ID"];
	$position = $elem["PROPERTY_POSITION_VALUE"];

	// check if selected item
	if (in_array($elem_id, $valueArray)) {
		$checked = true;
	} else {
		$checked = false;
	}

	$sectionArray = array(
		"id" 			=> $elem_id,
		"title" 		=> $position,
		"parent" 		=> $dep_id,
		"department" 	=> false,
		"position" 		=> true,
		"checked"		=> $checked
	);


	$arDepartments[$elem_id] = $sectionArray;
	
	$arPositions[$position]["id"] 	 = $position;
	$arPositions[$position]["title"] = $position;
	$arPositions[$position]["department"] = true;
	$arPositions[$position]["position"] = false;

	$parent_dep_id = $arDepartments[$dep_id]["parent"];
	$arPositions[$position]["categories"][$elem_id]["id"] = $elem_id;
	$arPositions[$position]["categories"][$elem_id]["title"] = $arDepartments[$parent_dep_id]["title"] .
															   ' -> ' .
															   $arDepartments[$dep_id]["title"];
	$arPositions[$position]["categories"][$elem_id]["department"] = false;
	$arPositions[$position]["categories"][$elem_id]["position"] = true;
	$arPositions[$position]["categories"][$elem_id]["checked"] = $checked;
}

// Построение дерева за один проход
foreach($arDepartments as $section_id=>$data) {
    $arDepartments[$data['parent']]['categories'][$section_id] = &$arDepartments[$section_id];
}

$arDepartments = $arDepartments['']['categories'];
?>