<?php
session_start();
require_once("./config/conexao.php");
$conn = conn();



$tables = ["processos_gerenciamento_dep", "processos_iniciados_dep", "processos_obs_dep", "processos_produtos_dep", "processos_atividade_dep"];
if(empty($_SESSION['table'])){
	$table = $tables[0];
	$_SESSION['table'] = $table;
} else {
	$table = $_SESSION['table'];
}

$SQL = "SELECT id, id_original FROM $table WHERE (id_original IS NULL OR id_original = 0) AND id <> 0 LIMIT 1000";

$query = mysqli_query($conn, $SQL);

if(mysqli_num_rows($query) > 0){
	$result_geral = true;
	while($fetch = mysqli_fetch_assoc($query)){
		// var_dump($fetch);
		echo '<br>-----<br>';
		if(empty($fetch['id_original'])){
			$id = $fetch['id'];

			$SQL = "UPDATE $table SET id_original = $id WHERE id = $id";
			$result = mysqli_query($conn, $SQL);

			if($result_geral){
				$result_geral = $result;
			}
		}
	}

	var_dump($result_geral);

	echo '
		<script>
			setTimeout(()=>{
				location.href = "/sistema/update_ids.php";
			}, 500);
		</script>
	';
	exit;
} else {
	$index = array_search($_SESSION['table'], $tables);
	$_SESSION['table'] = $tables[$index + 1];
}

echo 'Acabou';
echo '
	<script>
		setTimeout(()=>{
			location.href = "/sistema/update_ids.php";
		}, 500);
	</script>
';