<?php
if (isset($c_levels) && $c_levels){
	if (isset($_POST['parent_guidv4'])) $parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_POST['parent_guidv4']))));
	else {
		if (isset($_GET['parent_guidv4'])) $parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_GET['parent_guidv4']))));
		else $parent_guidv4 = NULL;
	}
		
	$data = array();
	if (isset($level_module_key[1])) $sql_tmp = "SELECT guidv4, title, parent_guidv4 FROM ".$module." WHERE language = :language GROUP BY guidv4 ORDER BY sortorder ASC";
	else $sql_tmp = "SELECT guidv4, title, parent_guidv4 FROM ".$level_module."_levels WHERE language = :language GROUP BY guidv4 ORDER BY sortorder ASC";
	$result_tmp = $conn->prepare($sql_tmp);
	if (isset($language) && $language) $result_tmp->bindValue(':language', $language, PDO::PARAM_STR);
	else $result_tmp->bindValue(':language', $_SESSION['backend']['language'], PDO::PARAM_STR);
	$result_tmp->execute();
	
	if ($result_tmp->rowCount()){
		while($arr_tmp = $result_tmp->fetch()){
			$data[] = $arr_tmp;
		}
	}
	
	print "
<div class='form-row'>
	<label for='parent_guidv4' class='form-label tree'>".LABEL_LEVEL."</label>
	<div class='form-cols'>
		<div class='form-col form-full'>
			<div class='form-controls tree-select'>";
	if (isset($parent_guidv4) && $parent_guidv4){
		print "<input type='hidden' name='parent_guidv4' class='parent_guidv4' id='parent_guidv4' value='".$parent_guidv4."' />";
	}
	print "
				<div class='filterselect'></div>
			</div><!-- /.form-controls -->
		</div><!-- /.form-col -->
	</div><!-- /.form-cols -->
</div><!-- /.form-row -->

<hr />";

	$script = array();
	$script['js'] = NULL;
	$script['options'] = json_encode(build_tree($data, true));
	$script['value'] = json_encode($parent_guidv4);
	$script['placeholder'] = LABEL_SEARCH;
	$script['empty'] = LABEL_NO_RESULTS;
	
	$script['js'] = <<<JS
		if ($('.filterselect').length != 0){
			const filterselect = new Treeselect({
			  parentHtmlContainer: document.querySelector('.filterselect'),
			  options: {$script['options']},
				value: {$script['value']},
			  id: "parent_guidv4",
			  openLevel: 5,
			  placeholder: "{$script['placeholder']}",
			  emptyText: "{$script['empty']}",
			  isSingleSelect: "true",
			  iconElements: {
			  	arrowUp: "<i class='material-icons md-20'>expand_less</i>",
			  	arrowDown: "<i class='material-icons md-20'>expand_more</i>",
			  	arrowRight: "<i class='material-icons md-20'>chevron_right</i>",
			  	attention: "<i class='material-icons md-20 md-blue'>warning_amber</i>",
			  	clear: "<i class='material-icons out md-20'>cancel</i>",
			  	cross: "<i class='material-icons md-20'>close</i>",
			  	check: "<i class='material-icons md-16'>check</i>",
			  	partialCheck: "<i class='material-icons md-16'>remove</i>"
			  }
			});
			
			filterselect.srcElement.addEventListener('input', (e) => {
				$('.parent_guidv4').remove();
				if (e.detail) $('<input type="hidden" name="parent_guidv4" class="parent_guidv4" id="parent_guidv4" value="'+e.detail+'" />').insertBefore('.filterselect');
				
		  	$("button[type='submit']").val('reload');
		  	$("button[type='submit']").click();
			});
		}
		JS;
	print "<script>".$script['js']."</script>";
}