<?php
// migrate-images.php
// ==============================================
// Script untuk update path gambar di database
// ==============================================

// KONFIGURASI DATABASE (GANTI DENGAN MILIKMU!)
$db_host = 'localhost';
$db_database=sutanrajahotel_tesmenu
$db_username=sutanrajahotel_azzam
$db_password=pasawahan12

// Connect ke database
try {
    $conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    echo "<h2>Migrasi Path Gambar</h2>";
    echo "<p>Script ini akan mengubah path gambar di database:</p>";
    echo "<p><strong>Dari:</strong> menu/categories/</p>";
    echo "<p><strong>Menjadi:</strong> uploads/menu/categories/</p>";
    echo "<hr>";
    
    // 1. Cek data sebelum update
    echo "<h3>Data Sebelum Update:</h3>";
    $sql_check = "SELECT id, name, image FROM menu_categories WHERE image IS NOT NULL";
    $result = $conn->query($sql_check);
    
    if ($result->num_rows > 0) {
        echo "<table border='1' cellpadding='8'>";
        echo "<tr><th>ID</th><th>Nama Kategori</th><th>Path Lama</th></tr>";
        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>{$row['id']}</td>";
            echo "<td>{$row['name']}</td>";
            echo "<td>{$row['image']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p>Tidak ada data gambar di database.</p>";
    }
    
    echo "<hr>";
    
    // 2. Update path
    echo "<h3>Proses Update...</h3>";
    $sql_update = "UPDATE menu_categories 
                   SET image = REPLACE(image, 'menu/categories/', 'uploads/menu/categories/')
                   WHERE image LIKE 'menu/categories/%'";
    
    if ($conn->query($sql_update)) {
        $affected_rows = $conn->affected_rows;
        echo "<p style='color: green;'>? BERHASIL! {$affected_rows} record di-update.</p>";
    } else {
        echo "<p style='color: red;'>? GAGAL: " . $conn->error . "</p>";
    }
    
    echo "<hr>";
    
    // 3. Cek data setelah update
    echo "<h3>Data Setelah Update:</h3>";
    $result2 = $conn->query($sql_check);
    
    if ($result2->num_rows > 0) {
        echo "<table border='1' cellpadding='8'>";
        echo "<tr><th>ID</th><th>Nama Kategori</th><th>Path Baru</th></tr>";
        while($row = $result2->fetch_assoc()) {
            echo "<tr>";
            echo "<td>{$row['id']}</td>";
            echo "<td>{$row['name']}</td>";
            echo "<td>{$row['image']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    echo "<hr>";
    echo "<p><strong>Catatan:</strong> Pastikan file gambar sudah dipindahkan ke folder <code>public/uploads/menu/categories/</code></p>";
    
    $conn->close();
    
} catch(Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}
?>