<?php
include 'includes/header.php';

$id = $_GET['id'] ?? 0;
$stmt = db()->prepare("SELECT p.*, c.name as category_name FROM products p JOIN categories c ON p.category_id = c.id WHERE p.id = ?");
$stmt->execute([$id]);
$product = $stmt->fetch();

if (!$product) {
    echo "<div class='alert alert-danger'>商品不存在。</div>";
    include 'includes/footer.php';
    exit;
}
?>

<div class="row g-5">
    <div class="col-lg-5">
        <div class="glass-card p-3 bg-white shadow-sm">
            <img src="<?php echo $product['image_url']; ?>" class="img-fluid rounded-4 w-100" alt="<?php echo $product['name']; ?>">
        </div>
    </div>
    <div class="col-lg-7">
        <div class="glass-card p-5 h-100 bg-white shadow-sm">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.php" class="text-decoration-none text-muted">首页</a></li>
                    <li class="breadcrumb-item"><a href="category.php?id=<?php echo $product['category_id']; ?>" class="text-decoration-none text-muted"><?php echo $product['category_name']; ?></a></li>
                    <li class="breadcrumb-item active text-dark" aria-current="page"><?php echo $product['name']; ?></li>
                </ol>
            </nav>
            <h1 class="display-4 mb-4 text-dark fw-bold"><?php echo $product['name']; ?></h1>
            <div class="mb-4">
                <span class="badge bg-primary bg-opacity-10 text-primary px-3 py-2 fs-6 mb-2"><?php echo $product['category_name']; ?></span>
                <p class="fs-4 text-muted mt-3"><?php echo $product['description']; ?></p>
            </div>
            
            <div class="d-flex align-items-center mb-5">
                <div class="me-5">
                    <span class="text-muted d-block mb-1 small">建议零售价</span>
                    <span class="display-5 fw-bold" style="color: var(--primary-color);"><?php echo $product['price_usdt']; ?> <small class="fs-4">USDT</small></span>
                </div>
                <div>
                    <span class="text-muted d-block mb-1 small">库存状态</span>
                    <span class="badge <?php echo $product['stock'] > 0 ? 'bg-success bg-opacity-10 text-success' : 'bg-danger bg-opacity-10 text-danger'; ?> fs-6 py-2 px-4 rounded-pill border border-<?php echo $product['stock'] > 0 ? 'success' : 'danger'; ?> border-opacity-25">
                        <?php echo $product['stock'] > 0 ? '现货供应 ('.$product['stock'].')' : '缺货中'; ?>
                    </span>
                </div>
            </div>

            <div class="d-grid gap-3 d-md-flex">
                <button class="btn btn-primary btn-lg px-5 flex-grow-1" onclick="addToCart(<?php echo $product['id']; ?>, '<?php echo addslashes($product['name']); ?>', <?php echo $product['price_usdt']; ?>, '<?php echo $product['image_url']; ?>')">
                    <i class="bi bi-cart-plus me-2"></i> 加入购物车
                </button>
                <a href="checkout.php?direct_id=<?php echo $product['id']; ?>" class="btn btn-outline-primary btn-lg px-5 flex-grow-1">
                    <i class="bi bi-lightning-fill me-2"></i> 立即购买
                </a>
            </div>
            
            <div class="mt-4 d-grid">
                <a href="<?php echo $tg_link; ?>" target="_blank" class="btn btn-outline-info btn-lg">
                    <i class="bi bi-chat-dots me-2"></i> 联系客服购买
                </a>
            </div>

            <div class="mt-5 pt-5 border-top border-light">
                <h5 class="text-dark mb-4">服务承诺</h5>
                <div class="row text-center g-4">
                    <div class="col-4">
                        <i class="bi bi-shield-lock fs-2 mb-2 d-block" style="color: var(--primary-color);"></i>
                        <span class="small text-muted">安全支付</span>
                    </div>
                    <div class="col-4">
                        <i class="bi bi-lightning-charge fs-2 mb-2 d-block" style="color: var(--primary-color);"></i>
                        <span class="small text-muted">自动发货</span>
                    </div>
                    <div class="col-4">
                        <i class="bi bi-headset fs-2 mb-2 d-block" style="color: var(--primary-color);"></i>
                        <span class="small text-muted">售后保障</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="mt-5">
    <div class="glass-card p-5 bg-white shadow-sm">
        <h3 class="text-dark mb-4">详细描述</h3>
        <div class="text-muted lh-lg">
            <?php echo nl2br($product['content'] ?? '暂无详细描述。'); ?>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>