Загрузка данных



<?php
session_start();

// Пример массива товаров (замените на запрос к БД в реальном проекте)
$products = [
    1 => [
        'name' => 'Монитор AOC 24"',
        'image' => 'https://via.placeholder.com/150x100?text=Монитор+AOC',
        'price' => 15990
    ],
    2 => [
        'name' => 'Мышь Razer DeathAdder',
        'image' => 'https://via.placeholder.com/150x100?text=Razer+Mouse',
        'price' => 4990
    ],
    3 => [
        'name' => 'Клавиатура Logitech K120',
        'image' => 'https://via.placeholder.com/150x100?text=Logitech+K120',
        'price' => 3490
    ],
    4 => [
        'name' => 'Наушники Sony WH-1000XM4',
        'image' => 'https://via.placeholder.com/150x100?text=Sony+WH-1000XM4',
        'price' => 24990
    ],
];

// Обработка действий: изменение количества, удаление, очистка корзины
if (isset( $_GET['action'])) {
    if ( $_GET['action'] == 'remove' && isset( $_GET['id'])) {
        unset( $_SESSION['cart'][$_GET['id']]);
    } elseif ( $_GET['action'] == 'clear') {
        $_SESSION['cart'] = [];
    } elseif ( $_GET['action'] == 'update' && isset( $_GET['id']) && isset( $_GET['qty'])) {
        $qty = (int)$_GET['qty'];
        if ( $qty > 0) {
            $_SESSION['cart'][$_GET['id']] = $qty;
        } else {
            unset( $_SESSION['cart'][$_GET['id']]);
        }
    }
}

// Расчёт итогов
$total_items = 0;
$total_price = 0;
$cart_items = [];
if (!empty( $_SESSION['cart'])) {
    foreach ( $_SESSION['cart'] as $product_id => $qty) {
        if (isset( $products[$product_id])) {
            $product = $products[$product_id];
            $item_price = $product['price'] * $qty;
            $cart_items[] = [
                'id' => $product_id,
                'name' => $product['name'],
                'image' => $product['image'],
                'price' => $product['price'],
                'qty' => $qty,
                'item_price' => $item_price
            ];
            $total_items += $qty;
            $total_price += $item_price;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Корзина</title>
    <style>
        * { margin:0; padding:0; box-sizing:border-box; }
        body { font-family:Arial, sans-serif; background:#f4f4f4; color:#333; }
        .container { max-width:1200px; margin:auto; padding:20px; }
        .breadcrumbs { margin-bottom:20px; }
        .breadcrumbs a { color:#27ae60; text-decoration:none; }
        .cart-table { width:100%; border-collapse:collapse; margin-bottom:30px; }
        .cart-table th, .cart-table td { padding:12px; border-bottom:1px solid #ddd; text-align:left; }
        .cart-table th { background:#f9f9f9; font-weight:bold; }
        .cart-table img { width:80px; height:auto; border-radius:4px; }
        .quantity-input { width:60px; padding:5px; text-align:center; border:1px solid #ccc; border-radius:4px; }
        .cart-actions a { text-decoration:none; color:#fff; padding:6px 12px; border-radius:4px; margin-right:8px; transition:background .3s; }
        .btn-remove { background:#e74c3c; }
        .btn-remove:hover { background:#c0392b; }
        .btn-update { background:#27ae60; }
        .btn-update:hover { background:#2ecc71; }
        .order-summary { background:#fff; padding:20px; border-radius:8px; box-shadow:0 2px 8px rgba(0,0,0,.1); }
        .order-summary h3 { margin-bottom:15px; font-size:1.2em; }
        .summary-item { display:flex; justify-content:space-between; margin-bottom:10px; font-size:1.1em; }
        .summary-item strong { color:#e74c3c; }
        .btn-checkout { display:inline-block; background:#3498db; color:#fff; padding:12px 25px; text-decoration:none; border-radius:6px; font-weight:bold; transition:background .3s; margin-top:-38px;}
        .btn-checkout:hover { background:#2980b9; }
    </style>
</head>
<body>

<div class="container">

    <!-- Хлебные крошки -->
    <div class="breadcrumbs">
      <a href="index.php">Главная</a> > <a href="cart.php">Корзина</a>
    </div>

    <h1>Корзина</h1>

    <?php if (!empty( $cart_items)): ?>

      <!-- Таблица товаров в корзине -->
      <table class="cart-table">
          <thead>
              <tr>
                  <th>Товар</th>
                  <th>Цена</th>
                  <th>Количество</th>
                  <th>Сумма</th>
                  <th>Действия</th>
              </tr>
          </thead>
          <tbody>
              <?php foreach ( $cart_items as $item): ?>
                  <tr>
                      <td>
                          <img alt="<?= htmlspecialchars( $item['name']) ?>" alt="<?= htmlspecialchars( $item['name']) ?>">
                          <?= htmlspecialchars( $item['name']) ?>
                      </td>
                      <td><?= number_format( $item['price'], 0, '', ' ') ?> ₽</td>
                      <td>
                          <form method="GET" style="display:inline-block;">
                              <input type="hidden" name="action" value="update">
                              <input type="hidden" name="id" value="<?= $item['id'] ?>">
                              <input type="number" name="qty" value="<?= $item['qty'] ?>" min="1" class="quantity-input">
                              <button type="submit" class="btn-update">Обновить</button>
                          </form>
                      </td>
                      <td><?= number_format( $item['item_price'], 0, '', ' ') ?> ₽</td>
                      <td>
                          <a href="?action=remove&id=<?= $item['id'] ?>" class="cart-actions btn-remove">Удалить</a>
                      </td>
                  </tr>
              <?php endforeach; ?>
          </tbody>
      </table>

      <!-- Итоги заказа -->
      <div class="order-summary">
          <h3>Итого к оплате:</h3>
          <div class="summary-item"><strong>Товаров:</strong> <?= $total_items ?> шт.</div>
          <div class="summary-item"><strong>Сумма:</strong> <?= number_format( $total_price, 0, '', ' ') ?> ₽</div>
          <a href="checkout.php" class="btn-checkout">Перейти к оформлению заказа</a>
      </div>

      <!-- Кнопка очистки корзины -->
      <p><a href="?action=clear" class="cart-actions btn-remove">Очистить корзину</a></p>

    <?php else: ?>
      <p>Ваша корзина пуста. <a href="index.php">Вернуться к покупкам</a></p>
    <?php endif; ?>

</div>

</body>
</html>