<?php
/**
 * Fix product page link in database
 */
require_once __DIR__ . '/db_config.php';

// Check if Products page exists and update it
$check = $conn->query("SELECT id FROM pages WHERE slug = 'products' OR slug = 'shop'");
$result = $check->fetch_assoc();

if ($result) {
    // Update or insert the correct shop page
    $update = $conn->prepare("UPDATE pages SET url = ?, menu_order = ? WHERE slug = 'products' OR slug = 'shop'");
    $shop_url = 'shop.php';
    $order = 2;
    $update->bind_param('si', $shop_url, $order);
    $update->execute();
    echo "✓ Updated shop/product page URL to shop.php<br>";
} else {
    // Insert shop page if it doesn't exist
    $insert = $conn->prepare("INSERT INTO pages (title, slug, url, is_featured, menu_order) VALUES (?, ?, ?, ?, ?)");
    $title = 'Shop';
    $slug = 'shop';
    $url = 'shop.php';
    $featured = 1;
    $order = 2;
    $insert->bind_param('sssii', $title, $slug, $url, $featured, $order);
    $insert->execute();
    echo "✓ Created shop page<br>";
}

// Show all pages
echo "<br><strong>All pages in database:</strong><br>";
$result = $conn->query("SELECT * FROM pages ORDER BY menu_order ASC");
echo "<table border='1' cellpadding='10' style='margin-top: 20px;'>";
echo "<tr><th>ID</th><th>Title</th><th>Slug</th><th>URL</th><th>Featured</th><th>Order</th></tr>";

while ($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['slug'] . "</td>";
    echo "<td>" . $row['url'] . "</td>";
    echo "<td>" . ($row['is_featured'] ? "YES" : "NO") . "</td>";
    echo "<td>" . $row['menu_order'] . "</td>";
    echo "</tr>";
}
echo "</table>";

$conn->close();
?>
