<?php
if ((isset($c_levels) && $c_levels) OR (isset($c_levels_images) && $c_levels_images) OR (isset($c_levels_files) && $c_levels_files)){
	if ($action == 'add'){
		if ((isset($_POST['submit']) && $_POST['submit'] != 'reload') OR !isset($_POST['submit'])){
			if (isset($_GET['parent_guidv4'])) {
				if (isset($c_levels_multi) && $c_levels_multi) $parent_guidv4[] = addslashes(trim(strip_tags(stripslashes($_GET['parent_guidv4']))));
				else $parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_GET['parent_guidv4']))));
			}
		}
	}
	if (!isset($parent_guidv4)) {
		if (isset($c_levels_multi) && $c_levels_multi) $parent_guidv4 = array();
		else $parent_guidv4 = NULL;
	}
	
	$data = array();
	if ((isset($c_levels_images) && $c_levels_images) OR (isset($c_levels_files) && $c_levels_files)) $sql_tmp = "SELECT guidv4, title, parent_guidv4 FROM ".str_replace('_levels', '', $module)."_levels WHERE language = :language GROUP BY guidv4 ORDER BY sortorder ASC";
	else if (isset($level_module_key[1])) $sql_tmp = "SELECT guidv4, title, parent_guidv4 FROM ".$level_module_key[1]."_levels 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) && is_array($parent_guidv4)){
		foreach ($parent_guidv4 AS $key => $value){
			print "<input type='hidden' name='parent_guidv4[".$key."]' class='parent_guidv4' value='".$value."' />";
		}
	} else if (isset($level_module_key[1]) && isset($parent_guidv4) && $parent_guidv4){
		print "<input type='hidden' name='parent_guidv4' class='parent_guidv4' value='".$parent_guidv4."' />";
	} else if (isset($parent_guidv4) && $parent_guidv4){
		print "<input type='hidden' name='parent_guidv4[0]' class='parent_guidv4' value='".$parent_guidv4."' />";
	}
	print "
				<div class='formselect'></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));
	if (isset($c_levels_multi) && $c_levels_multi) {
		$script['value'] = json_encode($parent_guidv4);
		$script['single'] = "false";
	} else {
		if (is_array($parent_guidv4) && isset($parent_guidv4[0])) $script['value'] = json_encode($parent_guidv4[0]);
		else $script['value'] = json_encode($parent_guidv4);
		$script['single'] = "true";
	}
	$script['placeholder'] = LABEL_SEARCH;
	$script['empty'] = LABEL_NO_RESULTS;
	
	$script['js'] = <<<JS
		if ($('.formselect').length != 0){
			const formselect = new Treeselect({
			  parentHtmlContainer: document.querySelector('.formselect'),
			  options: {$script['options']},
				value: {$script['value']},
			  id: "parent_guidv4",
			  openLevel: 5,
			  placeholder: "{$script['placeholder']}",
			  emptyText: "{$script['empty']}",
			  isIndependentNodes: true,
			  isSingleSelect: {$script['single']},
			  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>"
			  }
			});
		JS;

	if (isset($level_module_key[1])) {
		$script['js'] .= <<<JS
			formselect.srcElement.addEventListener('input', (e) => {
				$('.parent_guidv4').remove();
				if (e.detail) $('<input type="hidden" name="parent_guidv4" class="parent_guidv4" value="'+e.detail+'\" />').insertBefore('.formselect');
			});
			JS;
	} else {
		$script['js'] .= <<<JS
			formselect.srcElement.addEventListener('input', (e) => {
				$('.parent_guidv4').remove();
				if (e.detail && e.detail.constructor === Array){
					for (let i = 0; i < e.detail.length; ++i) {
				  	$('<input type="hidden" name="parent_guidv4['+i+']" class="parent_guidv4" value="'+e.detail[i]+'" />').insertBefore('.formselect');
					}
				} else if (e.detail) $('<input type="hidden" name="parent_guidv4[0]" class="parent_guidv4" value="'+e.detail+'" />').insertBefore('.formselect');
					
				if ($('input[name="change[levels]"]').length != 0){
					$('input[name="change[levels]"]').val(1);
				}
			})
			JS;
	}
	$script['js'] .= <<<JS
		}
		JS;
	print "<script>".$script['js']."</script>";

}