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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $client_name = trim($_POST['client_name'] ?? '');
    $address = trim($_POST['address'] ?? '');
    $technician_name = trim($_POST['technician_name'] ?? '');
    $status = trim($_POST['status'] ?? 'ativo');
    $installation_date = !empty($_POST['installation_date']) ? trim($_POST['installation_date']) : null;

    $pdo = db();
    try {
        $pdo->beginTransaction();

        $stmt = $pdo->prepare(
            "INSERT INTO installations (client_name, address, technician_name, status, created_at) VALUES (?, ?, ?, ?, ?)"
        );
        $stmt->execute([$client_name, $address, $technician_name, $status, $installation_date]);
        $installation_id = $pdo->lastInsertId();

        if (isset($_FILES['images'])) {
            $upload_dir = __DIR__ . '/assets/uploads/';
            if (!is_dir($upload_dir)) {
                mkdir($upload_dir, 0775, true);
            }

            $allowed_ext = ['jpg', 'jpeg', 'png', 'gif'];

            foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) {
                if ($_FILES['images']['error'][$key] === UPLOAD_ERR_OK) {
                    $file_name = $_FILES['images']['name'][$key];
                    $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

                    if (in_array($file_ext, $allowed_ext)) {
                        $new_file_name = time() . '_' . uniqid() . '_' . basename($file_name);
                        $dest_path = $upload_dir . $new_file_name;

                        if (move_uploaded_file($tmp_name, $dest_path)) {
                            $image_path = 'assets/uploads/' . $new_file_name;
                            $img_stmt = $pdo->prepare("INSERT INTO installation_images (installation_id, image_path) VALUES (?, ?)");
                            $img_stmt->execute([$installation_id, $image_path]);
                        } else {
                            throw new Exception('Falha ao mover o arquivo enviado: ' . $file_name);
                        }
                    } else {
                        throw new Exception('Tipo de arquivo inválido: ' . $file_name);
                    }
                }
            }
        }

        $pdo->commit();
        $_SESSION['message'] = 'Instalação registrada com sucesso!';
        $_SESSION['message_type'] = 'success';
        header('Location: index.php');
        exit;

    } catch (Exception $e) {
        if ($pdo->inTransaction()) {
            $pdo->rollBack();
        }
        $_SESSION['message'] = 'Erro ao registrar a instalação: ' . $e->getMessage();
        $_SESSION['message_type'] = 'danger';
        header('Location: add_installation.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>Adicionar Nova Instalaçã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 active" 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">
                            <h1 class="h3 mb-0 text-gray-800">Registrar Nova Instalação</h1>
                        </div>
                        <div class="card-body">
                            <?php if (!empty($_SESSION['message'])): ?>
                                <div class="alert alert-<?php echo $_SESSION['message_type']; ?>">
                                    <?php echo $_SESSION['message']; ?>
                                </div>
                                <?php unset($_SESSION['message'], $_SESSION['message_type']); ?>
                            <?php endif; ?>
                            <form action="add_installation.php" method="POST" enctype="multipart/form-data">
                                <div class="mb-3">
                                    <label for="client_name" class="form-label">Nome do Cliente</label>
                                    <input type="text" class="form-control" id="client_name" name="client_name" required style="width: 100%;">
                                </div>
                                <div class="mb-3">
                                    <label for="address" class="form-label">Endereço</label>
                                    <input type="text" class="form-control" id="address" name="address" required style="width: 100%;">
                                </div>
                                <div class="mb-3">
                                    <label for="technician_name" class="form-label">Nome do Técnico</label>
                                    <input type="text" class="form-control" id="technician_name" name="technician_name" required style="width: 100%;">
                                </div>
                                <div class="mb-3">
                                    <label for="installation_date" class="form-label">Data da Instalação</label>
                                    <input type="date" class="form-control" id="installation_date" name="installation_date" required style="width: 100%;">
                                </div>
                                <div class="mb-3">
                                    <label for="status" class="form-label">Status</label>
                                    <select class="form-select" id="status" name="status" style="width: 100%;">
                                        <option value="ativo">Ativo</option>
                                        <option value="Manutencao">Manutenção</option>
                                        <option value="Manutenção Parcial">Manutenção Parcial</option>
                                        <option value="inativo">Inativo</option>
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <label for="images" class="form-label">Imagens da Instalação (múltiplas)</label>
                                    <input type="file" class="form-control" id="images" name="images[]" multiple accept="image/*" style="width: 100%;">
                                </div>
                                <hr class="my-4">
                                <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-primary">Salvar Instalação</button>
                                </div>
                            </form>
                        </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>