<?php
$page_title = 'Product Data';
require_once('includes/load.php');
page_require_level(1);

// Mengganti query untuk mengambil data dari tabel "data"
if (isset($_POST['search'])) {
    $search_query = $_POST['search_query'];
    $search_option = $_POST['search_option'];

    if ($search_option === 'part_number' || $search_option === 'description') {
        $products = get_all_data_with_search($search_query, $search_option, $db); // Pass the MySQLi instance to the function
    } else {
        // ...
    }
}
    $products = get_all_data();
?>
<?php include_once('layouts/header.php'); ?>
<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <div class="pull-right">
                    <a href="add_product.php" class="btn btn-primary">Add New</a>
                    <a href="#" id="exportCSV" class="btn btn-success">Export</a>
                    <!-- ... Kode ekspor CSV ... -->
                </div>
            </div>
            <div class="panel-body">
                <form method="post" action="view_data.php" class="form-inline">
                    <div class="form-group">
                        <input type="text" class="form-control" name="search_query" placeholder="Search...">
                    </div>
                    <div class="form-group">
                        <select class="form-control" name="search_option">
                            <option value="part_number">Search by Part Number</option>
                            <option value="description">Search by Description</option>
                        </select>
                    </div>
                    <button type="submit" name="search" class="btn btn-default">Search</button>
                </form>
                <div class="row">
                    <div class="col-md-12">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th class="text-center">#</th>
                                    <th>Part Number</th>
                                    <th>Description</th>
                                    <th>Color</th>
                                    <th class="text-center">Unit Price</th>
                                    <th class="text-center">Unit Measurements</th>
                                    <th class="text-center">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($products as $product) : ?>
                                    <tr>
                                        <!-- Data Produk -->
                                        <td class="text-center"><?php echo count_id(); ?></td>
                                        <td><?php echo isset($product['part_number']) ? htmlspecialchars($product['part_number']) : 'N/A'; ?></td>
                                        <td><?php echo remove_junk($product['description']); ?></td>
                                        <td><?php echo remove_junk($product['color']); ?></td>
                                        <td class="text-center"><?php echo remove_junk($product['unit_price']); ?></td>
                                        <td class="text-center"><?php echo remove_junk($product['unit_measurements']); ?></td>
                                        <td class="text-center">
                                            <div class="btn-group">
                                                <a href="edit_product.php?id=<?php echo (int) $product['id']; ?>" class="btn btn-info btn-xs" title="Edit" data-toggle="tooltip">
                                                    <span class="glyphicon glyphicon-edit"></span>
                                                </a>
                                                <a href="delete_product.php?id=<?php echo (int) $product['id']; ?>" class="btn btn-danger btn-xs" title="Delete" data-toggle="tooltip">
                                                    <span class="glyphicon glyphicon-trash"></span>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include_once('layouts/footer.php'); ?>
