<?php
session_start();

// Перевіряємо, чи користувач увійшов
if (!isset($_SESSION['username'])) {
    header('Location: login.php'); // Якщо користувач не увійшов, перенаправляємо на сторінку входу
    exit();
}

// Логіка виходу з облікового запису
if (isset($_POST['logout'])) {
    session_destroy();
    header('Location: login.php'); // Після виходу перенаправляємо на сторінку входу
    exit();
}

// Логіка додавання коментаря
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['comment'])) {
    $comment = trim($_POST['comment']);
    $username = $_SESSION['username'];

    if (!empty($comment)) {
        // Зчитування існуючих коментарів
        $comments = json_decode(file_get_contents('comments.json'), true) ?? [];
        
        // Додавання нового коментаря
        $comments[] = [
            'username' => $username,
            'comment' => $comment,
            'timestamp' => date('Y-m-d H:i:s')
        ];

        // Запис коментарів у файл
        file_put_contents('comments.json', json_encode($comments, JSON_PRETTY_PRINT));
 header('Location: index.php');    
}
}

// Зчитування існуючих коментарів
$comments = json_decode(file_get_contents('comments.json'), true) ?? [];
?>

<!DOCTYPE html>
<html lang="uk">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MSI Магазин</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 20px;
            padding: 0;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 20px;
            border-bottom: 2px solid #ddd;
        }
        .header h2 {
            margin: 0;
        }
        .products {
            margin-top: 20px;
        }
        .product {
            border: 1px solid #ddd;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 10px;
            background-color: #f9f9f9;
        }
        .product h3 {
            margin: 0 0 10px 0;
            color: #333;
        }
        .product p {
            margin: 5px 0;
            color: #666;
        }
        .comments-section {
            margin-top: 40px;
            padding-top: 20px;
            border-top: 2px solid #ddd;
        }
        .comments-section h3 {
            margin-bottom: 10px;
            color: #333;
        }
        .comment {
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
        }
        .comment .username {
            font-weight: bold;
            color: #0056b3;
        }
        .comment .timestamp {
            font-size: 0.8em;
            color: #888;
            margin-left: 10px;
        }
        form textarea {
            width: 100%;
            height: 100px;
            padding: 10px;
            margin-top: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        form input[type="submit"] {
            margin-top: 10px;
            padding: 10px 20px;
            background-color: #0056b3;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        form input[type="submit"]:hover {
            background-color: #004499;
        }
        form .logout-btn {
            background-color: #dc3545;
            margin-left: 10px;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="header">
        <h2>Ласкаво просимо, <?php echo htmlspecialchars($_SESSION['username']); ?>!</h2>
        <form method="post" action="">
            <input type="submit" name="logout" value="Вийти" class="logout-btn">
        </form>
    </div>

    <h1>Магазин ноутбуків MSI</h1>

    <!DOCTYPE html>
<html lang="uk">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Слайдер з фото</title>
    <style>
        /* Стилі для слайдера */
        .slider {
            position: relative;
            max-width: 600px;
            margin: auto;
        }

        .slides {
            display: none;
        }

        img {
            width: 100%;
            height: auto;
        }

        /* Кнопки "Вперед" і "Назад" */
        .prev, .next {
            cursor: pointer;
            position: absolute;
            top: 50%;
            width: auto;
            padding: 16px;
            color: white;
            font-weight: bold;
            font-size: 18px;
            transition: 0.6s ease;
            border-radius: 0 3px 3px 0;
            user-select: none;
        }

        .next {
            right: 0;
            border-radius: 3px 0 0 3px;
        }

        .prev {
            left: 0;
        }

        .dots {
            text-align: center;
            padding: 10px;
        }

        .dot {
            cursor: pointer;
            height: 15px;
            width: 15px;
            margin: 0 2px;
            background-color: #bbb;
            border-radius: 50%;
            display: inline-block;
            transition: background-color 0.6s ease;
        }

        .active, .dot:hover {
            background-color: #717171;
        }
    </style>
</head>
<body>

<div class="slider">
    <div class="slides">
        <img src="1.jpg" alt="Фото 1">
    </div>

    <div class="slides">
        <img src="2.jpg" alt="Фото 2">
    </div>

    <div class="slides">
        <img src="3.png" alt="Фото 3">
    </div>

    <!-- Кнопки для перемикання слайдів -->
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

<!-- Точки для вибору слайдів -->
<div class="dots">
    <span class="dot" onclick="currentSlide(1)"></span> 
    <span class="dot" onclick="currentSlide(2)"></span> 
    <span class="dot" onclick="currentSlide(3)"></span> 
</div>

<script>
    let slideIndex = 1;
    showSlides(slideIndex);

    function plusSlides(n) {
        showSlides(slideIndex += n);
    }

    function currentSlide(n) {
        showSlides(slideIndex = n);
    }

    function showSlides(n) {
        let i;
        let slides = document.getElementsByClassName("slides");
        let dots = document.getElementsByClassName("dot");
        if (n > slides.length) {slideIndex = 1}    
        if (n < 1) {slideIndex = slides.length}
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";  
        }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex-1].style.display = "block";  
        dots[slideIndex-1].className += " active";
    }
</script>

</body>
</html>

    <div class="products">
        <div class="product">
            <h3>MSI Stealth 15M</h3>
            <p>Ціна: $1,499</p>
            <p>Процесор: Intel Core i7-11375H</p>
            <p>Графіка: NVIDIA GeForce RTX 3060</p>
            <p>ОЗУ: 16 ГБ DDR4</p>
            <p>Екран: 15.6" FHD 144Hz</p>
        </div>
<!DOCTYPE html>
<html lang="uk">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Слайдер з фото</title>
    <style>
        /* Стилі для слайдера */
        .slider {
            position: relative;
            max-width: 600px;
            margin: auto;
        }

        .slides {
            display: none;
        }

        img {
            width: 100%;
            height: auto;
        }

        /* Кнопки "Вперед" і "Назад" */
        .prev, .next {
            cursor: pointer;
            position: absolute;
            top: 50%;
            width: auto;
            padding: 16px;
            color: white;
            font-weight: bold;
            font-size: 18px;
            transition: 0.6s ease;
            border-radius: 0 3px 3px 0;
            user-select: none;
        }

        .next {
            right: 0;
            border-radius: 3px 0 0 3px;
        }

        .prev {
            left: 0;
        }

        .dots {
            text-align: center;
            padding: 10px;
        }

        .dot {
            cursor: pointer;
            height: 15px;
            width: 15px;
            margin: 0 2px;
            background-color: #bbb;
            border-radius: 50%;
            display: inline-block;
            transition: background-color 0.6s ease;
        }

        .active, .dot:hover {
            background-color: #717171;
        }
    </style>
</head>
<body>

<div class="slider2">
    <div class="slides">
        <img src="4.jpg" alt="Фото 1">
    </div>

    <div class="slides">
        <img src="5.jpg" alt="Фото 2">
    </div>

    <div class="slides">
        <img src="6.jpg" alt="Фото 3">
    </div>

    <!-- Кнопки для перемикання слайдів -->
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

<!-- Точки для вибору слайдів -->
<div class="dots2">
    <span class="dot" onclick="currentSlide(4)"></span> 
    <span class="dot" onclick="currentSlide(5)"></span> 
    <span class="dot" onclick="currentSlide(6)"></span> 
</div>

<script>
    let slideIndex = 1;
    showSlides(slideIndex);

    function plusSlides(n) {
        showSlides(slideIndex += n);
    }

    function currentSlide(n) {
        showSlides(slideIndex = n);
    }

    function showSlides(n) {
        let i;
        let slides = document.getElementsByClassName("slides");
        let dots = document.getElementsByClassName("dot");
        if (n > slides.length) {slideIndex = 1}    
        if (n < 1) {slideIndex = slides.length}
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";  
        }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex-1].style.display = "block";  
        dots[slideIndex-1].className += " active";
    }
</script>

</body>
</html>

        <div class="product">
            <h3>MSI GF63 Thin</h3>
            <p>Ціна: $899</p>
            <p>Процесор: Intel Core i5-10500H</p>
            <p>Графіка: NVIDIA GeForce GTX 1650</p>
            <p>ОЗУ: 8 ГБ DDR4</p>
            <p>Екран: 15.6" FHD</p>
        </div>
<!DOCTYPE html>
<html lang="uk">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Слайдер з фото</title>
    <style>
        /* Стилі для слайдера */
        .slider {
            position: relative;
            max-width: 600px;
            margin: auto;
        }

        .slides {
            display: none;
        }

        img {
            width: 100%;
            height: auto;
        }

        /* Кнопки "Вперед" і "Назад" */
        .prev, .next {
            cursor: pointer;
            position: absolute;
            top: 50%;
            width: auto;
            padding: 16px;
            color: white;
            font-weight: bold;
            font-size: 18px;
            transition: 0.6s ease;
            border-radius: 0 3px 3px 0;
            user-select: none;
        }

        .next {
            right: 0;
            border-radius: 3px 0 0 3px;
        }

        .prev {
            left: 0;
        }

        .dots {
            text-align: center;
            padding: 10px;
        }

        .dot {
            cursor: pointer;
            height: 15px;
            width: 15px;
            margin: 0 2px;
            background-color: #bbb;
            border-radius: 50%;
            display: inline-block;
            transition: background-color 0.6s ease;
        }

        .active, .dot:hover {
            background-color: #717171;
        }
    </style>
</head>
<body>

<div class="slide3">
    <div class="slides">
        <img src="10.jpg" alt="Фото 1">
    </div>

    <div class="slides">
        <img src="11.jpg" alt="Фото 2">
    </div>

    <div class="slides">
        <img src="12.jpg" alt="Фото 3">
    </div>

    <!-- Кнопки для перемикання слайдів -->
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

<!-- Точки для вибору слайдів -->
<div class="dot3">
    <span class="dot" onclick="currentSlide(7)"></span> 
    <span class="dot" onclick="currentSlide(8)"></span> 
    <span class="dot" onclick="currentSlide(9)"></span> 
</div>

<script>
    let slideIndex = 1;
    showSlides(slideIndex);

    function plusSlides(n) {
        showSlides(slideIndex += n);
    }

    function currentSlide(n) {
        showSlides(slideIndex = n);
    }

    function showSlides(n) {
        let i;
        let slides = document.getElementsByClassName("slides");
        let dots = document.getElementsByClassName("dot");
        if (n > slides.length) {slideIndex = 1}    
        if (n < 1) {slideIndex = slides.length}
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";  
        }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex-1].style.display = "block";  
        dots[slideIndex-1].className += " active";
    }
</script>

</body>
</html>

        <div class="product">
            <h3>MSI Raider GE76</h3>
            <p>Ціна: $2,599</p>
            <p>Процесор: Intel Core i9-11980HK</p>
            <p>Графіка: NVIDIA GeForce RTX 3080</p>
            <p>ОЗУ: 32 ГБ DDR4</p>
            <p>Екран: 17.3" FHD 300Hz</p>
        </div>
<!DOCTYPE html>
<html lang="uk">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Слайдер з фото</title>
    <style>
        /* Стилі для слайдера */
        .slider {
            position: relative;
            max-width: 600px;
            margin: auto;
        }

        .slides {
            display: none;
        }

        img {
            width: 100%;
            height: auto;
        }

        /* Кнопки "Вперед" і "Назад" */
        .prev, .next {
            cursor: pointer;
            position: absolute;
            top: 50%;
            width: auto;
            padding: 16px;
            color: white;
            font-weight: bold;
            font-size: 18px;
            transition: 0.6s ease;
            border-radius: 0 3px 3px 0;
            user-select: none;
        }

        .next {
            right: 0;
            border-radius: 3px 0 0 3px;
        }

        .prev {
            left: 0;
        }

        .dots {
            text-align: center;
            padding: 10px;
        }

        .dot {
            cursor: pointer;
            height: 15px;
            width: 15px;
            margin: 0 2px;
            background-color: #bbb;
            border-radius: 50%;
            display: inline-block;
            transition: background-color 0.6s ease;
        }

        .active, .dot:hover {
            background-color: #717171;
        }
    </style>
</head>
<body>

<div class="slider4">
    <div class="slides">
        <img src="7.jpg" alt="Фото 1">
    </div>

    <div class="slides">
        <img src="8.jpg" alt="Фото 2">
    </div>

    <div class="slides">
        <img src="9.jpg" alt="Фото 3">
    </div>

    <!-- Кнопки для перемикання слайдів -->
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

  <!-- Точки для вибору слайдів -->
<div class="dots4">
    <span class="dot" onclick="currentSlide(10)"></span> 
    <span class="dot" onclick="currentSlide(11)"></span> 
    <span class="dot" onclick="currentSlide(12)"></span> 
</div>

<script>
    let slideIndex = 1;
    showSlides(slideIndex);

    function plusSlides(n) {
        showSlides(slideIndex += n);
    }

    function currentSlide(n) {
        showSlides(slideIndex = n);
    }

    function showSlides(n) {
        let i;
        let slides = document.getElementsByClassName("slides");
        let dots = document.getElementsByClassName("dot");
        if (n > slides.length) {slideIndex = 1}    
        if (n < 1) {slideIndex = slides.length}
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";  
        }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex-1].style.display = "block";  
        dots[slideIndex-1].className += " active";
    }
</script>

</body>
</html>

        <div class="product">
            <h3>MSI Creator Z16</h3>
            <p>Ціна: $2,399</p>
            <p>Процесор: Intel Core i7-11800H</p>
            <p>Графіка: NVIDIA GeForce RTX 3060</p>
            <p>ОЗУ: 32 ГБ DDR4</p>
            <p>Екран: 16" QHD+ 120Hz</p>
        </div>
    </div>

    <div class="comments-section">
        <h3>Коментарі</h3>
<?php
// Завантаження коментарів із JSON
$comments = json_decode(file_get_contents('comments.json'), true);

// Виведення коментарів
foreach ($comments as $comment) {
    echo "<div class='comment'>";
    echo "<p><strong>" . $comment['username'] . ":</strong></p>"; // Вразливість тут
    echo "<p>" . $comment['comment'] . "</p>"; // Вразливість тут
    echo "<small>" . $comment['timestamp'] . "</small>";
    echo "</div>";
}
?>


<h3>Залишити коментар</h3>
<form method="post" action="">
    <label for="comment">Ваш коментар:</label><br>
    <textarea name="comment" placeholder="Ваш коментар..."></textarea> <!-- Виправлений тег -->
    <input type="submit" value="Залишити коментар">
</form>

<?php
// Обробка надісланого коментаря
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Перевірка, чи коментар не пустий
    if (!empty($_POST['comment'])) {
        $newComment = [
            'username' => 'Ваше ім’я', // Ви можете змінити це, щоб взяти ім'я з форми або сесії
            'comment' => $_POST['comment'],
            'timestamp' => date('Y-m-d H:i:s')
        ];

        // Додайте новий коментар до масиву
        $comments[] = $newComment;

        // Запишіть оновлений масив коментарів назад у файл JSON
        file_put_contents('comments.json', json_encode($comments, JSON_PRETTY_PRINT));
        
        // Оновіть сторінку, щоб відобразити новий коментар
        header('Location: ' . $_SERVER['PHP_SELF']);
        exit;
    }
}
?>

          
</div>
</div>

</body>

</html>   

