<?php
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/helpers/jwt_helper.php';
require_once __DIR__ . '/helpers/auth.php';

// เปิดใช้งาน Session ตะกร้าสินค้า
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = [];
}

$currentUser = get_current_user_data();

// จัดการการเพิ่มสินค้าลงตะกร้าพร้อมเลือกไซส์และสี
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_to_cart'])) {
    $product_id = (int)$_POST['product_id'];
    $quantity = (int)($_POST['quantity'] ?? 1);
    $size = trim($_POST['size'] ?? 'N/A'); 
    $color = trim($_POST['color'] ?? 'N/A'); 

    if ($product_id > 0 && $quantity > 0) {
        // ตรวจสอบสต็อกสินค้าก่อน
        $stmt = $pdo->prepare("SELECT stock, name, category FROM products WHERE id = ?");
        $stmt->execute([$product_id]);
        $prod = $stmt->fetch();

        if ($prod) {
            // หากไม่ใช่หมวดเสื้อผ้า ให้เซ็ตเป็น N/A เสมอ
            if (($prod['category'] ?? 'general') !== 'clothing') {
                $size = 'N/A';
                $color = 'N/A';
            }
            
            $cart_key = $product_id . '_' . $size . '_' . $color; // คีย์ผสม 3 ส่วน
            $current_in_cart = $_SESSION['cart'][$cart_key] ?? 0;
            
            if ($current_in_cart + $quantity <= $prod['stock']) {
                $_SESSION['cart'][$cart_key] = $current_in_cart + $quantity;
                $msg = 'เพิ่ม "' . htmlspecialchars($prod['name']) . '"';
                if ($size !== 'N/A') {
                    $msg .= ' (ไซส์ ' . htmlspecialchars($size) . ', สี' . htmlspecialchars($color) . ')';
                }
                $cart_success = $msg . ' ลงตะกร้าเรียบร้อย!';
            } else {
                $cart_error = 'ขออภัย สินค้าในคลังมีไม่เพียงพอสำหรับการสั่งซื้อเพิ่มเติม';
            }
        }
    }
}

// ค้นหาและดึงสินค้า
$search = trim($_GET['search'] ?? '');
$category_filter = trim($_GET['category'] ?? '');
$sort = trim($_GET['sort'] ?? 'newest');

try {
    $sql = "SELECT * FROM products WHERE 1=1";
    $params = [];

    if (!empty($search)) {
        $sql .= " AND (name LIKE ? OR description LIKE ?)";
        $params[] = "%$search%";
        $params[] = "%$search%";
    }

    if (!empty($category_filter)) {
        $sql .= " AND category = ?";
        $params[] = $category_filter;
    }

    $order_clause = "id DESC";
    if ($sort === 'price_low') {
        $order_clause = "price ASC";
    } elseif ($sort === 'price_high') {
        $order_clause = "price DESC";
    } elseif ($sort === 'in_stock') {
        $order_clause = "stock DESC, id DESC";
    }

    $sql .= " ORDER BY $order_clause";
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    $products = $stmt->fetchAll();
} catch (PDOException $e) {
    die("Error fetching catalog: " . $e->getMessage());
}

$cart_count = array_sum($_SESSION['cart']);
?>
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CyberShop // ร้านขายของออนไลน์ยุคอนาคต</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        .product-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 2rem;
            margin-top: 2rem;
        }
        .product-card {
            background: var(--bg-card);
            border: 1px solid var(--border-color);
            border-radius: 12px;
            overflow: hidden;
            transition: all 0.3s ease;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        .product-card:hover {
            border-color: var(--neon-red);
            box-shadow: 0 0 15px var(--glow-light);
            transform: translateY(-5px);
        }
        .product-image {
            width: 100%;
            height: 240px;
            object-fit: cover;
            border-bottom: 1px solid var(--border-color);
        }
        .product-content {
            padding: 1.5rem;
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        .product-title {
            font-size: 1.25rem;
            margin-bottom: 0.5rem;
            color: var(--text-white);
        }
        .product-price {
            font-family: 'Orbitron', sans-serif;
            color: var(--neon-red);
            font-size: 1.4rem;
            font-weight: 700;
            margin: 0.5rem 0;
            text-shadow: 0 0 5px var(--glow-light);
        }
        .cat-btn {
            background: transparent;
            border: 1px solid var(--border-color);
            color: var(--text-gray);
            padding: 0.5rem 1.2rem;
            border-radius: 20px;
            cursor: pointer;
            font-family: 'Orbitron', sans-serif;
            font-size: 0.85rem;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        .cat-btn:hover, .cat-btn.active {
            border-color: var(--neon-red);
            color: var(--text-white);
            box-shadow: 0 0 10px var(--glow-light);
        }
    </style>
</head>
<body>

    <nav class="navbar">
        <a href="index.php" class="navbar-brand">
            CYBER<span class="brand-red">SHOP</span>
        </a>
        
        <!-- แถบค้นหา -->
        <form action="index.php" method="GET" style="display: flex; gap: 0.5rem; flex-grow: 0.5; max-width: 400px; margin: 0 1rem;">
            <input class="cyber-input" type="text" name="search" placeholder="ค้นหาสินค้า เสื้อผ้า กระเป๋า..." value="<?php echo htmlspecialchars($search); ?>" style="padding: 0.5rem 1rem;">
            <?php if (!empty($category_filter)): ?>
                <input type="hidden" name="category" value="<?php echo htmlspecialchars($category_filter); ?>">
            <?php endif; ?>
            <button type="submit" class="btn-cyber" style="padding: 0.5rem 1.2rem;">ค้นหา</button>
        </form>

        <div class="navbar-menu">
            <a href="index.php" class="nav-link active">หน้าร้านค้า</a>
            <a href="cart.php" class="nav-link" style="color: var(--text-white); position: relative;">
                ตะกร้าสินค้า 
                <span style="background: var(--neon-red); color: #fff; padding: 2px 7px; border-radius: 50%; font-size: 0.75rem; font-family: 'Orbitron', sans-serif; margin-left: 4px; box-shadow: 0 0 8px var(--neon-red);">
                    <?php echo $cart_count; ?>
                </span>
            </a>

            <!-- ช่องใส่โค้ดส่วนลดใน Navbar -->
            <div style="margin-left: 0.5rem; display: flex; align-items: center;">
                <?php if (!empty($_SESSION['coupon']['code'])): ?>
                    <div style="background: rgba(0, 240, 255, 0.1); border: 1px solid var(--neon-cyan); padding: 0.3rem 0.6rem; border-radius: 6px; display: flex; align-items: center; gap: 6px; font-size: 0.8rem; box-shadow: 0 0 10px rgba(0, 240, 255, 0.2);">
                        <span style="color: var(--neon-cyan); font-family: 'Orbitron', sans-serif; font-weight: bold;">🏷️ <?php echo htmlspecialchars($_SESSION['coupon']['code']); ?> (-10%)</span>
                        <a href="?action=remove_nav_coupon" style="color: var(--neon-red); text-decoration: none; font-weight: bold; margin-left: 2px;" title="ยกเลิกโค้ด">✕</a>
                    </div>
                <?php else: ?>
                    <form action="" method="POST" style="display: flex; gap: 0.3rem; align-items: center;">
                        <input class="cyber-input" type="text" name="coupon_code" placeholder="🎟️ โค้ดลด 10%" style="padding: 0.35rem 0.6rem; font-size: 0.8rem; width: 120px; text-transform: uppercase; font-family: 'Orbitron', sans-serif; border-color: rgba(0, 240, 255, 0.5); color: var(--neon-cyan);">
                        <button type="submit" name="apply_nav_coupon" class="btn-cyber" style="padding: 0.35rem 0.65rem; font-size: 0.75rem; border-color: var(--neon-cyan); color: var(--neon-cyan); background: rgba(0, 240, 255, 0.1); white-space: nowrap;">ใช้โค้ด</button>
                    </form>
                <?php endif; ?>
            </div>
            
            <?php if ($currentUser): ?>
                <div style="margin-left: 1rem; padding-left: 1rem; border-left: 1px solid var(--border-color); display: flex; align-items: center; gap: 0.8rem;">
                    <a href="<?php echo (in_array($currentUser['role'], ['admin', 'super_admin'])) ? 'admin/profile.php' : 'profile.php'; ?>" style="text-decoration: none; display: flex; align-items: center; gap: 6px;" title="จัดการโปรไฟล์">
                        <?php if (!empty($currentUser['avatar']) && file_exists(__DIR__ . '/' . $currentUser['avatar'])): ?>
                            <img src="<?php echo htmlspecialchars($currentUser['avatar']); ?>" style="width: 28px; height: 28px; border-radius: 50%; object-fit: cover; border: 1.5px solid var(--neon-green);">
                        <?php elseif (!empty($currentUser['avatar']) && strpos($currentUser['avatar'], 'http') === 0): ?>
                            <img src="<?php echo htmlspecialchars($currentUser['avatar']); ?>" style="width: 28px; height: 28px; border-radius: 50%; object-fit: cover; border: 1.5px solid var(--neon-green);">
                        <?php endif; ?>
                        <span style="font-size: 0.9rem; color: var(--text-white);">
                            สวัสดี, <span class="brand-red" style="font-weight: bold;"><?php echo htmlspecialchars($currentUser['username']); ?></span>
                        </span>
                    </a>
                    <?php if (in_array($currentUser['role'], ['admin', 'super_admin'])): ?>
                        <a href="admin/dashboard.php" class="btn-cyber btn-green" style="padding: 0.4rem 0.8rem; font-size: 0.75rem;">หลังบ้านแอดมิน</a>
                    <?php else: ?>
                        <a href="profile.php" class="btn-cyber" style="padding: 0.4rem 0.8rem; font-size: 0.75rem; border-color: var(--neon-green); color: var(--neon-green);">👤 โปรไฟล์</a>
                    <?php endif; ?>
                    <a href="logout.php" class="btn-cyber" style="padding: 0.4rem 0.8rem; font-size: 0.75rem; border-color: var(--text-gray);">ออกระบบ</a>
                </div>
            <?php else: ?>
                <div style="margin-left: 1rem; padding-left: 1rem; border-left: 1px solid var(--border-color);">
                    <a href="login.php" class="btn-cyber" style="padding: 0.4rem 1rem; font-size: 0.8rem;">เข้าสู่ระบบ</a>
                </div>
            <?php endif; ?>
        </div>
    </nav>

    <!-- Announcement Ticker แบบวิ่ง 2 โปรโมชั่น -->
    <div style="background: #0a0a0a; border-bottom: 1px solid rgba(255, 0, 85, 0.3); padding: 0; overflow: hidden; position: relative;">
        <div style="display: flex; align-items: center; height: 38px;">
            <div style="background: linear-gradient(90deg, #ff0055, #b30040); padding: 0 16px; height: 100%; display: flex; align-items: center; font-family: 'Orbitron', sans-serif; font-size: 0.7rem; font-weight: 900; letter-spacing: 2px; white-space: nowrap; flex-shrink: 0;">📢 PROMO</div>
            <div style="overflow: hidden; flex: 1;">
                <div style="display: flex; animation: ticker-scroll 28s linear infinite; white-space: nowrap; align-items: center; gap: 0;">
                    <span style="padding: 0 40px; font-size: 0.85rem; font-weight: bold; color: #fff; letter-spacing: 1px;">🔥 โค้ด <span style="color: #00f0ff; font-family: 'Orbitron', sans-serif;">CYBER10</span> รับส่วนลดทันที 10% ทุกรายการ</span>
                    <span style="padding: 0 20px; color: rgba(255,0,85,0.6);">|</span>
                    <span style="padding: 0 40px; font-size: 0.85rem; font-weight: bold; color: #ffd700; letter-spacing: 1px;">💰 ซื้อครบ <span style="color: #ff6b00;">฿2,000</span> รับส่วนลดสูงสุด <span style="color: #ffd700; font-family: 'Orbitron', sans-serif; font-size: 1rem;">67%</span> ทันที!</span>
                    <span style="padding: 0 20px; color: rgba(255,0,85,0.6);">|</span>
                    <span style="padding: 0 40px; font-size: 0.85rem; font-weight: bold; color: #fff; letter-spacing: 1px;">🔥 โค้ด <span style="color: #00f0ff; font-family: 'Orbitron', sans-serif;">CYBER10</span> รับส่วนลดทันที 10% ทุกรายการ</span>
                    <span style="padding: 0 20px; color: rgba(255,0,85,0.6);">|</span>
                    <span style="padding: 0 40px; font-size: 0.85rem; font-weight: bold; color: #ffd700; letter-spacing: 1px;">💰 ซื้อครบ <span style="color: #ff6b00;">฿2,000</span> รับส่วนลดสูงสุด <span style="color: #ffd700; font-family: 'Orbitron', sans-serif; font-size: 1rem;">67%</span> ทันที!</span>
                    <span style="padding: 0 20px; color: rgba(255,0,85,0.6);">|</span>
                </div>
            </div>
        </div>
    </div>
    <style>
    @keyframes ticker-scroll {
        0% { transform: translateX(0); }
        100% { transform: translateX(-50%); }
    }
    @keyframes pulse-glow {
        0%, 100% { box-shadow: 0 0 20px rgba(255, 180, 0, 0.3), 0 0 40px rgba(255, 100, 0, 0.1); }
        50% { box-shadow: 0 0 35px rgba(255, 180, 0, 0.6), 0 0 70px rgba(255, 100, 0, 0.3); }
    }
    @keyframes badge-pulse {
        0%, 100% { transform: scale(1); }
        50% { transform: scale(1.06); }
    }
    </style>

    <div class="container" style="margin-top: 2rem;">

        <?php if (isset($_GET['registered']) && $_GET['registered'] == 1): ?>
            <div class="cyber-card" style="border-color: var(--neon-green); background: rgba(0, 255, 102, 0.08); margin-bottom: 2rem; padding: 1.2rem 1.8rem; box-shadow: 0 0 25px rgba(0, 255, 102, 0.25); display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 1rem;">
                <div style="display: flex; align-items: center; gap: 12px;">
                    <span style="font-size: 2rem;">🎉</span>
                    <div>
                        <h3 style="color: var(--neon-green); margin: 0; font-size: 1.2rem; font-family: 'Orbitron', sans-serif;">ยินดีต้อนรับสมาชิกใหม่! คุณได้รับส่วนลดสมาชิก 10% ทันที</h3>
                        <p style="color: #fff; margin: 4px 0 0 0; font-size: 0.9rem;">ส่วนลดถูกเปิดใช้งานสำหรับบัญชีสมาชิกของคุณโดยอัตโนมัติแล้ว สนุกกับการเลือกช้อปสินค้าได้เลย!</p>
                    </div>
                </div>
                <a href="index.php" class="btn-cyber btn-green" style="padding: 0.5rem 1.2rem; font-size: 0.85rem; text-decoration: none;">ตกลง</a>
            </div>
        <?php endif; ?>
        
        <!-- การ์ดโฆษณาโปรโมชั่นลด 10% สำหรับสมาชิก -->
        <div style="background: radial-gradient(circle at top right, rgba(0, 255, 102, 0.15), rgba(0, 240, 255, 0.05) 70%), #12141a; border: 1px solid var(--neon-green); border-radius: 12px; padding: 1.8rem 2rem; margin-bottom: 2.5rem; position: relative; overflow: hidden; box-shadow: 0 0 25px rgba(0, 255, 102, 0.18);">
            <div style="position: absolute; right: -25px; top: -25px; background: var(--neon-green); color: #000; font-family: 'Orbitron', sans-serif; font-size: 0.85rem; font-weight: 900; padding: 32px 40px 10px; transform: rotate(45deg); box-shadow: 0 0 15px var(--glow-green); text-align: center; letter-spacing: 1px;">
                MEMBER -10%
            </div>
            <div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1.5rem;">
                <div style="max-width: 600px;">
                    <div style="display: inline-flex; align-items: center; gap: 8px; background: rgba(0, 255, 102, 0.12); border: 1px solid var(--neon-green); padding: 4px 12px; border-radius: 20px; color: var(--neon-green); font-size: 0.8rem; font-weight: bold; margin-bottom: 0.8rem; letter-spacing: 1px;">
                        <span style="width: 8px; height: 8px; background: var(--neon-green); border-radius: 50%; display: inline-block; box-shadow: 0 0 8px var(--neon-green);"></span> MEMBER PRIVILEGE ACTIVE
                    </div>
                    <h2 style="font-family: 'Orbitron', sans-serif; font-size: 1.8rem; color: #fff; text-shadow: 0 0 10px var(--glow-green); margin-bottom: 0.5rem; line-height: 1.2;">
                        MEMBER SPECIAL <span style="color: var(--neon-green);">10% DISCOUNT</span>
                    </h2>
                    <p style="color: var(--text-gray); font-size: 0.95rem; margin: 0;">
                        <?php if ($currentUser): ?>
                            คุณเป็นสมาชิกแล้ว! ได้รับสิทธิ์ส่วนลดพิเศษสมาชิก <strong style="color: var(--neon-green);">ลด 10% ทันทีทุกสินค้า</strong> ในระบบอัตโนมัติ
                        <?php else: ?>
                            สมัครสมาชิกลูกค้าใหม่กับ CYBERSHOP วันนี้ รับสิทธิ์ส่วนลดสมาชิก <strong style="color: var(--neon-green);">ลด 10% ทันทีทุกรายการ</strong> ตั้งแต่ชิ้นแรก!
                        <?php endif; ?>
                    </p>
                </div>
                <div style="display: flex; flex-direction: column; align-items: center;">
                    <?php if ($currentUser): ?>
                        <div style="background: rgba(0, 255, 102, 0.1); border: 1.5px solid var(--neon-green); padding: 0.8rem 1.5rem; border-radius: 8px; text-align: center;">
                            <span style="font-size: 0.85rem; color: var(--neon-green); font-weight: bold; display: block;">🛡️ สิทธิ์สมาชิกเปิดใช้งานอยู่</span>
                            <span style="font-size: 0.75rem; color: var(--text-gray);">ราคาสินค้าปรับเป็นราคาสมาชิกให้อัตโนมัติ</span>
                        </div>
                    <?php else: ?>
                        <a href="register.php" class="btn-cyber btn-green" style="padding: 0.8rem 1.6rem; font-size: 1rem; text-decoration: none; box-shadow: 0 0 15px rgba(0,255,102,0.3);">
                            ✨ สมัครสมาชิกรับส่วนลด 10% ทันที
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- การ์ดโฆษณาโปรโมชั่นซื้อครบ 2000 บาท ลด 67% -->
        <div style="background: linear-gradient(135deg, #0a0500 0%, #1a0a00 40%, #0d1117 100%); border: 1.5px solid #ff8c00; border-radius: 14px; padding: 0; margin-bottom: 2rem; position: relative; overflow: hidden; animation: pulse-glow 3s ease-in-out infinite;">
            <!-- Background decorative stripes -->
            <div style="position: absolute; inset: 0; background: repeating-linear-gradient(45deg, transparent, transparent 30px, rgba(255,140,0,0.015) 30px, rgba(255,140,0,0.015) 60px); pointer-events: none;"></div>
            <!-- Top ribbon -->
            <div style="background: linear-gradient(90deg, #ff6b00, #ffd700, #ff6b00); padding: 8px 20px; text-align: center; font-family: 'Orbitron', sans-serif; font-size: 0.75rem; font-weight: 900; letter-spacing: 3px; color: #000; text-shadow: none;">
                ⚡ LIMITED TIME OFFER — ซื้อครบ ฿2,000 รับส่วนลดสูงสุด 67% ⚡
            </div>
            <div style="padding: 2rem; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 2rem; position: relative;">
                <!-- Left: Description -->
                <div style="flex: 1; min-width: 280px;">
                    <div style="display: inline-flex; align-items: center; gap: 8px; background: rgba(255, 140, 0, 0.12); border: 1px solid #ff8c00; padding: 4px 14px; border-radius: 20px; font-size: 0.8rem; font-weight: bold; margin-bottom: 1rem; letter-spacing: 1px; color: #ffa500;">
                        <span style="width: 8px; height: 8px; background: #ffd700; border-radius: 50%; display: inline-block; box-shadow: 0 0 8px #ffd700; animation: badge-pulse 1.5s ease-in-out infinite;"></span>
                        🎯 MEGA DEAL ACTIVE
                    </div>
                    <h2 style="font-family: 'Orbitron', sans-serif; font-size: 1.7rem; color: #fff; margin-bottom: 0.6rem; line-height: 1.2;">
                        ซื้อครบ <span style="color: #ff8c00;">฿2,000</span><br>
                        <span style="color: #ffd700; text-shadow: 0 0 15px rgba(255,215,0,0.6);">รับส่วนลดสูงสุด</span>
                        <span style="font-size: 2.8rem; color: #ffd700; text-shadow: 0 0 20px rgba(255,215,0,0.8);"> 67%</span>
                    </h2>
                    <p style="color: var(--text-gray); font-size: 0.9rem; margin-bottom: 1rem; line-height: 1.6;">
                        🛒 เพิ่มสินค้าในตะกร้าให้ครบ <strong style="color: #ff8c00;">2,000 บาท</strong> และรับส่วนลดอัตโนมัติสูงสุด <strong style="color: #ffd700;">67%</strong> สำหรับสินค้าคัดพิเศษ!<br>
                        <span style="font-size: 0.82rem; color: #888;">* ส่วนลด 67% สำหรับสินค้าที่ร่วมรายการ เงื่อนไขเป็นไปตามที่บริษัทกำหนด</span>
                    </p>
                    <div style="display: flex; gap: 0.8rem; flex-wrap: wrap;">
                        <div style="display: flex; align-items: center; gap: 6px; font-size: 0.82rem; color: #aaa;">
                            <span style="color: #ffd700;">✓</span> สินค้าทุกหมวดหมู่ร่วมรายการ
                        </div>
                        <div style="display: flex; align-items: center; gap: 6px; font-size: 0.82rem; color: #aaa;">
                            <span style="color: #ffd700;">✓</span> ไม่จำกัดจำนวนการสั่งซื้อ
                        </div>
                        <div style="display: flex; align-items: center; gap: 6px; font-size: 0.82rem; color: #aaa;">
                            <span style="color: #ffd700;">✓</span> ใช้ร่วมกับโค้ด CYBER10 ได้
                        </div>
                    </div>
                </div>
                <!-- Right: Deal Badge -->
                <div style="display: flex; flex-direction: column; align-items: center; gap: 1rem;">
                    <div style="position: relative; width: 140px; height: 140px; display: flex; align-items: center; justify-content: center;">
                        <div style="position: absolute; inset: 0; border-radius: 50%; border: 3px solid #ffd700; box-shadow: 0 0 25px rgba(255,215,0,0.4), inset 0 0 25px rgba(255,140,0,0.1); background: radial-gradient(circle, #1a0e00, #0a0500);"></div>
                        <div style="position: relative; text-align: center;">
                            <div style="font-size: 0.65rem; color: #ffa500; font-family: 'Orbitron', sans-serif; letter-spacing: 2px; text-transform: uppercase;">สูงสุด</div>
                            <div style="font-family: 'Orbitron', sans-serif; font-size: 2.8rem; font-weight: 900; color: #ffd700; line-height: 1; text-shadow: 0 0 20px rgba(255,215,0,0.8);">67<span style="font-size: 1.4rem;">%</span></div>
                            <div style="font-size: 0.65rem; color: #ffa500; font-family: 'Orbitron', sans-serif; letter-spacing: 2px;">OFF</div>
                        </div>
                    </div>
                    <a href="#product-grid" style="background: linear-gradient(135deg, #ff8c00, #ffd700); color: #000; font-family: 'Orbitron', sans-serif; font-size: 0.8rem; font-weight: 900; padding: 0.6rem 1.4rem; border-radius: 8px; text-decoration: none; letter-spacing: 1px; box-shadow: 0 4px 20px rgba(255,180,0,0.4); transition: all 0.2s ease;">
                        🛍️ ช้อปเลย!
                    </a>
                </div>
            </div>
        </div>

        <div style="text-align: center; margin-bottom: 2rem;">
            <h1 style="font-size: 2.8rem; text-shadow: 0 0 15px var(--glow-red); margin-bottom: 0.5rem;">CYBERNETIC_MALL</h1>
            <p style="color: var(--text-gray); margin-bottom: 1.5rem;">แหล่งจำหน่ายเสื้อผ้าแนวสตรีท กระเป๋าเอนกประสงค์ และของใช้ไอทีสไตล์ไซเบอร์พังก์</p>
        </div>

        <!-- แถบเครื่องมือเลือกลดกรอง และเรียงลำดับสินค้าสำหรับลูกค้า -->
        <div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem; margin-bottom: 2rem; background: rgba(18, 20, 26, 0.8); padding: 1rem 1.5rem; border-radius: 10px; border: 1px solid var(--border-color);">
            <!-- เลือกหมวดหมู่ -->
            <div style="display: flex; gap: 0.6rem; flex-wrap: wrap; align-items: center;">
                <span style="font-size: 0.85rem; color: var(--text-gray); font-family: 'Orbitron', sans-serif; margin-right: 4px;">เลือกหมวดหมู่:</span>
                <a href="index.php?sort=<?php echo urlencode($sort); ?>&search=<?php echo urlencode($search); ?>" class="cat-btn <?php echo empty($category_filter) ? 'active' : ''; ?>">ทั้งหมด (<?php echo count($products); ?>)</a>
                <a href="index.php?category=clothing&sort=<?php echo urlencode($sort); ?>&search=<?php echo urlencode($search); ?>" class="cat-btn <?php echo $category_filter === 'clothing' ? 'active' : ''; ?>">👕 เสื้อผ้า</a>
                <a href="index.php?category=bags&sort=<?php echo urlencode($sort); ?>&search=<?php echo urlencode($search); ?>" class="cat-btn <?php echo $category_filter === 'bags' ? 'active' : ''; ?>">🎒 กระเป๋า</a>
                <a href="index.php?category=accessories&sort=<?php echo urlencode($sort); ?>&search=<?php echo urlencode($search); ?>" class="cat-btn <?php echo $category_filter === 'accessories' ? 'active' : ''; ?>">🎧 ของใช้ไอที</a>
            </div>

            <!-- เรียงลำดับสินค้า -->
            <form action="index.php" method="GET" style="display: flex; align-items: center; gap: 0.5rem;">
                <?php if (!empty($category_filter)): ?>
                    <input type="hidden" name="category" value="<?php echo htmlspecialchars($category_filter); ?>">
                <?php endif; ?>
                <?php if (!empty($search)): ?>
                    <input type="hidden" name="search" value="<?php echo htmlspecialchars($search); ?>">
                <?php endif; ?>
                <label style="font-size: 0.85rem; color: var(--text-gray); font-family: 'Orbitron', sans-serif; white-space: nowrap;">เรียงตาม:</label>
                <select name="sort" onchange="this.form.submit()" class="cyber-input" style="padding: 0.4rem 0.8rem; font-size: 0.85rem; border-color: var(--neon-cyan); color: var(--text-white); background: #0d1117;">
                    <option value="newest" <?php echo $sort === 'newest' ? 'selected' : ''; ?>>🆕 สินค้าใหม่ล่าสุด</option>
                    <option value="price_low" <?php echo $sort === 'price_low' ? 'selected' : ''; ?>>💲 ราคา: น้อย -> มาก</option>
                    <option value="price_high" <?php echo $sort === 'price_high' ? 'selected' : ''; ?>>💎 ราคา: มาก -> น้อย</option>
                    <option value="in_stock" <?php echo $sort === 'in_stock' ? 'selected' : ''; ?>>📦 มีสินค้าพร้อมส่งก่อน</option>
                </select>
            </form>
        </div>

        <?php if (isset($cart_success)): ?>
            <div class="cyber-alert cyber-alert-success">
                <strong>[สำเร็จ]</strong> <?php echo $cart_success; ?> <a href="cart.php" style="color:#55ff99; font-weight: bold; text-decoration: underline;">เปิดตะกร้าสินค้าเพื่อสั่งซื้อ &raquo;</a>
            </div>
        <?php endif; ?>

        <?php if (isset($cart_error)): ?>
            <div class="cyber-alert cyber-alert-error">
                <strong>[ไม่สำเร็จ]</strong> <?php echo $cart_error; ?>
            </div>
        <?php endif; ?>

        <!-- แสดงตารางรายการสินค้าหลากหลายหมวดหมู่ -->
        <div class="product-grid">
            <?php if (empty($products)): ?>
                <div style="grid-column: 1/-1; text-align: center; padding: 4rem 0; color: var(--text-muted);">
                    <h3>ไม่พบข้อมูลสินค้าตรงกับที่คุณค้นหา</h3>
                </div>
            <?php else: ?>
                <?php foreach ($products as $prod): ?>
                    <div class="product-card">
                        <div>
                            <a href="product.php?id=<?php echo $prod['id']; ?>">
                                <?php if ($prod['image_url']): ?>
                                    <img class="product-image" src="<?php echo htmlspecialchars($prod['image_url']); ?>" alt="<?php echo htmlspecialchars($prod['name']); ?>">
                                <?php else: ?>
                                    <div style="width: 100%; height: 240px; background: #161616; display: flex; align-items: center; justify-content: center; color: #444; border-bottom: 1px solid var(--border-color);">ไม่มีรูปภาพสินค้า</div>
                                <?php endif; ?>
                            </a>
                            
                            <div class="product-content">
                                <div style="margin-bottom: 0.5rem;">
                                    <span class="badge" style="background: rgba(255, 0, 85, 0.05); color: var(--neon-red); border: 1px solid rgba(255, 0, 85, 0.2); font-size: 0.7rem; padding: 1px 8px;">
                                        <?php 
                                            $prod_cat = $prod['category'] ?? 'general';
                                            if ($prod_cat === 'clothing') echo "เสื้อผ้า";
                                            elseif ($prod_cat === 'bags') echo "กระเป๋า";
                                            else echo "ของใช้ไอที";
                                        ?>
                                    </span>
                                </div>
                                <a href="product.php?id=<?php echo $prod['id']; ?>" style="text-decoration: none;">
                                    <h3 class="product-title"><?php echo htmlspecialchars($prod['name']); ?></h3>
                                </a>
                                <p style="font-size: 0.85rem; color: var(--text-gray); margin-bottom: 1rem; line-height: 1.4; height: 4.2em; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;">
                                    <?php echo htmlspecialchars($prod['description']); ?>
                                </p>
                            </div>
                        </div>
                        
                        <div style="padding: 0 1.5rem 1.5rem 1.5rem;">
                            <div style="display: flex; justify-content: space-between; align-items: center;">
                                <div class="product-price">
                                    <?php 
                                        $member_disc = get_member_discount_percent();
                                        $coupon_disc = !empty($_SESSION['coupon']['discount_percent']) ? $_SESSION['coupon']['discount_percent'] : 0;
                                        $effective_disc = max($member_disc, $coupon_disc);
                                        $disc_label = ($member_disc >= $coupon_disc && $member_disc > 0) ? '🛡️ สมาชิก (-' . $member_disc . '%)' : ('- ' . $coupon_disc . '%');

                                        if ($effective_disc > 0): 
                                            $disc_price = $prod['price'] * (1 - $effective_disc / 100);
                                    ?>
                                        <div style="display: flex; align-items: center; gap: 6px; flex-wrap: wrap;">
                                            <span style="color: var(--neon-green); font-weight: bold; text-shadow: 0 0 8px var(--glow-green);">฿<?php echo number_format($disc_price, 2); ?></span>
                                            <span style="font-size: 0.7rem; background: var(--neon-green); color: #000; padding: 1px 6px; border-radius: 4px; font-weight: bold; font-family: 'Orbitron', sans-serif;"><?php echo $disc_label; ?></span>
                                        </div>
                                        <div style="font-size: 0.8rem; color: var(--text-gray); text-decoration: line-through;">฿<?php echo number_format($prod['price'], 2); ?></div>
                                    <?php else: ?>
                                        ฿<?php echo number_format($prod['price'], 2); ?>
                                        <div style="font-size: 0.72rem; margin-top: 2px;">
                                            <a href="register.php" style="color: var(--neon-cyan); text-decoration: none; font-weight: bold;">✨ สมัครสมาชิกรับลด 10%</a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <div style="font-size: 0.85rem;">
                                    <?php if ($prod['stock'] > 0): ?>
                                        <span style="color: var(--neon-green);">มีสินค้า (<?php echo $prod['stock']; ?>)</span>
                                    <?php else: ?>
                                        <span style="color: var(--neon-red); font-weight: bold;">สินค้าหมดชั่วคราว</span>
                                    <?php endif; ?>
                                </div>
                            </div>
                            
                            <form action="" method="POST" style="margin-top: 1rem;">
                                <input type="hidden" name="product_id" value="<?php echo $prod['id']; ?>">
                                
                                <!-- เลือกไซส์และสีเฉพาะหมวดเสื้อผ้าเท่านั้น -->
                                <?php if (($prod['category'] ?? 'general') === 'clothing'): ?>
                                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; margin-bottom: 1rem;">
                                        <div>
                                            <label class="cyber-label" style="font-size: 0.8rem; color: var(--text-gray);">ไซส์ (SIZE)</label>
                                            <select class="cyber-input" name="size" style="padding: 0.4rem 0.5rem; font-size: 0.95rem; background: var(--bg-input); border-color: var(--border-color);">
                                                <option value="S">S</option>
                                                <option value="M" selected>M</option>
                                                <option value="L">L</option>
                                                <option value="XL">XL</option>
                                                <option value="XXL">XXL</option>
                                            </select>
                                        </div>
                                        <div>
                                            <label class="cyber-label" style="font-size: 0.8rem; color: var(--text-gray);">สี (COLOR)</label>
                                            <select class="cyber-input" name="color" style="padding: 0.4rem 0.5rem; font-size: 0.95rem; background: var(--bg-input); border-color: var(--border-color);">
                                                <option value="ดำ" selected>ดำ (Black)</option>
                                                <option value="แดง">แดง (Red)</option>
                                                <option value="เทา">เทา (Grey)</option>
                                            </select>
                                        </div>
                                    </div>
                                <?php else: ?>
                                    <input type="hidden" name="size" value="N/A">
                                    <input type="hidden" name="color" value="N/A">
                                <?php endif; ?>
                                
                                <?php if ($prod['stock'] > 0): ?>
                                    <!-- ตัวเลือกปรับจำนวนชิ้นสินค้าในหน้าคาร์ดสินค้าหลัก -->
                                    <div style="display: flex; gap: 0.5rem; margin-bottom: 1rem;">
                                        <div style="width: 40%; display: flex; flex-direction: column;">
                                            <label class="cyber-label" style="font-size: 0.8rem; color: var(--text-gray);">จำนวนชิ้น</label>
                                            <input class="cyber-input" type="number" name="quantity" value="1" min="1" max="<?php echo $prod['stock']; ?>" style="padding: 0.35rem; text-align: center;">
                                        </div>
                                        <div style="width: 60%; display: flex; align-items: flex-end;">
                                            <button type="submit" name="add_to_cart" class="btn-cyber" style="width: 100%; padding: 0.55rem 0;">
                                                ใส่ตะกร้า
                                            </button>
                                        </div>
                                    </div>
                                <?php else: ?>
                                    <button type="button" class="btn-cyber btn-gray" style="width: 100%; cursor: not-allowed;" disabled>
                                        ไม่พร้อมจำหน่าย
                                    </button>
                                <?php endif; ?>
                            </form>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

    </div>

    <footer style="margin-top: 8rem; padding: 2rem; border-top: 1px solid var(--border-color); text-align: center; font-size: 0.85rem; font-family: 'Orbitron', sans-serif;">
        <span style="color: var(--text-muted);">&copy; 2026 CYBER_SHOP SYSTEMS. ALL RIGHTS SECURED BY NEURAL_LINK.</span>
    <!-- ปุ่มตะกร้าลอยตัวสะดวกรวดเร็วสำหรับลูกค้า -->
    <?php if ($cart_count > 0): ?>
        <a href="cart.php" style="position: fixed; bottom: 25px; right: 25px; z-index: 999; background: linear-gradient(135deg, #111, #1a080d); border: 2px solid var(--neon-red); color: #fff; padding: 12px 20px; border-radius: 50px; text-decoration: none; display: flex; align-items: center; gap: 12px; box-shadow: 0 0 25px rgba(255, 0, 85, 0.5), 0 5px 15px rgba(0,0,0,0.8); backdrop-filter: blur(10px); transition: transform 0.2s ease;">
            <span style="font-size: 1.4rem;">🛒</span>
            <div>
                <div style="font-size: 0.75rem; color: var(--text-gray); text-transform: uppercase; letter-spacing: 1px;">ตะกร้าของคุณ</div>
                <div style="font-family: 'Orbitron', sans-serif; font-weight: bold; font-size: 0.95rem; color: var(--neon-red);">
                    <?php echo $cart_count; ?> ชิ้น
                </div>
            </div>
            <span style="background: var(--neon-green); color: #000; padding: 5px 12px; border-radius: 20px; font-weight: bold; font-size: 0.8rem; margin-left: 4px; box-shadow: 0 0 8px var(--glow-green);">เปิดตะกร้า &raquo;</span>
        </a>
    <?php endif; ?>

</body>
</html>
