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

// Get installation ID from URL
$id = $_GET['id'] ?? null;
if (!$id) {
    header("Location: index.php");
    exit;
}

// Fetch installation data
$pdo = db();
$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;
}

// Fetch images for this installation
$img_stmt = $pdo->prepare("SELECT * FROM installation_images WHERE installation_id = ?");
$img_stmt->execute([$id]);
$images = $img_stmt->fetchAll(PDO::FETCH_ASSOC);

?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Visualizar Instalação #<?php echo htmlspecialchars($installation['id']); ?> - 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="row justify-content-center">
                <div class="col-lg-8 col-md-10">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header py-3 d-flex justify-content-between align-items-center">
                            <h1 class="h3 mb-0 text-gray-800">Detalhes da Instalação #<?php echo htmlspecialchars($installation['id']); ?></h1>
                            <a href="edit_installation.php?id=<?php echo $installation['id']; ?>" class="btn btn-sm btn-primary"><i class="bi bi-pencil-fill me-1"></i> Editar</a>
                        </div>
                        <div class="card-body">
                            <div class="row mb-3">
                                <div class="col-md-6">
                                    <strong>Nome do Cliente:</strong>
                                    <p class="text-muted"><?php echo htmlspecialchars($installation['client_name'] ?? 'N/A'); ?></p>
                                </div>
                                <div class="col-md-6">
                                    <strong>Endereço:</strong>
                                    <p class="text-muted"><?php echo htmlspecialchars($installation['address'] ?? 'N/A'); ?></p>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-md-6">
                                    <strong>Técnico Responsável:</strong>
                                    <p class="text-muted"><?php echo htmlspecialchars($installation['technician_name'] ?? 'N/A'); ?></p>
                                </div>
                                <div class="col-md-6">
                                    <strong>Data da Instalação:</strong>
                                    <p class="text-muted"><?php echo htmlspecialchars(date('d/m/Y', strtotime($installation['created_at'] ?? time()))); ?></p>
                                </div>
                            </div>
                             <div class="row mb-3">
                                <div class="col-md-6">
                                    <strong>Status:</strong>
                                    <p><span class="badge bg-<?php echo $installation['status'] === 'ativo' ? 'success' : 'warning'; ?>"><?php echo htmlspecialchars(ucfirst($installation['status'] ?? 'N/A')); ?></span></p>
                                </div>
                            </div>

                            <hr>
                            <h5 class="mb-3">Dados Elétricos</h5>
                            <div class="row mb-3">
                                <div class="col-md-4">
                                    <strong>Tensão (V):</strong>
                                    <p class="text-muted"><?php echo htmlspecialchars($installation['voltage'] ?? 'N/A'); ?></p>
                                </div>
                                <div class="col-md-4">
                                    <strong>Fase-Neutro-Terra:</strong>
                                    <p class="text-muted"><?php echo htmlspecialchars($installation['phase_neutral_ground'] ?? 'N/A'); ?></p>
                                </div>
                                <div class="col-md-4">
                                    <strong>Saída do Disjuntor:</strong>
                                    <p class="text-muted"><?php echo htmlspecialchars($installation['breaker_output'] ?? 'N/A'); ?></p>
                                </div>
                            </div>

                            <div class="mb-3">
                                <strong>Observações:</strong>
                                <p class="text-muted"><?php echo nl2br(htmlspecialchars($installation['observations'] ?? 'Nenhuma observação.')); ?></p>
                            </div>

                            <hr>
                            <h5 class="mb-3">Imagens da Instalação</h5>
                            <?php if (!empty($images)): ?>
                                <div class="row">
                                    <?php foreach ($images as $image): ?>
                                        <div class="col-md-4 mb-3">
                                            <a href="<?php echo htmlspecialchars($image['image_path']); ?>" target="_blank">
                                                <img src="<?php echo htmlspecialchars($image['image_path']); ?>" class="img-thumbnail" alt="Imagem da instalação">
                                            </a>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php else: ?>
                                <p class="text-muted">Nenhuma imagem registrada para esta instalação.</p>
                            <?php endif; ?>

                            <hr class="my-4">
                            <div class="d-flex justify-content-end">
                                <a href="index.php" class="btn btn-secondary">Voltar ao Painel</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

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