<?php
require "global.ajax.php";
// https://evopos.dev/ajax/reuploadpicture.php/go=ruta?ent[]=1&ent[]=2&ent[]=3&ent[]=4&ent[]=5&ent[]=6

$valores = array_values($_GET);

foreach ($valores as $valor) {
    if (is_array($valor)) {
        foreach ($valor as $entId) {

            $ent_folder_name = getEnterpriseData($entId)['ent_folder_name'];

            $sql = "SELECT * FROM tb_products WHERE pro_ent_id = $entId";
            $result = executePDOFetchAll($sql);
            foreach ($result as $key => $value) {

                $imageName = "";
                // $sourcePath = "../files/products/all/{$ent_folder_name}/{$value['pro_image']}";
                $sourcePath = "../files/products/{$value['pro_image']}";

                if (file_exists($sourcePath) && $value['pro_image'] !== "no-image.png") {

                    $sourceFile = $value['pro_image'];
                    $uploadDirectory = "../files/products/all/{$ent_folder_name}";
                    $imageFileName = reduceImageQualityAndUpload($sourcePath, $sourceFile, $uploadDirectory);

                    if ($imageFileName) $imageName = $imageFileName;

                    $sqlUpdateProduct = "UPDATE tb_products SET pro_image = '{$imageName}' WHERE pro_id = '{$value['pro_id']}' AND pro_ent_id = $entId";
                    echo $res = executeQueryPDO($sqlUpdateProduct, "PASS", "ERROR");
                }
            }
        }
    }
}
