<?php if(defined('TENANT_URL') && !empty(TENANT_URL)) {
    $_REQUEST['tenant'] = TENANT_URL;
}

if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php'); }
if(file_exists('/config.php')){require_once('/config.php'); }



if($_REQUEST['FUNCTION']=="getReps"){
$date=strtotime($_REQUEST['start']);

$stmt = $GLOBALS['con']->prepare("SELECT `repitem`,`repnumber` FROM `reportitems` WHERE `repdate`<'".$date."'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$lc=$stmt->get_result();$stmt->close();
$count=mysqli_num_rows($lc);
echo $count;
}


if($_REQUEST['FUNCTION']=="commit"){
$date=strtotime($_REQUEST['start']);

$stmt = $GLOBALS['con']->prepare("SELECT `repitem`,`repnumber` FROM `reportitems` WHERE `repdate`<'".$date."'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$lc=$stmt->get_result();$stmt->close();
$count=mysqli_num_rows($lc);
while($dta=mysqli_fetch_assoc($lc)){
$stmt = $GLOBALS['con']->prepare("INSERT INTO `archiveitems` SELECT * FROM `reportitems` WHERE `repitem`='".$dta['repitem']."' ")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$stmt->close();

$stmt2 = $GLOBALS['con']->prepare("DELETE FROM `reportitems` WHERE `repitem`='".$dta['repitem']."' ")or die(mysqli_error($GLOBALS['con']));
$stmt2->execute();$stmt2->close();
}
echo $count;
}

if($_REQUEST['FUNCTION']=="removeFiles"){
    function deleteFilesOlderThanYears($dir, $yearsToCheck, $exceptions = []) {
        foreach(glob("$dir/*") as $file) {
            if(is_dir($file)) {
                deleteFilesOlderThanYears($file, $yearsToCheck, $exceptions); // recursively delete files in subfolders
            } else {
                // Check if the file is older than specified number of years
                if(time() - filemtime($file) > $yearsToCheck * 31536000) { // 1 year = 31536000 seconds
                    // Check if the file is not in the exceptions list
                    if(!in_array(basename($file), $exceptions)) {
                        unlink($file); // delete the file
                        echo "Deleted: $file<br>";
                    } else {
                        echo "Skipped (Exception): $file<br>";
                    }
                }
            }
        }
    }
    
    // Specify the directory you want to start the deletion from
    $directory = '../../..'.$_REQUEST['tenant'].'/output';
    
    // Number of years to check
    $yearsToCheck = $_REQUEST['years'];
    
    // List of file names to be excluded from deletion (exceptions)
    $exceptions = ['file1.txt', 'file2.jpg'];
    
    deleteFilesOlderThanYears($directory, $yearsToCheck, $exceptions);
}

if($_REQUEST['FUNCTION']=="getDocs"){

    function findOldFiles($targetFolder, $years) {
        $cutoffDate = strtotime("-$years years");
        $totalFiles = 0;
    
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($targetFolder));
        foreach ($iterator as $file) {
            if ($file->isFile() && $file->getMTime() < $cutoffDate) {
                $totalFiles++;
                // You can print the path of the old file if needed: echo $file->getPathname() . PHP_EOL;
            }
        }
    
        return $totalFiles;
    }
    
    // Example usage
    $targetFolder = '../../..'.$_REQUEST['tenant'].'/output';; // Replace this with the actual path to your target folder
    $years = $_REQUEST['years']; // Replace this with the number of years provided by the user
    
    $totalOldFiles = findOldFiles($targetFolder, $years);
    echo $totalOldFiles;
    
}

?>