<?php
ini_set('memory_limit', '-1');    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    require_once('config.php');
    require_once('students.php');
    require '../aws/app/start.php'; // AWS S3 SDK initialization

    // Create an instance of the student class
    $studentObj = new Student($conn);
    
//   $studentList = $studentObj->selectImageString(); // Call the method

//     if ($studentList) {
//         // Loop through the result set
//         while ($fetch = mysqli_fetch_assoc($studentList)) {
//             // Assuming pic_name contains the Base64 image string
//             $imageString = $fetch['pic_name'];
    
//             // Display the image using an <img> tag
//             echo '<img src="data:image/jpeg;base64,' . $imageString . '" alt="Image" />';
//             echo '<br>'; // Line break between images (optional)
//         }
//     } else {
//         echo "No data found or query failed.";
//     }


//     exit;
        
    // $students = $studentObj->getAllStudentsTem();
    
    // // echo json_encode($students);
    // exit;
    $objects= $s3->getIterator('ListObjects', [
        'Bucket' => $config['s3']['bucket']
    ]);


?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Download Passport</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>S/N</th>
                <!--<th>File Name</th>-->
                <th>Download</th>
            </tr>
        </thead>
        
        <tbody>
             <?php  
                $serialNumber=0;
                foreach($objects as $object){
                $serialNumber++;
                try {
                    // Get the image content from S3
                    $resultS3 = $s3->getObject([
                        'Bucket' => $config['s3']['bucket'],
                        'Key'    => $object['Key'], // The passport image path from the database
                    ]);
                
                    // Get the binary content of the image
                    $imageData = $resultS3['Body']->getContents();
    
                    // Convert image content to Base64
                    $passportBase64 = base64_encode($imageData);

                } catch (Aws\Exception\AwsException $e) {
                    // Handle S3 exception
                    $passportBase64 = ''; // Set to empty if the image is not found or another error occurs
                }
                
                // $studentObj->storeImageString();
            ?>
                    <tr>
                        <td><?php echo $serialNumber;  ?></td>
                        <td><?php echo $object['Key'];  ?></td>
                        <td>
                            <!--<img src="<?php echo $imageData; ?>" />-->
                            <a href="<?php echo $s3->getObjectUrl($config['s3']['bucket'], $object['Key']);  ?>">
                           <?php echo $s3->getObjectUrl($config['s3']['bucket'], $object['Key']);  ?>
                            </a>
                        </td>
                    </tr>
            <?php                    
                }
            ?>
        </tbody>
    </table>
   
     
</body>
</html>
