<?php    
// Enable error reporting for debugging    
ini_set('display_errors', 1);    
ini_set('display_startup_errors', 1);    
error_reporting(E_ALL);    
    
// Database connection constants    
define('DB_HOST', 'localhost');    
define('DB_USER', 'wiselie_nanda');    
define('DB_PASS', 'Clemira123');    
define('DB_NAME', 'wiselie_inventory');    
    
// Establish database connection    
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);    
    
// Check connection    
if ($db->connect_error) {    
    die("Connection failed: " . $db->connect_error);    
}    
    
// Initialize variables    
$productName = '';    
$partNumber = '';    
$barcode = '';    
$scanSuccess = false; // Variable to track scan success    
    
// Check if the form is submitted    
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['barcode'])) {    
    $barcode = $db->real_escape_string(trim($_POST['barcode']));    
    
    // Fetch product details based on the scanned barcode    
    $query = "SELECT part_number, name FROM generate_barcode WHERE barcode = ?";    
    $stmt = $db->prepare($query);    
    $stmt->bind_param("s", $barcode);    
    $stmt->execute();    
    $result = $stmt->get_result();    
        
    if ($data = $result->fetch_assoc()) {    
        $partNumber = $data['part_number'];    
        $productName = $data['name'];    
        $scanSuccess = true; // Set scan success to true    
    
        // Insert scanned data into the completion table    
        $insertQuery = "INSERT INTO completion (part_number, name, barcode) VALUES (?, ?, ?)";    
        $insertStmt = $db->prepare($insertQuery);    
        $insertStmt->bind_param("sss", $partNumber, $productName, $barcode);    
        $insertStmt->execute();    
    } else {    
        $productName = 'Product not found.';    
        $partNumber = '';    
    }    
}    
?>    
    
<!DOCTYPE html>    
<html lang="en">    
<head>    
    <meta charset="UTF-8">    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">    
    <title>Barcode Scanner Completion</title>    
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">    
    <style>    
        body {    
            font-family: 'Roboto', sans-serif;    
            background-color: #f4f4f4;    
            color: #333;    
            text-align: center;    
            margin: 0;    
            padding: 20px;    
        }    
        h1 {    
            color: #4CAF50;    
            margin-bottom: 20px;    
        }    
        h2 {    
            color: #333;    
            margin-top: 20px;    
            font-weight: 700;    
        }    
        form {    
            background: #fff;    
            padding: 20px;    
            border-radius: 8px;    
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);    
            margin-bottom: 20px;    
        }    
        input[type="text"] {    
            width: 300px;    
            padding: 10px;    
            font-size: 16px;    
            border: 1px solid #ccc;    
            border-radius: 4px;    
            margin-right: 10px;    
        }    
        input[type="submit"], .back-button {    
            padding: 10px 20px;    
            font-size: 16px;    
            background-color: #4CAF50;    
            color: white;    
            border: none;    
            border-radius: 4px;    
            cursor: pointer;    
            transition: background-color 0.3s;    
            margin-top: 10px;    
        }    
        input[type="submit"]:hover, .back-button:hover {    
            background-color: #45a049;    
        }    
        .result {    
            margin-top: 20px;    
            padding: 15px;    
            background: #fff;    
            border-radius: 8px;    
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);    
            font-size: 18px;    
            font-weight: bold;    
        }    
        .success-message {    
            color: #4CAF50;    
            font-size: 20px;    
            font-weight: bold;    
            margin-top: 10px;    
        }    
        .output-title {    
            font-size: 24px;    
            color: #333;    
            margin-top: 30px;    
            font-weight: 700;    
        }    
        .result p {    
            margin: 5px 0;    
        }    
        @media (max-width: 600px) {    
            input[type="text"] {    
                width: 80%;    
                margin-bottom: 10px;    
            }    
            input[type="submit"], .back-button {    
                width: 80%;    
            }    
        }    
    </style>    
</head>    
<body>    
    <h1>Scan Barcode</h1>    
    <form method="POST" action="">    
        <input type="text" name="barcode" placeholder="Scan barcode here" autofocus required>    
        <input type="submit" value="Submit">    
    </form>    
    
    <?php if ($scanSuccess): ?>    
        <div class="success-message">SCAN BERHASIL!</div>    
        <h2 class="output-title">Output Produksi</h2>    
        <div class="result">    
            <p><strong>Product Name:</strong> <?php echo htmlspecialchars($productName); ?></p>    
            <p><strong>Part Number:</strong> <?php echo htmlspecialchars($partNumber); ?></p>    
            <p><strong>Barcode:</strong> <?php echo htmlspecialchars($barcode); ?></p>    
        </div>    
        <a href="list_scan_completion.php" class="back-button">Lihat Semua Scan</a>    
    <?php elseif ($productName): ?>    
        <div class="result">    
            <p><?php echo htmlspecialchars($productName); ?></p>    
        </div>    
    <?php endif; ?>    
    
    <a href="list_scan_completion.php" class="back-button">Lihat Semua Scan</a> <!-- Updated button -->  
</body>    
</html>  
