<?php
require_once __DIR__ . '/../auth/verifica.php';
require_once("../config/conexao.php");
$aluno_id = $_GET['aluno_id'];
$busca = $_GET['busca'] ?? '';

$sql = "SELECT * FROM documentos 
WHERE aluno_id=:aluno_id AND status='ativo'
AND nome_arquivo LIKE :busca
ORDER BY id DESC";

$stmt = $pdo->prepare($sql);
$stmt->bindValue(":aluno_id",$aluno_id);
$stmt->bindValue(":busca","%$busca%");
$stmt->execute();
$dados = $stmt->fetchAll();
?>

<h2>📂 Documentos do Aluno</h2>

<form method="GET">
<input type="hidden" name="aluno_id" value="<?=$aluno_id?>">
<input type="text" name="busca" placeholder="Buscar documento">
<button>Buscar</button>
</form>

<br>
<a href="documentos_upload.php?aluno_id=<?=$aluno_id?>">+ Upload</a>

<table border="1" width="100%">
<tr>
<th>Nome</th>
<th>Tipo</th>
<th>Preview</th>
<th>Ações</th>
</tr>

<?php foreach($dados as $d){ 
$path = "../../uploads/alunos/".$aluno_id."/".$d['tipo']."/".$d['arquivo'];
?>

<tr>
<td><?=$d['nome_arquivo']?></td>
<td><?=$d['tipo']?></td>

<td>
<?php if(strpos($d['arquivo'],'.pdf')){ ?>
<iframe src="<?=$path?>" width="100" height="80"></iframe>
<?php } else { ?>
<img src="<?=$path?>" width="80">
<?php } ?>
</td>

<td>
<a href="<?=$path?>" target="_blank">Abrir</a>
<a href="documentos_excluir.php?id=<?=$d['id']?>&aluno_id=<?=$aluno_id?>">Excluir</a>
</td>
</tr>

<?php } ?>
</table>
