<?php
$page_title = 'All product';
require_once('includes/load.php');
page_require_level(3);
if (isset($_POST['search'])) {
    $search_query = $_POST['search_query'];
    $search_option = $_POST['search_option'];

    if ($search_option === 'part_number') {
        $products = search_products_by_part_number($search_query);
    } elseif ($search_option === 'product') {
        $products = search_products_by_name($search_query);
    } else {
        // Handle other cases or show error message
    }
} else {
    $products = join_product_table(); // Ganti dengan fungsi untuk mendapatkan semua produk
}

?>
<?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="export_product.php" class="btn btn-success">Export</a>
                <a href="import_product.php" class="btn btn-danger">Import</a> <!-- Gunakan kelas btn-danger untuk warna merah -->
            </div>
        </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-12">
                        <div class="float-left">
                            <img src="images/logo.png" alt="Logo" style="max-width: 50px; max-height: 50px;">
                            <span style="font-weight: bold;">PT Siklus Karya Global, Beta Version R.0 (by Nanda)</span>
                        </div>
                        <div class="float-right">
                           
                        
                            <form method="post" action="all_products.php" class="form-inline">
                                 <form method="post" action="all_products.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="product">Search by Product Name</option>
            </select>
        </div>
        <button type="submit" name="search" class="btn btn-default">Search</button>
    </form>
</div>
                <!-- Tabel Produk -->
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th class="text-center" style="width: 50px;">#</th>
                                    <th> part number </th>
                                    <th> Product </th>
                                    <th class="text-center" style="width: 10%;"> Categories </th>
                                    <th class="text-center" style="width: 10%;"> In-Stock </th>
                                    <th class="text-center" style="width: 10%;"> Product Added </th>
                                    <th class="text-center" style="width: 100px;"> 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['name']); ?></td>
                                        <td class="text-center">
    <?php echo isset($product['categorie_id']) ? remove_junk($product['categorie_id']) : 'N/A'; ?>
</td>

                                        <td class="text-center"><?php echo get_product_stock($product['id']); ?></td>
                                        <td class="text-center"><?php echo read_date($product['date']); ?></td>
                                        <td class="text-center">
                                            <div class="btn-group">
                                               
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include_once('layouts/footer.php'); ?>