<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'connect.php';
session_start();

if (!isset($_SESSION['user_id'])) {
    die("กรุณาเข้าสู่ระบบก่อน");
}

//เพิ่มสินค้าเข้า cart
if (isset($_GET['add'])) {
    $stmt = $conn->prepare("
        INSERT INTO cart (id, users_id, products_id, qty)
        VALUES (NULL, ?, ?, 1)
        ON DUPLICATE KEY UPDATE qty = qty + 1
    ");
    $stmt->execute([$_SESSION['user_id'], $_GET['add']]);
    header("Location: cart.php");
    exit;
}

//อัปเดตจำนวนสินค้า
if (isset($_GET['update']) && isset($_GET['qty'])) {
    $qty = max(1, intval($_GET['qty'])); // ป้องกันค่าติดลบ
    $stmt = $conn->prepare("UPDATE cart SET qty = ? WHERE users_id = ? AND products_id = ?");
    $stmt->execute([$qty, $_SESSION['user_id'], $_GET['update']]);
    header("Location: cart.php");
    exit;
}

//ลบสินค้าออกจาก cart
if (isset($_GET['remove'])) {
    $stmt = $conn->prepare("DELETE FROM cart WHERE users_id = ? AND products_id = ?");
    $stmt->execute([$_SESSION['user_id'], $_GET['remove']]);
    header("Location: cart.php");
    exit;
}

//ดึงข้อมูลจาก cart และ products
$stmt = $conn->prepare("
    SELECT cart.*, products.name, products.price 
    FROM cart 
    JOIN products ON cart.products_id = products.id 
    WHERE cart.users_id = ?
");
$stmt->execute([$_SESSION['user_id']]);
$cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<h2>ตะกร้าสินค้า</h2>

<?php if (empty($cart_items)): ?>
    <p>ไม่มีสินค้าในตะกร้า</p>
<?php else: ?>
    <table border="1" cellpadding="5" cellspacing="0">
        <tr>
            <th>User ID</th>
            <th>รหัสสินค้า</th>
            <th>ชื่อสินค้า</th>
            <th>ราคา</th>
            <th>จำนวน</th>
            <th>รวม</th>
            <th>การจัดการ</th>
        </tr>
    
        <?php 
        $total = 0;
        foreach ($cart_items as $item): 
            $subtotal = $item['price'] * $item['qty'];
            $total += $subtotal;
        ?>
            <tr>
                <td><?= $item['users_id'] ?></td>
                <td><?= $item['products_id'] ?></td>
                <td><?= htmlspecialchars($item['name']) ?></td>
                <td><?= number_format($item['price'], 2) ?> บาท</td>
                <td>
                    <a href="cart.php?update=<?= $item['products_id'] ?>&qty=<?= max(1, $item['qty'] - 1) ?>">➖</a>
                    <?= $item['qty'] ?>
                    <a href="cart.php?update=<?= $item['products_id'] ?>&qty=<?= $item['qty'] + 1 ?>">➕</a>
                </td>
                <td><?= number_format($subtotal, 2) ?> บาท</td>
                <td><a href="cart.php?remove=<?= $item['products_id'] ?>" onclick="return confirm('ลบสินค้านี้ออกจากตะกร้า?')">ลบ</a></td>
            </tr>
        <?php endforeach; ?>
        <tr>
            <td colspan="5" align="right"><strong>ยอดรวมทั้งหมด:</strong></td>
            <td colspan="2"><strong><?= number_format($total, 2) ?> บาท</strong></td>
        </tr>
    </table>
    <br>
    <a href="checkout.php">🛒 ดำเนินการชำระเงิน</a>
<?php endif; ?>