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

$SQL = "SELECT * FROM processos_iniciados_dep WHERE loja_id IS NULL AND EXISTS(SELECT * FROM user WHERE user.id = processos_iniciados_dep.id_user) LIMIT 1000";

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

if(mysqli_num_rows($query)){
	while ($fetch = mysqli_fetch_assoc($query)) {
		$id = $fetch['id'];
		$id_user = $fetch['id_user'];
		$SQL_user = "SELECT * FROM user WHERE id = $id_user";

		$query_user = mysqli_query($conn, $SQL_user);

		if($query_user){
			$fetch_user = mysqli_fetch_assoc($query_user);
			$loja_id = $fetch_user['loja_id'];

			$SQL_update = "UPDATE processos_iniciados_dep SET loja_id = $loja_id WHERE id = $id";
			echo $SQL_update;
			$query_update = mysqli_query($conn, $SQL_update);
			if($query_update){
				echo '<br>---<br>';
			}
		}
	}

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