<?php
include_once("../../../../lib/konfig.php");
include_once("../../../../lib/function.php");
$cari = $_GET['cari'];
session_start();
$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
$query = "SELECT A.ID, 
A.strKdItem, 
A.strDeskripsi, 
A.strUnit, 
A.strUnit2, 
A.strUnit3, 
A.iRatio2, 
A.iRatio3,
A.strItemLoc,
ifnull(B.hitung_ID,0) as hitung_ID 
FROM item A
Left join(select item_ID,count(item_ID) as hitung_ID from tpc_d_lpbj)B on B.item_ID = A.ID
WHERE A.stsAktif > 0 and A.strDeskripsi like '%" . $cari . "%' order by A.strKdItem;";
$result = $MySQLi->query($query);
?>






<link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">

<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>


<table id="tblbrg" class="table table-striped table-sm table-bordered">
    <thead>
        <tr>
            <th>#</th>
            <th>Kode</th>
            <th>Deskripsi</th>
            <th>Unit 1</th>
            <th>Unit 2</th>
            <th>Ratio 2</th>
            <th>Unit 3</th>
            <th>Ratio 3</th>
            <th>Lokasi</th>
            <th>Option</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        foreach ($result as $r) {
            echo "<tr>";
            echo "<td>" . $i . "</td>";
            echo "<td>" . $r['strKdItem'] . "</td>";
            echo "<td>" . $r['strDeskripsi'] . "</td>";
            echo "<td>" . $r['strUnit'] . "</td>";
            echo "<td>" . $r['strUnit2'] . "</td>";
            echo "<td>" . $r['iRatio2'] . "</td>";
            echo "<td>" . $r['strUnit3'] . "</td>";
            echo "<td>" . $r['iRatio3'] . "</td>";
            echo "<td>" . $r['strItemLoc'] . "</td>";
            if ($r['hitung_ID'] == 0) {
                echo "<td><div class='btn-group' role='group' aria-label='Basic outlined example'>
                <button type='button' class='btn btn-outline-primary btn-sm' data-bs-toggle='modal' data-bs-target='#Modalupdate' onclick='editbrg(" . $r['ID'] . ",\"" . $r['strKdItem'] . "\",\"" . $r['strDeskripsi'] . "\",\"" . $r['strUnit'] . "\",\"" . $r['strUnit2'] . "\"," . $r['iRatio2'] . ",\"" . $r['strUnit3'] . "\"," . $r['iRatio3'] . ",\"" . $r['strItemLoc'] . "\")'><i class='bi bi-pencil'></i></button>                
                </div></td>";
            } else {
                if ($r['hitung_ID'] > 0) {
                    echo "<td><div class='btn-group' role='group' aria-label='Basic outlined example'>
                    <button type='button' class='btn btn-outline-primary btn-sm'><i class='bi bi-pencil'></i></button>
                    <button type='button' class='btn btn-outline-danger btn-sm' onclick='hapusbrg(" . $r['ID'] . ")'><i class='bi bi-trash'></i></button>
                    </div></td>";
                }
            }
            echo "</tr>";
            $i++;
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="10"><b>Input Barang Baru</b></td>
        </tr>

        <tr>
            <td colspan="2"><input type="text" class="form-control" id="kodebrg" placeholder="Kode Barang"></td>
            <td><input type="text" class="form-control" id="deskripsibrg" placeholder="Deskripsi Barang"></td>
            <td><input type="text" class="form-control" id="unit1brg" placeholder="Unit 1"></td>
            <td><input type="text" class="form-control" id="unit2brg" placeholder="Unit 2"></td>
            <td><input type="text" class="form-control" id="ratio2brg" placeholder="Ratio 2" value="0"></td>
            <td><input type="text" class="form-control" id="unit3brg" placeholder="Unit 3"></td>
            <td><input type="text" class="form-control" id="ratio3brg" placeholder="Ratio 3" value="0"></td>
            <td><input type="text" class="form-control" id="lokasibrg" placeholder="Lokasi Barang"></td>
            <td><button class="btn btn-primary" type="button" onclick="tambahbrg()">Simpan</button></td>
        </tr>
        <tr>
            <td colspan="10"></td>
        </tr>

    </tfoot>
</table>

<script>
    $(document).ready(function() {
        $('#tblbrg').DataTable();
    });
</script>