<?php
declare(strict_types=1);

$dbHost = getenv('DB_HOST') ?: 'localhost';
$dbUser = getenv('DB_USER') ?: 'root';
$dbPass = getenv('DB_PASSWORD') ?: '';
$dbName = 'listbnb';

try {
    $pdo = new PDO("mysql:host={$dbHost};dbname={$dbName};charset=utf8mb4", $dbUser, $dbPass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);
} catch (Throwable $e) {
    http_response_code(500);
    echo "<!doctype html><html><head><meta charset='utf-8'></head><body><script>alert('Database connection failed.');window.location.href='admin-products.php';</script></body></html>";
    exit;
}

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) {
    http_response_code(400);
    echo "<!doctype html><html><head><meta charset='utf-8'></head><body><script>alert('Invalid product id.');window.location.href='admin-products.php';</script></body></html>";
    exit;
}

$stmt = $pdo->prepare('SELECT * FROM products WHERE id=:id');
$stmt->execute([':id'=>$id]);
$p = $stmt->fetch();
if (!$p) {
    http_response_code(404);
    echo "<!doctype html><html><head><meta charset='utf-8'></head><body><script>alert('Product not found.');window.location.href='admin-products.php';</script></body></html>";
    exit;
}
?>
<!doctype html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Product Details - ListBnb</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" type="image/x-icon" href="assets/img/logo/favicon.png">
    <link rel="stylesheet" href="assets/css/bootstrap.css">
    <link rel="stylesheet" href="assets/css/animate.css">
    <link rel="stylesheet" href="assets/css/swiper-bundle.css">
    <link rel="stylesheet" href="assets/css/slick.css">
    <link rel="stylesheet" href="assets/css/magnific-popup.css">
    <link rel="stylesheet" href="assets/css/font-awesome-pro.css">
    <link rel="stylesheet" href="assets/css/spacing.css">
    <link rel="stylesheet" href="assets/css/custom-animation.css">
    <link rel="stylesheet" href="assets/css/main.css">
    <style>
      .admin-layout{display:flex;gap:20px}
      .admin-sidebar{width:280px;background:#fff;border:1px solid #eee;border-radius:16px;padding:16px;box-shadow:0 6px 24px rgba(0,0,0,.06)}
      .admin-sidebar .menu-title{font-size:14px;color:#999;text-transform:uppercase;margin-bottom:10px}
      .admin-sidebar ul{list-style:none;padding:0;margin:0}
      .admin-sidebar li a{display:flex;align-items:center;gap:10px;padding:10px 12px;border-radius:12px;color:#222}
      .admin-sidebar li a:hover{background:#f5f7f9}
      .admin-main{flex:1}
      .section{background:#fff;border:1px solid #eee;border-radius:16px;padding:16px;box-shadow:0 6px 24px rgba(0,0,0,.06)}
      .gallery{display:grid;grid-template-columns:repeat(5,1fr);gap:10px}
      .gallery img{width:100%;height:120px;object-fit:cover;border-radius:8px;border:1px solid #eee}
      .detail-grid{display:grid;grid-template-columns:1fr 1fr;gap:12px}
      .detail-item{padding:10px;border:1px solid #eee;border-radius:12px}
    </style>
    </head>
<body>
   <header class="tp-header-height">
      <div id="header-sticky" class="tp-header-area tp-header-inner-style">
         <div class="container">
            <div class="row align-items-center">
               <div class="col-xl-3 col-lg-6 col-md-6 col-6">
                  <div class="tp-header-logo">
                     <a href="admin-dashboard.php" class="brand-text">ReWear</a>
                  </div>
               </div>
               <div class="col-xl-6 col-lg-4 d-none d-xl-block">
                  <div class="tp-header-menu">
                     <nav class="tp-main-menu-content">
                        <ul>
                           <li><a href="admin-dashboard.php">Dashboard</a></li>
                           <li><a href="listing-ads.html">Post Your Ad</a></li>
                           <li><a href="my-products.php">My Products</a></li>
                        </ul>
                     </nav>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </header>

   <main>
      <div class="tp-profile-area z-index pb-120 pt-60">
         <div class="container">
            <div class="admin-layout">
              <aside class="admin-sidebar">
                <div class="menu-title">Menu</div>
                <ul>
                  <li><a href="admin-dashboard.php"><i class="fa-regular fa-grid-2"></i> Dashboard</a></li>
                </ul>
                <div class="menu-title">Product & Order</div>
                <ul>
                  <li><a href="admin-products.php"><i class="fa-regular fa-box"></i> Manage Products</a></li>
                  <li><a href="admin-order-requests.php"><i class="fa-regular fa-cart-shopping"></i> Manage Order Requests</a></li>
                </ul>
                <div class="menu-title">Site Content</div>
                <ul>
                  <li><a href="admin-home-images.php"><i class="fa-regular fa-image"></i> Home Image</a></li>
                </ul>
                <div class="menu-title">Accounts & Roles</div>
                <ul>
                  <li><a href="admin-users.php"><i class="fa-regular fa-users"></i> Customers</a></li>
                  <li><a href="#"><i class="fa-regular fa-user-tie"></i> Sellers</a></li>
                  <li><a href="#"><i class="fa-regular fa-chart-pie-simple"></i> Reports</a></li>
                </ul>
              </aside>
              <section class="admin-main">
                <div class="section mb-20">
                  <h4 class="tp-form-title mb-5">Product Details</h4>
                  <p class="mb-0">#<?php echo (int)$p['id']; ?> • <?php echo htmlspecialchars($p['product_title'], ENT_QUOTES); ?></p>
                </div>
                <div class="section mb-20">
                  <div class="gallery">
                    <?php for ($i=1;$i<=5;$i++): $k='image'.$i; if(!empty($p[$k])): ?>
                      <img src="<?php echo htmlspecialchars($p[$k], ENT_QUOTES); ?>" alt="img"/>
                    <?php endif; endfor; ?>
                  </div>
                </div>
                <div class="section">
                  <div class="detail-grid">
                    <div class="detail-item"><strong>Title:</strong> <?php echo htmlspecialchars($p['product_title'], ENT_QUOTES); ?></div>
                    <div class="detail-item"><strong>Price:</strong> <?php echo number_format((float)$p['price'], 2); ?></div>
                    <div class="detail-item"><strong>Size:</strong> <?php echo htmlspecialchars($p['product_size'], ENT_QUOTES); ?></div>
                    <div class="detail-item"><strong>Condition:</strong> <?php echo htmlspecialchars($p['condition'], ENT_QUOTES); ?></div>
                    <div class="detail-item"><strong>Category:</strong> <?php echo htmlspecialchars($p['category'], ENT_QUOTES); ?></div>
                    <div class="detail-item"><strong>Product Type:</strong> <?php echo htmlspecialchars($p['product_type'], ENT_QUOTES); ?></div>
                    <div class="detail-item"><strong>Brand:</strong> <?php echo htmlspecialchars((string)$p['brand'], ENT_QUOTES); ?></div>
                    <div class="detail-item"><strong>Status:</strong> <?php echo htmlspecialchars((string)$p['status'], ENT_QUOTES); ?></div>
                    <div class="detail-item"><strong>Created:</strong> <?php echo htmlspecialchars($p['created_at'], ENT_QUOTES); ?></div>
                    <div class="detail-item" style="grid-column:1/-1"><strong>Description:</strong><br><?php echo nl2br(htmlspecialchars($p['description'], ENT_QUOTES)); ?></div>
                  </div>
                </div>
              </section>
            </div>
         </div>
      </div>
   </main>

   <footer>
      <div class="tp-copyright-area black-bg">
         <div class="container">
            <div class="tp-copyright-ptb">
               <div class="row align-items-center">
                  <div class="col-xl-7 col-lg-6 col-md-6 col-sm-7">
                     <div class="tp-copyright-left d-flex align-items-center justify-content-center justify-content-md-start">
                        <div class="tp-copyright-text text-center text-md-start">
                           <p>Listbnb Admin</p>
                        </div>
                     </div>
                  </div>
                  <div class="col-xl-5 col-lg-6 col-md-6 col-sm-5">
                     <div class="tp-copyright-right justify-content-center d-flex justify-content-sm-end align-items-center">
                        <div class="tp-copyright-social">
                           <a href="#"><i class="fa-brands fa-facebook-f"></i></a>
                           <a href="#"><i class="fa-brands fa-twitter"></i></a>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </footer>

   <script src="assets/js/vendor/jquery.js"></script>
   <script src="assets/js/bootstrap-bundle.js"></script>
   <script src="assets/js/slick.js"></script>
   <script src="assets/js/main.js"></script>
</body>
</html>
