<?php
if (isset($level_data) && is_array($level_data) && check_table($module.'_position')){
	$data = array();
	$sql_tmp = "SELECT title FROM ".$module."_position WHERE c_active = '1' ORDER BY sortorder ASC";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){
		while ($arr_tmp = $result_tmp->fetch()){
			$new = array();
			$new['name'] = constant('LABEL_POSITION_'.strtoupper($arr_tmp['title']));
			$new['value'] = $arr_tmp['title'];
			$data[] = $new;
		}
	}
	print "
<span class='headline'>".LABEL_POSITION."</span>";
	foreach ($level_data AS $key => $value){
		if (isset($value['value_position']) && $value['value_position']) $positions = explode(',', $value['value_position']);
		else $positions = array();
		print "
<div class='form-row'>
	<label for='level_tree_".$key."' class='form-label tree'>".$value['title']."</label>
	<div class='form-cols'>
		<div class='form-col form-full'>
			<div class='form-controls tree-select'>";
		if (isset($positions) && is_array($positions)){
			foreach ($positions AS $position_key => $position_value){
				print "<input type='hidden' name='level_data[".$key."][value_position][".$position_key."]' class='level_position_".$key."' value='".$position_value."' />";
			}
		} else if (isset($positions) && $positions){
			print "<input type='hidden' name='level_data[".$key."][value_position][0]' class='level_position_".$key."' value='".$positions."' />";
		}
		print "
				<div class='level_tree_".$key."'></div>
			</div><!-- /.form-controls -->
		</div><!-- /.form-col -->
	</div><!-- /.form-cols -->
</div><!-- /.form-row -->";
?>
<script>
if ($('.level_tree_<?=$key;?>').length != 0){
	const treeselect = new Treeselect({
	  parentHtmlContainer: document.querySelector('.level_tree_<?=$key;?>'),
	  options: <?=json_encode($data);?>,
	  value: <?=json_encode($positions);?>,
	  id: 'level_tree_<?=$key;?>',
	  openLevel: 5,
	  placeholder: "<?=LABEL_SEARCH;?>",
	  emptyText: "<?=LABEL_NO_RESULTS;?>",
	  isIndependentNodes: 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>"
	  }
	})
	
	treeselect.srcElement.addEventListener('input', (e) => {
		$('.level_position_<?=$key;?>').remove();
		if (e.detail.constructor === Array){
			for (let i = 0; i < e.detail.length; ++i) {
		  	$('<input type="hidden" name="level_data[<?=$key;?>][value_position]['+i+']" class="level_position_<?=$key;?>" value="'+e.detail[i]+'\" />').insertBefore('.level_tree_<?=$key;?>');
			}
		} else if (e.detail) $('<input type="hidden" name="level_data[<?=$key;?>][value_position][0]" class="level_position_<?=$key;?>" value="'+e.detail+'\" />').insertBefore('.level_tree_<?=$key;?>');
	})
}
</script>
<?php
	}
	print "<hr />";
}
