<?php
$page_title = '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'];
    $data = get_all_data($search_query);
} else {
    $data = 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_data.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="get" action="search_result.php" class="form-inline" id="searchForm">
    <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" 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">UOM</th>
                                    <th class="text-center">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($data as $data) : ?>
                                    <tr>
                                        <td class="text-center"><?php echo count_id(); ?></td>
                                        <td><?php echo isset($data['part_number']) ? htmlspecialchars($data['part_number']) : 'N/A'; ?></td>
                                        <td><?php echo remove_junk($data['description']); ?></td>
                                        <td><?php echo remove_junk($data['color']); ?></td>
                                        <td class="text-center"><?php echo remove_junk($data['unit_price']); ?></td>
                                        <td class="text-center"><?php echo remove_junk($data['unit_measurements']); ?></td>
                                      <td class="text-center">
    <div class="btn-group">
        <a href="edit_data.php?id=<?php echo $data['id']; ?>" class="btn btn-xs btn-warning" title="Edit">
            <i class="glyphicon glyphicon-pencil"></i>
        </a>
        <a href="delete_data.php?id=<?php echo $data['id']; ?>" class="btn btn-xs btn-danger" title="Delete">
            <i class="glyphicon glyphicon-trash"></i>
        </a>
    </div>
</td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        document.getElementById('searchForm').addEventListener('submit', function (event) {
            event.preventDefault();

            var form = event.target;
            var formData = new FormData(form);

            var xhr = new XMLHttpRequest();
            xhr.open('GET', form.action + '?' + new URLSearchParams(formData).toString(), true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
                    var searchResult = xhr.responseText;

                    // Tampilkan hasil pencarian dalam pop-up
                    var popup = window.open('', 'Search Result', 'width=600,height=400');
                    popup.document.write(searchResult);
                    popup.document.close();
                }
            };
            xhr.send();
        });
    });
</script>


<?php include_once('layouts/footer.php'); ?>
