<?php
session_start();
require_once __DIR__ . '/db/config.php';

$pdo = db();

// Handle the POST request to delete the installation
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = $_POST['id'] ?? null;
    if ($id) {
        try {
            // Optional: You might want to delete related images first
            $stmt = $pdo->prepare("DELETE FROM installation_images WHERE installation_id = ?");
            $stmt->execute([$id]);

            // Delete the installation
            $stmt = $pdo->prepare("DELETE FROM installations WHERE id = ?");
            $stmt->execute([$id]);

            $_SESSION['message'] = 'Instalação excluída com sucesso!';
            $_SESSION['message_type'] = 'success';
        } catch (Exception $e) {
            $_SESSION['message'] = 'Erro ao excluir a instalação: ' . $e->getMessage();
            $_SESSION['message_type'] = 'danger';
        }
    }
    header("Location: index.php");
    exit;
}

// Handle the GET request to show the confirmation page
$id = $_GET['id'] ?? null;
if (!$id) {
    header("Location: index.php");
    exit;
}

// Fetch installation data for display
$stmt = $pdo->prepare("SELECT * FROM installations WHERE id = ?");
$stmt->execute([$id]);
$installation = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$installation) {
    $_SESSION['message'] = 'Instalação não encontrada.';
    $_SESSION['message_type'] = 'danger';
    header("Location: index.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Confirmar Exclusão - iload net Solutions</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>

    <div class="sidebar">
        <a class="navbar-brand" href="index.php">iload net Solutions</a>
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link" href="index.php">
                    <i class="bi bi-grid-1x2-fill"></i> Painel de Controle
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="add_installation.php">
                    <i class="bi bi-plus-circle-fill"></i> Adicionar Instalação
                </a>
            </li>
        </ul>
    </div>

    <div class="main-content">
        <nav class="top-navbar navbar navbar-expand">
            <div class="container-fluid">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="bi bi-person-circle me-1"></i> Administrador
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><a class="dropdown-item" href="#">Perfil</a></li>
                            <li><a class="dropdown-item" href="#">Sair</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </nav>

        <div class="container-fluid">
            <div class="card border-danger">
                <div class="card-header bg-danger text-white">
                    <h1 class="h3 mb-0">Confirmar Exclusão</h1>
                </div>
                <div class="card-body">
                    <p>Você tem certeza de que deseja excluir permanentemente a instalação para o cliente <strong><?php echo htmlspecialchars($installation['client_name']); ?></strong> no endereço <strong><?php echo htmlspecialchars($installation['address']); ?></strong>?</p>
                    <p class="text-danger"><strong>Atenção:</strong> Esta ação não pode ser desfeita.</p>
                    
                    <form action="delete_installation.php" method="POST">
                        <input type="hidden" name="id" value="<?php echo htmlspecialchars($installation['id']); ?>">
                        
                        <div class="d-flex justify-content-end">
                            <a href="index.php" class="btn btn-secondary me-2">Cancelar</a>
                            <button type="submit" class="btn btn-danger">Confirmar Exclusão</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>