<?php
$action = false;

if (isset($_POST['submit'])) {
	if ($_POST['submit'] == 'reload') $update = 0;
}

if (isset($_POST['change'])){
	foreach ($_POST['change'] AS $key => $val){
		$change[$key] = $val;
	}
}

if ($update) {

	if (isset($c_levels) && $c_levels && (!isset($c_levels_multi) OR !$c_levels_multi)) {
		$parent_guidv4 = array();
		if (isset($_POST['parent_guidv4']) && $_POST['parent_guidv4']){
			foreach ($_POST['parent_guidv4'] AS $key => $value){
				$parent_guidv4[] = $value;
			}
			$parent_guidv4 = array_unique($parent_guidv4);
		}
	}

	if (isset($_POST['date'])) {
		$datetime = DateTime::createFromFormat('Y-m-d\TH:i', $_POST['date']);
		$datetime = $datetime->getTimestamp();
	}
		
	if (!isset($fehler) && $update == 1){
				
		$sql = "UPDATE ".$multi_module." SET ";
		$i = 1;
		$update = 0;
    foreach($change AS $key => $val){
    	if (isset(${$key}) && $val == 1 && $key != 'levels'){
	    	if ($i == 1) $sql.= " ".$key." = :".$key." ";
	    	else $sql.= ", ".$key." = :".$key." ";
	    	$update = 1;
	    	$i++;
	    }
    }
    $sql.= " WHERE id IN ($ids) ";
		
		$result = $conn->prepare($sql);
    foreach($change AS $key => $val){
    	if (isset(${$key}) && $val == 1){
				$result->bindValue(':'.$key, ${$key});
			}
		}
		
			
		if (isset($c_levels) && $c_levels && (!isset($c_levels_multi) OR !$c_levels_multi) && isset($change['levels']) && $change['levels'] == 1){
			foreach (explode(',', $ids) AS $id){
				$sql_sub = "DELETE FROM ".$multi_module."_levels_links WHERE id_module = :id ";
				$result_sub = $conn->prepare($sql_sub);
				$result_sub->bindValue(':id', $id, PDO::PARAM_INT);
				$result_sub->execute();
				if (isset($parent_guidv4) && is_array($parent_guidv4)){
					foreach ($parent_guidv4 AS $key => $value){
						$sql_tmp = "INSERT INTO ".$multi_module."_levels_links SET id_module = :id, guidv4 = :guidv4 ";
						$result_tmp = $conn->prepare($sql_tmp);
						$result_tmp->bindValue(':id', $id, PDO::PARAM_INT);
						$result_tmp->bindValue(':guidv4', $value, PDO::PARAM_STR);
						$result_tmp->execute();
					}
				}
			}
		}
		
		if ($update) {
			
			if ($result->execute()){
			
			  $url_form = "popup_form.php?module=".$module."&backend_module=".$backend_module."&multi_module=".$multi_module."&ids=".$ids;
								
				if (isset($url_form) || (!empty($update_parent))) {
					include('includes/function/reload.php');
				}
				
			}
		}
	}
}

include('includes/function/multi_load.php');

if ((!isset($_POST['submit']) && isset($ids)) OR (isset($_POST['submit']) && $_POST['submit'] != 'reload' && isset($ids) && !isset($fehler))){	
	if (isset($c_levels) && $c_levels && (!isset($c_levels_multi) OR !$c_levels_multi)) {
		$sql_tmp = "SELECT guidv4 FROM ".$multi_module."_levels_links WHERE id_module IN ($ids) ORDER BY id";
		$result_tmp = $conn->prepare($sql_tmp);
		$result_tmp->execute();
		if ($result_tmp->rowCount()){
			$parent_guidv4 = NULL;
			while($arr_tmp = $result_tmp->fetch()){
				if (!$parent_guidv4) $parent_guidv4 = $arr_tmp['guidv4'];
				else if ($parent_guidv4 != $arr_tmp['guidv4']) $parent_guidv4 = 'undefined';
			}
			if ($parent_guidv4 == 'undefined') $parent_guidv4 = NULL;
		}
	}
}

if (isset($datetime) && $datetime) $datetime = date('Y-m-d\TH:i', $datetime);
else $datetime = date('Y-m-d\TH:i');