
<?php
require_once 'conexao.php'; // Inclui a conexão MySQLi ($conn)

// Consulta SQL
$sql = "SELECT * FROM clientes";
$resultado = $conn->query($sql);

if ($resultado === false) {
    die("Erro na consulta: " . $conn->error);
}
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TVI Vendas</title>
    <style>


// Fechar conexão
$conn->close();
?>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        .formulario {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .quadro-colunas {
            display: flex;
            gap: 15px;
            margin-top: 20px;
        }
        .coluna {
            flex: 1;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            padding: 15px;
            min-height: 500px;
        }
        .coluna h2 {
            text-align: center;
            margin-top: 0;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        .coluna-1 { border-top: 4px solid #006600; }
        .coluna-2 { border-top: 4px solid #3498db; }
        .coluna-3 { border-top: 4px solid #FFD700; }
        .cliente-item {
            background: #f9f9f9;
            border-left: 3px solid #3498db;
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 4px;
            cursor: move;
            user-select: none;
        }
        .cliente-item h3 {
            margin: 0 0 5px 0;
        }
        .cliente-item p {
            margin: 5px 0;
            font-size: 0.9em;
            color: #666;
        }
        input, button {
            width: 100%;
            padding: 8px;
            margin: 5px 0 15px;
            box-sizing: border-box;
        }
        button {
            background: #3498db;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background: #2980b9;
        }
        .mensagem {
            margin: 10px 0;
            padding: 10px;
            background: #dff0d8;
            color: #3c763d;
            border-radius: 4px;
        }
    </style>
</head>
<body>

         <img src="C:\Users\thiago.vieira\Pictures/tvitec.png" alt="Descrição da imagem">  <B><center>Sistema de organização de negócios</center></B>
        <div class="quadro-colunas">
            <div class="coluna coluna-1">
                <h2>Contactar</h2>
                <?php foreach ($clientes[1] as $clientes): ?>
                    <div class="cliente-item" draggable="true" data-id="<?php echo $cliente['id']; ?>">
                        <h3>Nome:<?php echo htmlspecialchars($Nome_Cliente['Nome_Cliente']); ?></h3>
                        <p>Email: <?php echo htmlspecialchars($cliente['E-mail']); ?></p>
                        <p>Tel: <?php echo htmlspecialchars($cliente['Telefone']); ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
            
            <div class="coluna coluna-2">
                <h2>Contactados</h2>
                <?php foreach ($clientes_por_coluna[2] as $cliente): ?>
                    <div class="cliente-item" draggable="true" data-id="<?php echo $cliente['id']; ?>">
                        <h3>Nome:<?php echo htmlspecialchars($clientes['Nome']); ?></h3>
                        <p>Email: <?php echo htmlspecialchars($clientes['email']); ?></p>
                        <p>Tel: <?php echo htmlspecialchars($clientes['telefone']); ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
			
			
			  <div class="coluna coluna-2">
                <h2>Negociação</h2>
                <?php foreach ($clientes_por_coluna[2] as $cliente): ?>
                    <div class="cliente-item" draggable="true" data-id="<?php echo $cliente['id']; ?>">
                        <h3>Nome:<?php echo htmlspecialchars($cliente['Nome']); ?></h3>
                        <p>Email: <?php echo htmlspecialchars($cliente['email']); ?></p>
                        <p>Tel: <?php echo htmlspecialchars($cliente['telefone']); ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
			
			  <div class="coluna coluna-2">
                <h2>Agenda ERC</h2>
                <?php foreach ($clientes_por_coluna[2] as $cliente): ?>
                    <div class="cliente-item" draggable="true" data-id="<?php echo $cliente['id']; ?>">
                        <h3>Nome:<?php echo htmlspecialchars($cliente['Nome']); ?></h3>
                        <p>Email: <?php echo htmlspecialchars($cliente['email']); ?></p>
                        <p>Tel: <?php echo htmlspecialchars($cliente['telefone']); ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
			
			  <div class="coluna coluna-2">
                <h2>Visitado</h2>
                <?php foreach ($clientes_por_coluna[2] as $cliente): ?>
                    <div class="cliente-item" draggable="true" data-id="<?php echo $cliente['id']; ?>">
                        <h3>Nome:<?php echo htmlspecialchars($cliente['Nome']); ?></h3>
                        <p>Email: <?php echo htmlspecialchars($cliente['email']); ?></p>
                        <p>Tel: <?php echo htmlspecialchars($cliente['telefone']); ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
			
            
            <div class="coluna coluna-3">
                <h2>Vendido</h2>
                <?php foreach ($clientes_por_coluna[3] as $cliente): ?>
                    <div class="cliente-item" draggable="true" data-id="<?php echo $cliente['id']; ?>">
                        <h3>Nome:<?php echo htmlspecialchars($cliente['Nome']); ?></h3>
                        <p>Email: <?php echo htmlspecialchars($cliente['email']); ?></p>
                        <p>Tel: <?php echo htmlspecialchars($cliente['telefone']); ?></p>
                    </div>
					

					
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const clientes = document.querySelectorAll('.cliente-item');
            const colunas = document.querySelectorAll('.coluna');
            
            // Configurar eventos de arrastar para cada cliente
            clientes.forEach(cliente => {
                cliente.addEventListener('dragstart', dragStart);
            });
            
            // Configurar eventos de soltar para cada coluna
            colunas.forEach(coluna => {
                coluna.addEventListener('dragover', dragOver);
                coluna.addEventListener('drop', drop);
            });
            
            function dragStart(e) {
                e.dataTransfer.setData('text/plain', e.target.dataset.id);
                setTimeout(() => {
                    e.target.classList.add('dragging');
                }, 0);
            }
            
            function dragOver(e) {
                e.preventDefault();
            }
            
            function drop(e) {
                e.preventDefault();
                const clienteId = e.dataTransfer.getData('text/plain');
                const clienteElement = document.querySelector(`.cliente-item[data-id="${clienteId}"]`);
                const colunaAlvo = e.currentTarget;
                
                // Determinar qual coluna o cliente foi solto
                let novaColuna;
                if (colunaAlvo.classList.contains('coluna-1')) novaColuna = 1;
                else if (colunaAlvo.classList.contains('coluna-2')) novaColuna = 2;
                else if (colunaAlvo.classList.contains('coluna-3')) novaColuna = 3;
                
                // Mover o elemento para a nova coluna
                colunaAlvo.appendChild(clienteElement);
                clienteElement.classList.remove('dragging');
                
                // Atualizar no banco de dados
                fetch('index.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `atualizar_coluna=1&id=${clienteId}&coluna=${novaColuna}`
                })
                .then(response => response.text())
                .then(data => console.log(data))
                .catch(error => console.error('Erro:', error));
            }
        });
    </script>
</body>
</html>