<?php
// Enable error reporting for debugging (remove in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Include database connection
include __DIR__ . '/database/database.php';
include __DIR__ . '/database/data.php';

// Ensure `$product` is defined (replace 'your_table_name' with actual name)
// Example: 'products'

$sql_rate = "SELECT * FROM ".$rate." LIMIT 1"; // Add LIMIT 1 to ensure only one row is fetched
$result = $connection->query($sql_rate);

if ($result && $result->num_rows > 0) {
    $row_rate = $result->fetch_assoc();
    $baht = $row_rate['baht']; 
    $yuan = $row_rate['yuan']; 
} else {
    // Handle case where no rows are returned
    $baht = null;
    $yuan = null;
}

$query = isset($_GET['q']) ? mysqli_real_escape_string($connection, $_GET['q']) : '';
$limit = 20; // You can adjust this for search result limits

$sql = "SELECT * FROM $product WHERE 
        product_name LIKE '%$query%' 
        OR barcode LIKE '%$query%'
        OR barcode_1 LIKE '%$query%'
        OR type LIKE '%$query%' 
        ORDER BY id DESC LIMIT $limit";

$result = mysqli_query($connection, $sql);

// Check query execution
if (!$result) {
    die("Query failed: " . mysqli_error($connection));
}

// Generate output
if ($result && mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<div class="col-md-3 mb-3 card-item">
                <div class="card bg-dark text-white">
                    <div class="card-body">
                        <div style="justify-content: center;">
                            <div class="image-container">';

        // Display image logic
        $extensions = ['jpg', 'jpeg', 'png', 'gif'];
        $imagePath = '';
        foreach ($extensions as $ext) {
            $potentialPath = 'uploads/' . $row["product_name"] . '.' . $ext;
            if (file_exists($potentialPath)) {
                $imagePath = $potentialPath;
                break;
            }
        }
        if ($imagePath) {
            echo '<img src="' . $imagePath . '" class="img-fluid mb-3" alt="Sample Image" style="margin-left:auto;margin-right:auto;width:200px;">';
        } else {
            echo 'Image not found.';
        }

        if (!function_exists('forkyat')) {
            function forkyat($number)
            {
                return ceil($number / 100) * 100;
            }
        }
        // Render card content
        echo '</div></div>
              <h6 class="card-text" style="margin-top:20px"><strong>ပစ္စည်းအမည်:</strong> ' . htmlspecialchars($row['product_name']) . '</h6>
              <h6 class="card-text" style="margin-top:20px"><strong>code 1:</strong> ' . htmlspecialchars($row['barcode']) . '</h6>
              <h6 class="card-text" style="margin-top:20px"><strong>code 2:</strong> ' . htmlspecialchars($row['barcode_1']) . '</h6>
              <h6 class="card-text" style="margin-top:20px"><strong>အမျိုးစား:</strong> ' . htmlspecialchars($row['type']) . '</h6>
              <h6 class="card-text" style="margin-top:20px"><strong>ငွေဈေးနှုန်း:</strong> ' . htmlspecialchars($row['rate'] . " (" . $row['currency_buy'] . ")") . '</h6>
              <h6 class="card-text" style="margin-top:20px"><strong>ကားခ:</strong> ' . htmlspecialchars($row['trans']) . '</h6>
                  <h6 class="card-text" style="margin-top:20px"><strong>ဖာလိုက်ဝယ်ဈေး:</strong> ' . htmlspecialchars($row['buy']) . '</h6>

              <table class="table table-bordered"> 
                <tr>
                <td colspan="3" style="text-align:center">ရောင်းဈေး</td>
                </tr>

                <tr>
                <td style="text-align:center">ဖာ</td>
                <td style="text-align:center">ကဒ်/ထုပ်</td>
                <td style="text-align:center">ဗူး/ခု</td>
                </tr>

                <tr>
                <td style="text-align:center;color:white">' . ($row["round_package_price"]) . '</td>
                <td style="text-align:center;color:white">' . ($row["round_unit_price"]) . '</td>
                <td style="text-align:center;color:white">' . ($row["round_pieces_price"]) . '</td>
                </tr>
              </table>

             

              

                 <table class="table"> 


                <tr>
                <td>
                <form action="product_photo_view.php" method="POST"  target="_blank" style="width:100%">
                        <input type="hidden" name="product_name" value="' . htmlspecialchars($row['product_name']) . '">
                        <input type="hidden" name="action" value="info">
                        <button type="submit" class="btn btn-outline-success" name="info_button" style="width:100%">ပြင်ရန်</button>
                    </form> 
                </td>

                <td>
                <form action="product_view_photo.php" method="POST"  target="_blank" style="margin-left:10px;width:100%">
                        <input type="hidden" name="product_name" value="' . htmlspecialchars($row['product_name']) . '">
                        <input type="hidden" name="action" value="info">
                        <button type="submit" class="btn btn-outline-danger" name="delete" style="width:100%">ဖျက်ရန်</button>
                    </form> 
                </td>

                </tr>

                <tr>
                    <td colspan=2>
                <form action="print_barcode.php" method="POST"  target="_blank" style="margin-left:10px;width:100%">
                        <input type="hidden" name="product_name" value="' . htmlspecialchars($row['product_name']) . '">
                        <input type="hidden" name="action" value="info">
                        <button type="submit" class="btn btn-outline-warning" name="delete" style="width:100%">Print Barcode</button>
                    </form> 
                </td>

                </tr>


                </table>


            </div>
        </div>
    </div>';

    
    }
} else {
    echo '<p class="text-center text-white">No results found.</p>';
}
