<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$status = "";

// Run only when form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $product_name = substr(trim($_POST["product_name"]), 0, 20); // Max 20 chars
    $product_price = $_POST["product_price"];

    // COM port setting — Update this according to your VFD connection
    $comPort = "COM3"; // e.g., COM1, COM3, COM4 for USB/Serial

    // Format lines
    $line1 = str_pad($product_name, 20);         // Product Name
    $line2 = str_pad("Rs. " . $product_price, 20); // Product Price

    // Set COM port settings (9600 baud, 8 data bits, no parity, 1 stop bit)
    exec("mode $comPort: BAUD=9600 PARITY=n DATA=8 STOP=1 xon=off");

    // Try to open the port
    $fp = @fopen($comPort, "w+");

    if ($fp) {
        // Clear screen (chr(12))
        fwrite($fp, chr(12));
        usleep(50000); // Delay

        // Write product name
        fwrite($fp, $line1);
        usleep(50000);

        // Move to next line (CR + LF)
        fwrite($fp, "\r\n");
        usleep(50000);

        // Write product price
        fwrite($fp, $line2);
        fclose($fp);

        $status = "✅ Sent to VFD: $product_name | Rs. $product_price";
    } else {
        $status = "❌ Unable to open $comPort. Check port or permissions.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Send to HP RP7 VFD Display</title>
    <style>
        body { font-family: Arial, sans-serif; background: #f0f0f0; padding: 20px; }
        form {
            background: #fff; padding: 20px; border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1); max-width: 400px;
        }
        input[type=text], input[type=number] {
            width: 100%; padding: 10px; margin: 10px 0;
            border: 1px solid #ccc; border-radius: 5px;
        }
        input[type=submit] {
            background: #28a745; color: white; padding: 10px 15px;
            border: none; border-radius: 5px; cursor: pointer;
        }
        .status {
            margin-top: 20px; font-weight: bold;
        }
    </style>
</head>
<body>
    <h2>HP RP7 VFD Display Test</h2>
    <form method="post">
        <label>Product Name:</label>
        <input type="text" name="product_name" maxlength="20" required>

        <label>Product Price:</label>
        <input type="number" name="product_price" step="0.01" required>

        <input type="submit" value="Send to Display">
    </form>

    <?php if (!empty($status)) echo "<div class='status'>$status</div>"; ?>
</body>
</html>
