<?php 
include('connection.php');
if (isset($_POST['orderid'])) {
    $orderID = $_POST['orderid'];

    // Retrieve order details based on the order ID
    $sql = "SELECT * FROM new_order WHERE order_id = '$orderID'";
    $res = mysqli_query($conn, $sql);

    if ($row = mysqli_fetch_array($res)) {
        $productName = $row['product_name'];

        // Output the order details HTML with a table
        echo '
            <div class="row">
                <div class="col-12">
                    <table id="myTable" class="table table-bordered">
                        <tbody>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">Origin :</td>
                                <td style="color:purple;">' . $row['origin'] . '</td>
                            </tr>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">Name :</td>
                                <td style="color:purple;">' . $row['name'] . '</td>
                            </tr>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">Address :</td>
                                <td style="color:purple;">' . $row['address'] . '</td>
                            </tr>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">City :</td>
                                <td style="color:purple;">' . $row['city'] . '</td>
                            </tr>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">State :</td>
                                <td style="color:purple;">' . $row['state'] . '</td>
                            </tr>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">Zip Code  :</td>
                                <td style="color:purple;">' . $row['zipcode'] . '</td>
                            </tr>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">Country  :</td>
                                <td style="color:purple;">' . $row['country'] . '</td>
                            </tr>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">Product  :</td>
                                <td style="color:purple;">' . $row['product_name'] . '</td>
                            </tr>
                            <tr>
                                <td style="font-weight:bold;text-transform: uppercase;">Quantity  :</td>
                                <td style="color:purple;">' . $row['quantity'] . '</td>
                            </tr>
                            <!-- Add more rows as needed -->
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="row">
                <div class="col-12">
                    <button id="copyButton" class="btn btn-dark">Copy</button>
                </div>
            </div>

            <script>
            // JavaScript code for copying the table with its HTML structure
            const copyButton = document.getElementById("copyButton");
            const table = document.getElementById("myTable");
        
            copyButton.addEventListener("click", () => {
                const range = document.createRange();
                range.selectNode(table);
                window.getSelection().addRange(range);
        
                try {
                    // Execute the copy command
                    const successful = document.execCommand("copy");
                    const message = successful ? "Table copied to clipboard" : "Unable to copy table";
                    console.log(message);
                } catch (err) {
                    console.error("Failed to copy table", err);
                }
        
                // Modify the copied text to remove extra spaces around colons
                const copiedText = window.getSelection().toString();
                const modifiedText = copiedText.replace(/\s*:\s*/g, ":");
                // Copy the modified text back to the clipboard
                const clipboard = navigator.clipboard || window.clipboardData;
                clipboard.writeText(modifiedText.trim()); // Use trim() to remove leading/trailing spaces
        
                // Clear the selection
                window.getSelection().removeAllRanges();
            });
        </script>
        

        ';
    }
}


?>