<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mozi adatbázis megoldások</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
            color: #333;
        }
        .container {
            width: 80%;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #444;
        }
        .result {
            margin: 20px 0;
            padding: 10px;
            background-color: #e7f3fe;
            border-left: 6px solid #2196F3;
        }
        .result.success {
            background-color: #d4edda;
            border-left: 6px solid #28a745;
        }
        .result.error {
            background-color: #f8d7da;
            border-left: 6px solid #dc3545;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f1f1f1;
        }
    </style>
</head>
<body>
    <?php
         require_once('config.php');

         function executeQuery($conn, $query, $onlyValues = false) {
             $result = $conn->query($query);
             if ($result->num_rows > 0) {
                 $output = "";
                 while($row = $result->fetch_assoc()) {
                     foreach ($row as $key => $value) {
                         if ($onlyValues) {
                             $output .= "$value<br>";
                         } else {
                             $output .= "$key: $value<br>";
                         }
                     }
                 }
                 return $output;
             } else {
                 return "0 results";
             }
         }
 
         function executeQueryAsTable($conn, $query) {
             $result = $conn->query($query);
             if ($result->num_rows > 0) {
                 $output = "<table><tr>";
                 // Fetch the headers
                 $headers = array_keys($result->fetch_assoc());
                 foreach ($headers as $header) {
                     $output .= "<th>$header</th>";
                 }
                 $output .= "</tr>";
                 // Reset result pointer and fetch data
                 $result->data_seek(0);
                 while($row = $result->fetch_assoc()) {
                     $output .= "<tr>";
                     foreach ($row as $value) {
                         $output .= "<td>$value</td>";
                     }
                     $output .= "</tr>";
                 }
                 $output .= "</table>";
                 return $output;
             } else {
                 return "0 results";
             }
         }

         function executeUpdate($conn, $query) {
            if ($conn->query($query) === TRUE) {
                return true;
            } else {
                return false;
            }
        }

        function executeInsert($conn, $query) {
            if ($conn->query($query) === TRUE) {
                return true;
            } else {
                return false;
            }
        }
 
         
         $feladat15 = "SELECT COUNT(id) as vetitesek_szama FROM vetites WHERE kezdes = '20:00'";
         echo "<div class='result'>Vetítések száma 20:00 órakor: " . executeQuery($conn, $feladat15, true) . "</div>";
 
         
         $feladat16 = "SELECT COUNT(jegy.id) as 'jegy_db', vendeg.nev as 'vendeg_nev' FROM jegy INNER JOIN vendeg on jegy.vendegId = vendeg.id GROUP BY (jegy.vendegId) ORDER BY jegy_db DESC LIMIT 5;";
         echo "16.feladat: " . executeQueryAsTable($conn, $feladat16) . "<br>";

        
        $updateQuery = "UPDATE film SET cim = 'Csillagok között' WHERE cim = 'Interstellar'";
        
        if (executeUpdate($conn, $updateQuery)) {
            echo "<div class='result'>17. feladat: a film címe sikeresen átírva <br></div>";
        } else {
            echo "Hiba a film címének átírásakor <br>";
        }

        
        $insertQuery = "INSERT INTO filmtipus (nev) SELECT 'Animációs' WHERE NOT EXISTS (SELECT 1 FROM filmtipus WHERE nev = 'Animációs')";
        if (executeInsert($conn, $insertQuery)) {
            echo "<div class='result'>18. feladat: Az új filmtípus hozzá lett adva <br></div>";
        } else {
            echo "Hiba a hozzáadáskor <br>";
        }

        $feladat19 = "SELECT nev as 'nev', count(jegy.id) as 'teljes_aru_jegy_db' FROM vendeg INNER JOIN jegy on vendeg.id = jegy.vendegId WHERE nev = 'Fodor András' and kedvezmeny = 0;";
        echo "19.feladat: " . executeQueryAsTable($conn, $feladat19) . "<br>";

        $feladat20 = "SELECT film.cim as 'film_neve', COUNT(jegy.id) as 'jegy_darabszam', SUM(film.jegyar - film.jegyar * jegy.kedvezmeny / 100) as 'teljes_bevetel'
        FROM film
        INNER JOIN vetites on film.id = vetites.filmId
        INNER JOIN jegy on vetites.id = jegy.vetitesId
        GROUP BY film.cim
        ORDER BY teljes_bevetel DESC LIMIT 1;";
        echo "20.feladat: " . executeQueryAsTable($conn, $feladat20) . "<br>";
 
         $conn->close();
    ?>
</body>
</html>