<?php
$page_title = 'Stock All Products';
require_once('includes/load.php');
page_require_level(3); // Pastikan level akses sesuai dengan kebutuhan

// Query untuk mengambil produk dengan quantity lebih dari 0
$products = find_all("products", ["conditions" => ["quantity > 0"]]);

// Inisialisasi variabel total inventory
$total_inventory = 0;

?>

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

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <h4 class="panel-title pull-left">Stock All Products</h4>
                <div class="pull-right">
                    <h4>Total Inventory: <?php echo number_format($total_inventory, 2); ?></h4>
                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#exportModal">Export to
                        Excel</button>
                </div>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th class="text-center">#</th>
                                <th>Part Number</th>
                                <th>Product</th>
                                <th class="text-center">Categories</th>
                                <th class="text-center">In-Stock</th>
                                <th class="text-center">UoM</th>
                                <th class="text-center">Unit Price</th>
                                <th class="text-center">Location ID</th>
                                <th class="text-center">Total Amount</th> <!-- Kolom baru untuk total amount -->
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($products as $product) : ?>
                                <?php $stock = get_product_stock($product['id']); ?>
                                <?php if ($stock > 0) : ?>
                                    <?php
                                    // Hitung total amount
                                    $total_amount = $product['buy_price'] * $stock;
                                    // Tambahkan ke total inventory
                                    $total_inventory += $total_amount;
                                    ?>
                                    <tr>
                                        <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
                                            // Query untuk mengambil nama kategori berdasarkan categorie_id
                                            $category = find_by_id('categories', $product['categorie_id']);
                                            echo $category['name'];
                                            ?>
                                        </td>
                                        <td class="text-center"><?php echo remove_junk($stock); ?></td>
                                        <td class="text-center"><?php echo remove_junk($product['uom']); ?></td>
                                        <td class="text-center"><?php echo remove_junk($product['buy_price']); ?></td>
                                        <td class="text-center"><?php echo remove_junk($product['loc_id']); ?></td>
                                        <td class="text-center"><?php echo number_format($total_amount, 2); ?></td>
                                        <!-- Menampilkan total amount -->
                                    </tr>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal Export Excel -->
<div class="modal fade" id="exportModal" tabindex="-1" role="dialog" aria-labelledby="exportModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exportModalLabel">Export Data</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <!-- Add your export options or content here -->
                <p>Choose the export format and options:</p>
                <button type="button" id="exportExcelBtn" class="btn btn-primary">Export to Excel (XLSX)</button>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<!-- End Modal Export Excel -->

<?php include_once('layouts/footer.php'); ?>
<script>
    document.getElementById('exportExcelBtn').addEventListener('click', function() {
        var wb = XLSX.utils.table_to_book(document.querySelector('.table'));
        var wbout = XLSX.write(wb, {
            bookType: 'xlsx',
            bookSST: true,
            type: 'binary'
        });

        function s2ab(s) {
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for (var i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF;
            return buf;
        }

        saveAs(new Blob([s2ab(wbout)], {
            type: 'application/octet-stream'
        }), 'stock_all_products.xlsx');
    });
</script>
<!-- End of Script for Exporting to Excel -->