<!DOCTYPE html>
<html>

<body>
   <style>
    .tabz {
        background-color: rgba(255, 255, 255, 0.5); /* Adjust the alpha (opacity) value as needed */
    }
</style>

<?php
if (isset($_GET['product']) && $_GET['product'] != '') {
    // Save the search value
    $product = trim($_GET['product']);
    $selectedBranch = isset($_GET['branch']) ? trim($_GET['branch']) : 'all';

    // Create a base query and words string
    $query_string = "SELECT * FROM products WHERE ";
    
    // Add branch filter if not 'all'
    if ($selectedBranch !== 'all') {
        $query_string .= "`branch` = '$selectedBranch' AND ";
    }

    $display_words = "";
    $keywords = explode(' ', $product);
    foreach ($keywords as $word) {
        $query_string .= " product_name LIKE '%" . $word . "%' OR ";
        $display_words .= $word . " ";
    }
    $query_string = substr($query_string, 0, strlen($query_string) - 3);

    // Connect to the database
    include_once 'database/shikanisha.kts.php';
    $query = mysqli_query($conn, $query_string);
    $result = mysqli_num_rows($query);

    // Check if results were returned
    if ($result > 0) {
        // Display the results
        ?>
        <div id="table" class="tabz">
           
 
    
    <script src="js/jquery.min.js"></script>



            <div class="card-body tabz">
                <div class="left"><b><u><?php echo $result; ?></u></b> results found</div>
                <h4 class="card-title">Search Results</h4>
                <div class="table-responsive">
                    <table class="table table-striped table-bordered zero-configuration">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Category</th>
                                <th>Shelf</th>
                                <th>Selling Price</th>
                                <th>Available</th>
                                <th>Sell Price/Quantity</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $i = 0;
                            while ($row = mysqli_fetch_array($query)) {
                                $i++;
                            ?>
                            <tr>
                                <td><?php echo $row['product_name']; ?></td>
                                <td><?php echo $row['category']; ?></td>
                                <td><?php echo $row['shelf']; ?></td>
                               
                                <td><?php echo number_format($row['selling_price'], 2); ?></td>
                                <td><?php echo number_format($row['shop_quantity']); ?></td>
                                <td>
                                    <form method="post" action="">
    <input type="hidden" name="name" value="<?php echo $row['product_name']; ?>" />
    <input type="hidden" name="id" value="<?php echo $row['product_id']; ?>" />
  
   <!-- Use Bootstrap classes to style the select element -->
<!-- Select element -->
<!-- Your existing HTML and PHP code -->
<select name="price" class="priceInput" required>
    <option value="<?php echo $row['selling_price']; ?>"><?php echo $row['selling_price']; ?></option>
   
    <option value="custom">Custom Input</option>
</select>

<!-- Input field for custom price -->
<input type="number" name="custom_price" class="customPrice" placeholder="Custom Price" step="0.01" style="display: none;" />

<script>
// Function to handle select change event
function handleSelectChange() {
    // Get the selected value
    var selectedValue = $(".priceInput").val();

    // Find the associated custom price input field
    var customPriceInput = $(".customPrice");

    // Check if it's "custom"
    if (selectedValue === "custom") {
        // Show the custom input field
        customPriceInput.show();
    } else {
        // Hide the custom input field
        customPriceInput.hide();
    }
}

// Attach change event handler to the select element with class "priceInput"
$(".priceInput").change(function() {
    handleSelectChange();
});

// Initial call to handleSelectChange for any pre-selected "Custom" options
handleSelectChange();
</script>


<input type="number" name="qty" placeholder="Quantity" step="0.01" required />
<button type="submit" class="btn btn-secondary" name="add" value="Add">Add</button>
<!-- End of your HTML and PHP code -->
            </form>
                                    
                                </td>
                                
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php
    } else {
        echo 'No Results Found';
    }
} else {
    echo '';
}
?>




</body>
</html>