<?php
$page_title = 'List BOM RND';
require_once('includes/load.php');
page_require_level(2); // Hanya izinkan user level 2 atau lebih tinggi  

if (isset($_POST['search'])) {
    $search = $db->escape($_POST['search']);
    $sql = "SELECT bl.*,   
                   p1.name AS parent_name,   
                   p1.new_code AS parent_new_code,   
                   p2.name AS child_name,   
                   p2.new_code AS child_new_code,   
                   p2.uom AS uom,   
                   (p2.buy_price * bl.qty) AS unit_price   
            FROM bom_level_rnd bl  
            LEFT JOIN products p1 ON bl.parent_bom = p1.part_number  
            LEFT JOIN products p2 ON bl.child_bom = p2.part_number  
            WHERE bl.parent_bom LIKE '%$search%'  
            OR bl.child_bom LIKE '%$search%'  
            OR p1.name LIKE '%$search%'  
            OR p2.name LIKE '%$search%'  
            OR p2.uom LIKE '%$search%'  
            OR p1.new_code LIKE '%$search%'  
            OR p2.new_code LIKE '%$search%'";
    $boms = find_by_sql($sql);
} else {
    $sql = "SELECT bl.*,   
                   p1.name AS parent_name,   
                   p1.new_code AS parent_new_code,   
                   p2.name AS child_name,   
                   p2.new_code AS child_new_code,   
                   p2.uom AS uom,   
                   (p2.buy_price * bl.qty) AS unit_price   
            FROM bom_level_rnd bl  
            LEFT JOIN products p1 ON bl.parent_bom = p1.part_number  
            LEFT JOIN products p2 ON bl.child_bom = p2.part_number";
    $boms = find_by_sql($sql);
}

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">
                <strong>
                    <span class="glyphicon glyphicon-th"></span>
                    <span>Bill of Materials RND List</span>
                </strong>
                <div class="pull-right">
                    <a href="add_bom_rnd.php" class="btn btn-primary">Add New BOM RND</a>
                    <button type="button" class="btn btn-success" id="exportExcelBtn">Export Excel</button>
                </div>
            </div>
            <div class="panel-body">
                <form method="post" action="bom_list_rnd.php" class="form-inline mb-3">
                    <div class="form-group">
                        <label for="search" class="sr-only">Search</label>
                        <input type="text" class="form-control" id="search" name="search" placeholder="Search BOM RND">
                    </div>
                    <button type="submit" name="search_btn" class="btn btn-primary">Search</button>
                </form>
                <div class="table-responsive">
                    <table class="table table-striped table-bordered table-hover" id="bomTable">
                        <thead>
                            <tr>
                                <th class="text-center" style="width: 5%;"> # </th>
                                <th> Parent BOM </th>
                                <th> Parent Name </th>
                                <th> New Code </th>
                                <th> Child BOM </th>
                                <th> Child Name </th>
                                <th> New Code </th>
                                <th> Quantity </th>
                                <th> UOM </th>
                                <th> Unit Price </th>
                                <th class="text-center" style="width: 15%;"> Actions </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($boms as $bom): ?>
                                <tr>
                                    <td class="text-center"><?php echo $bom['id']; ?></td>
                                    <td><?php echo htmlspecialchars($bom['parent_bom']); ?></td>
                                    <td><?php echo htmlspecialchars($bom['parent_name']); ?></td>
                                    <td><?php echo htmlspecialchars($bom['parent_new_code']); ?></td>
                                    <td><?php echo htmlspecialchars($bom['child_bom']); ?></td>
                                    <td><?php echo htmlspecialchars($bom['child_name']); ?></td>
                                    <td><?php echo htmlspecialchars($bom['child_new_code']); ?></td>
                                    <td><?php echo $bom['qty']; ?></td>
                                    <td><?php echo htmlspecialchars($bom['uom']); ?></td>
                                    <td><?php echo number_format($bom['unit_price'], 2); ?></td>
                                    <td class="text-center">
                                        <div class="btn-group">
                                            <a href="edit_bom_rnd.php?id=<?php echo (int)$bom['id']; ?>" class="btn btn-xs btn-primary"
                                                title="Edit">
                                                <span class="glyphicon glyphicon-edit"></span>
                                            </a>
                                            <a href="delete_bom_rnd.php?id=<?php echo (int)$bom['id']; ?>" class="btn btn-xs btn-danger"
                                                title="Delete" onclick="return confirm('Are you sure you want to delete this BOM RND?');">
                                                <span class="glyphicon glyphicon-trash"></span>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>
<script>
    document.getElementById('exportExcelBtn').addEventListener('click', function() {
        var wb = XLSX.utils.table_to_book(document.getElementById('bomTable'));
        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'
        }), 'bom_list_rnd.xlsx');
    });
</script>