<?php

// Database configuration
define('DB_HOST', 'localhost');
define('DB_USER', 'wiselie_nanda');
define('DB_PASS', 'Clemira123');
define('DB_NAME', 'wiselie_inventory');

// Attempt to connect to the database
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// Check connection
if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: " . $mysqli->connect_error);
}

$message = "";
$message_type = "";

// Function to handle shuffling for a given table
function shuffleTableData($tableName, $mysqli) {
    global $message, $message_type; // Use global to update messages

    // Update query now targets the 'quantity' column
    $update_query = "UPDATE " . $tableName . " SET quantity = id";
    if ($mysqli->query($update_query)) {
        $message = "Data kuantitas pada tabel '" . $tableName . "' berhasil diacak (quantity = id)!";
        $message_type = "success";
    } else {
        $message = "Error mengacak data pada tabel '" . $tableName . "': " . $mysqli->error;
        $message_type = "error";
    }
}

// --- Proses Acak Data untuk bom_level ---
if (isset($_POST['shuffle_bom_level'])) {
    shuffleTableData('bom_level', $mysqli);
}

// --- Proses Acak Data untuk wh_jakarta ---
if (isset($_POST['shuffle_wh_jakarta'])) {
    shuffleTableData('wh_jakarta', $mysqli);
}

// --- Proses Acak Data untuk products ---
if (isset($_POST['shuffle_products'])) {
    shuffleTableData('products', $mysqli);
}

// --- Proses Acak Data untuk wh_makassar ---
if (isset($_POST['shuffle_wh_makassar'])) {
    shuffleTableData('wh_makassar', $mysqli);
}

// --- Proses Acak Data untuk wh_bandung ---
if (isset($_POST['shuffle_wh_bandung'])) {
    shuffleTableData('wh_bandung', $mysqli);
}

// --- Proses Acak Data untuk wh_bali ---
if (isset($_POST['shuffle_bali'])) {
    shuffleTableData('wh_bali', $mysqli);
}

// --- Proses Acak Data untuk wh_semarang ---
if (isset($_POST['shuffle_semarang'])) {
    shuffleTableData('wh_semarang', $mysqli);
}


$mysqli->close(); // Tutup koneksi setelah semua proses PHP selesai

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Acak Data Kuantitas</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f4f4f4;
            color: #333;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 80vh;
        }
        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            text-align: center;
            width: 90%;
            max-width: 500px;
        }
        h1 {
            color: #0056b3;
            margin-bottom: 30px;
        }
        .message {
            padding: 10px;
            margin-bottom: 20px;
            border-radius: 5px;
            text-align: center;
            font-weight: bold;
            width: 100%;
            box-sizing: border-box;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .button-group {
            display: flex;
            flex-direction: column;
            gap: 15px;
            margin-top: 20px;
        }
        .shuffle-button {
            background-color: #28a745;
            color: white;
            padding: 12px 25px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 18px;
            width: 100%;
            box-sizing: border-box;
            transition: background-color 0.3s ease;
        }
        .shuffle-button:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>

    <div class="container">
        <h1>Acak Data Kuantitas per Tabel</h1>

        <?php if (!empty($message)): ?>
            <div class="message <?php echo $message_type; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>

        <div class="button-group">
            <form action="" method="post">
                <button type="submit" name="shuffle_bom_level" class="shuffle-button">
                    Acak Data Kuantitas: bom_level (quantity = id)
                </button>
            </form>

            <form action="" method="post">
                <button type="submit" name="shuffle_wh_jakarta" class="shuffle-button">
                    Acak Data Kuantitas: wh_jakarta (quantity = id)
                </button>
            </form>

            <form action="" method="post">
                <button type="submit" name="shuffle_products" class="shuffle-button">
                    Acak Data Kuantitas: products (quantity = id)
                </button>
            </form>

            <form action="" method="post">
                <button type="submit" name="shuffle_wh_makassar" class="shuffle-button">
                    Acak Data Kuantitas: wh_makassar (quantity = id)
                </button>
            </form>

            <form action="" method="post">
                <button type="submit" name="shuffle_wh_bandung" class="shuffle-button">
                    Acak Data Kuantitas: wh_bandung (quantity = id)
                </button>
            </form>

            <form action="" method="post">
                <button type="submit" name="shuffle_bali" class="shuffle-button">
                    Acak Data Kuantitas: wh_bali (quantity = id)
                </button>
            </form>

            <form action="" method="post">
                <button type="submit" name="shuffle_semarang" class="shuffle-button">
                    Acak Data Kuantitas: wh_semarang (quantity = id)
                </button>
            </form>
        </div>
    </div>

</body>
</html>