<?php
// Database configuration settings
$host = 'localhost';
$db = 'digupdog_FEED';
$user = 'digupdog_FEEDadmin';
$pass = 'Raimundinho1';
$charset = 'utf8mb4';

// Attempt to establish a new database connection
try {
    $pdo = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

$suggested_equipment = [];

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $square_footage = $_POST['square_footage'];
    $num_rooms = $_POST['num_rooms'];

    // Calculating total BTUs required
    // The below values are placeholders and should be replaced with your actual BTU calculation logic
    $btu_per_room = 6000; // This value should be determined based on the actual data and calculation logic
    $total_btus = $num_rooms * $btu_per_room;
    
    // Fetch the suggested equipment based on the calculated BTUs and number of rooms
    $stmt = $pdo->prepare("SELECT * FROM HVAC_Products WHERE rooms_range_min <= :num_rooms AND rooms_range_max >= :num_rooms AND btus_range_min <= :total_btus AND btus_range_max >= :total_btus");
    $stmt->execute(['num_rooms' => $num_rooms, 'total_btus' => $total_btus]);
    
    // Fetching all the matching equipment
    $suggested_equipment = $stmt->fetchAll();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HVAC Equipment Suggestion</title>
</head>
<body>

<h1>Find Your Suggested HVAC Equipment</h1>

<form method="post">
    Square Footage: <input type="number" name="square_footage" required><br>
    Number of Rooms: <input type="number" name="num_rooms" required><br>
    <input type="submit" value="Get Suggestion">
</form>

<?php if ($_SERVER["REQUEST_METHOD"] == "POST" && empty($suggested_equipment)): ?>
    <p>No equipment found for the given criteria.</p>
<?php elseif (!empty($suggested_equipment)): ?>
    <h2>Suggested Equipment</h2>
    <table>
        <thead>
            <tr>
                <th>Manufacturer</th>
                <th>Model Number</th>
                <th>Heating Capacity</th>
                <th>Cooling Capacity</th>
                <th>SEER2</th>
                <th>HSPF2</th>
                <th>Heating Capacity at Low Temp</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($suggested_equipment as $equipment): ?>
            <tr>
                <td><?= htmlspecialchars($equipment['manufacturer']) ?></td>
                <td><?= htmlspecialchars($equipment['model_number']) ?></td>
                <td><?= htmlspecialchars($equipment['heating_capacity']) ?></td>
                <td><?= htmlspecialchars($equipment['cooling_capacity']) ?></td>
                <td><?= htmlspecialchars($equipment['seer2']) ?></td>
                <td><?= htmlspecialchars($equipment['hspf2']) ?></td>
                <td><?= htmlspecialchars($equipment['heating_capacity_low_temp']) ?></td>
                <td>$<?= htmlspecialchars($equipment['price']) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

</body>
</html>
