190 lines
7.6 KiB
PHP
190 lines
7.6 KiB
PHP
<!DOCTYPE html>
|
|
<html lang="pt-BR">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Upload de Documentos</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; margin: 20px; }
|
|
.upload-form { max-width: 500px; margin: 0 auto; padding: 20px; background: #fff; }
|
|
.button { padding: 10px 20px; background: #4CAF50; color: white; border: none; }
|
|
.preview { max-width: 100%; margin: 20px 0; }
|
|
.preview img { max-width: 100%; }
|
|
#result { display: none; }
|
|
.error { color: red; }
|
|
select { width: 100%; padding: 8px; margin-bottom: 10px; }
|
|
</style>
|
|
<link href="https://cdn.jsdelivr.net/npm/fancybox@3.5.7/dist/jquery.fancybox.min.css" rel="stylesheet">
|
|
<script src="https://cdn.jsdelivr.net/npm/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
|
|
</head>
|
|
<body>
|
|
<div class="upload-form">
|
|
<h2>Upload de Documentos</h2>
|
|
<?php
|
|
// Debug de inicialização
|
|
echo "<!-- Debug: Iniciando página de upload -->\n";
|
|
require_once 'config.php';
|
|
|
|
if ($conn->connect_error) {
|
|
echo "<div class='error'>Erro de conexão: " . $conn->connect_error . "</div>";
|
|
} else {
|
|
echo "<!-- Debug: Conexão OK -->\n";
|
|
}
|
|
?>
|
|
<form action="process.php" method="post" enctype="multipart/form-data" id="uploadForm">
|
|
<p>
|
|
<label>Cliente:</label><br>
|
|
<select name="client_id" required>
|
|
<?php
|
|
require_once 'config.php';
|
|
|
|
// Debug de conexão
|
|
if (!$conn) {
|
|
die("Erro de conexão: " . mysqli_connect_error());
|
|
}
|
|
|
|
// Debug da tabela
|
|
$check_table = $conn->query("SHOW TABLES LIKE 'clients'");
|
|
if ($check_table->num_rows == 0) {
|
|
die("Tabela 'clients' não encontrada!");
|
|
}
|
|
|
|
// Verificar estrutura da tabela
|
|
$check_columns = $conn->query("DESCRIBE clients");
|
|
if (!$check_columns) {
|
|
die("Erro ao verificar estrutura da tabela: " . $conn->error);
|
|
}
|
|
|
|
// Contar registros
|
|
$count = $conn->query("SELECT COUNT(*) as total FROM clients");
|
|
$total = $count->fetch_assoc()['total'];
|
|
echo "<!-- Total de clientes: " . $total . " -->";
|
|
|
|
// Query com validação e debug
|
|
$sql = "SELECT id, name FROM clients WHERE 1=1";
|
|
$result = $conn->query($sql);
|
|
|
|
if (!$result) {
|
|
die("Erro na query: " . $conn->error);
|
|
}
|
|
|
|
// Exibir clientes com verificação
|
|
echo "<option value=''>Selecione um cliente</option>";
|
|
if ($result->num_rows > 0) {
|
|
while ($row = $result->fetch_assoc()) {
|
|
echo "<option value='" . $row['id'] . "'>" .
|
|
htmlspecialchars($row['name']) . "</option>";
|
|
}
|
|
} else {
|
|
echo "<option value='' disabled>Nenhum cliente encontrado</option>";
|
|
echo "<!-- Query executada: " . $sql . " -->";
|
|
}
|
|
?>
|
|
</select>
|
|
</p>
|
|
<p>
|
|
<label>Selecione o documento:</label><br>
|
|
<input type="file" name="document" accept="image/*" required>
|
|
</p>
|
|
<p>
|
|
<label>Tipo de documento:</label><br>
|
|
<select name="doc_type" required>
|
|
<option value="cpf">CPF</option>
|
|
<option value="rg">RG</option>
|
|
<option value="cnh">CNH</option>
|
|
</select>
|
|
</p>
|
|
<input type="submit" value="Processar" class="button">
|
|
</form>
|
|
|
|
<div id="uploadStatus" style="margin: 10px 0; padding: 10px;"></div>
|
|
<div id="debug" style="margin: 10px 0; padding: 10px; background: #f5f5f5; display: none;"></div>
|
|
|
|
<div id="error" class="error">
|
|
<?php
|
|
if (isset($conn) && !$result) {
|
|
echo "Erro ao buscar clientes: " . $conn->error;
|
|
}
|
|
?>
|
|
</div>
|
|
<p><a href="index.php?page=doc_list">Ver documentos processados</a></p>
|
|
</div>
|
|
|
|
<div id="result">
|
|
<h3>Resultado do Processamento:</h3>
|
|
<div class="preview">
|
|
<h4>Documento Original:</h4>
|
|
<img id="originalImage" src="" alt="Original">
|
|
</div>
|
|
<div class="preview">
|
|
<h4>Documento Processado:</h4>
|
|
<img id="processedImage" src="" alt="Processado">
|
|
</div>
|
|
</div>
|
|
|
|
<div id="error" class="error"></div>
|
|
|
|
<script>
|
|
document.getElementById('uploadForm').onsubmit = async (e) => {
|
|
e.preventDefault();
|
|
|
|
const formData = new FormData(e.target);
|
|
const statusDiv = document.getElementById('uploadStatus');
|
|
const debugDiv = document.getElementById('debug');
|
|
|
|
debugDiv.style.display = 'block';
|
|
debugDiv.innerHTML = 'Iniciando upload...<br>';
|
|
|
|
try {
|
|
const response = await fetch('index.php?page=temp_upload', {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
debugDiv.innerHTML += `Status: ${response.status}<br>`;
|
|
const text = await response.text();
|
|
debugDiv.innerHTML += `Resposta raw: ${text}<br>`;
|
|
|
|
let result;
|
|
try {
|
|
result = JSON.parse(text);
|
|
} catch (e) {
|
|
throw new Error(`Resposta inválida: ${text}`);
|
|
}
|
|
|
|
if (result.success) {
|
|
debugDiv.innerHTML += 'Upload bem sucedido, abrindo editor...<br>';
|
|
localStorage.setItem('tempImage', result.imageUrl);
|
|
localStorage.setItem('tempClientId', formData.get('client_id'));
|
|
localStorage.setItem('tempDocType', formData.get('doc_type'));
|
|
|
|
$.fancybox.open({
|
|
src: 'editor.php',
|
|
type: 'iframe',
|
|
opts: {
|
|
afterClose: () => {
|
|
localStorage.removeItem('tempImage');
|
|
localStorage.removeItem('tempClientId');
|
|
localStorage.removeItem('tempDocType');
|
|
}
|
|
}
|
|
});
|
|
} else {
|
|
throw new Error(result.message || 'Erro desconhecido');
|
|
}
|
|
} catch (error) {
|
|
debugDiv.innerHTML += `Erro: ${error.message}<br>`;
|
|
statusDiv.innerHTML = `Erro: ${error.message}`;
|
|
}
|
|
};
|
|
|
|
// Receber mensagem do iframe
|
|
window.addEventListener('message', function(event) {
|
|
if (event.data.type === 'documentSaved') {
|
|
$.fancybox.close();
|
|
window.location.href = 'index.php?page=doc_list&status=success';
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|