<?php
session_start();
include_once("../../../../../lib/konfig.php");
include_once("../../../../../lib/function.php");
$id = $_GET['id'];
$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);



$qhead = "SELECT distinct HEAD.ID, 
    HEAD.strKdLPBJ,
    HEAD.strNmDivisiRequestor, 
    HEAD.strFullNameREQ, 
    HEAD.strFullNameSPV, 
    HEAD.strFullNameMGR, 
    HEAD.strFullNameDIR, 
    HEAD.dtCreate, 
    HEAD.dtSubmitSPV, 
    HEAD.dtSubmitMGR, 
    HEAD.dtSubmitDIR, 
    HEAD.stsDone            
    FROM tpc_h_lpbj AS HEAD 
    LEFT JOIN tpc_d_lpbj AS DET ON HEAD.ID=DET.h_lpbj_ID 
    LEFT JOIN item AS list_item ON DET.item_ID=list_item.ID 
    LEFT JOIN tpc_dept AS DEPT ON DET.dept_ID=DEPT.ID 
    WHERE DET.stsAktif=1 AND 
    HEAD.stsAktif=1 AND HEAD.strKdLPBJ = '" . $id . "' AND
    list_item.stsAktif=1 order by HEAD.ID;";

$query = "SELECT  HEAD.ID,HEAD.strKdLPBJ,             
            list_item.strKdItem, 
            list_item.strDeskripsi, 
            DET.iJumlah, 
            DET.item_strUnit AS strUnit, 
            DET.iJumlahStok, 
            DET.strKeteranganREQ, 
            DET.strKeteranganSPV, 
            DET.strKeteranganMGR, 
            DET.strKeteranganDIR, 
            DET.dtDibutuhkan, 
            DET.stsACCSPV, 
            DET.stsACCMGR, 
            DET.stsACCDIR, 
            DEPT.strNmDept,
            DATE_FORMAT(DET.dtDibutuhkan, '%d %b %Y') as tgllpbj 
            FROM tpc_h_lpbj AS HEAD 
            LEFT JOIN tpc_d_lpbj AS DET ON HEAD.ID=DET.h_lpbj_ID 
            LEFT JOIN item AS list_item ON DET.item_ID=list_item.ID 
            LEFT JOIN tpc_dept AS DEPT ON DET.dept_ID=DEPT.ID 
            WHERE HEAD.strKdLPBJ = '" . $id . "' AND DET.stsAktif=1 AND 
            HEAD.stsAktif=1 AND 
            list_item.stsAktif=1;";

$head = $MySQLi->query($qhead);
$detail = $MySQLi->query($query);
?>

<?php //$_SESSION['strJenisAkses']; 
?>
<style>
    td,
    th {
        vertical-align: top;
    }
</style>
<table id="tbllpbj" class="display cell-border" style="width:100%">
    <thead>
        <tr>
            <th>No</th>
            <th>Kode LPBJ</th>
            <th>Divisi</th>
            <th>Requestor</th>
            <th>SPV</th>
            <th>MGR</th>
            <th>DIR</th>
            <th>Kode Barang</th>
            <th>Deskripsi</th>
            <th>Departement</th>
            <th>Jumlah</th>
            <th>Satuan</th>
            <th>Jumlah Stok</th>
            <th>Keterangan</th>
            <th>Persetujuan</th>
            <th>Status</th>
            <th>Prioritas</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        foreach ($head as $h) {
            if ($h['dtSubmitSPV'] == "1000-01-01")
                $dtSubmitSPV = "<i class='bi bi-clock'></i>"; //Pending
            else $dtSubmitSPV = $h['dtSubmitSPV'];

            if ($h['dtSubmitMGR'] == "1000-01-01")
                $dtSubmitMGR = "<i class='bi bi-clock'></i>";
            else $dtSubmitMGR = $h['dtSubmitMGR'];

            if ($h['dtSubmitDIR'] == "1000-01-01")
                $dtSubmitDIR = "<i class='bi bi-clock'></i>";
            else $dtSubmitDIR = $h['dtSubmitDIR'];

            echo "<tr>";
            echo "<td valign='top'>" . $i . "</td>";
            echo "<td><button class='btn btn-primary btn-sm' type='button' onclick='cetak(" . $h['ID'] . ")'><i class='bi bi-printer'></i></button>&nbsp;" . $h['strKdLPBJ'] . "</td>";
            echo "<td>" . $h['strNmDivisiRequestor'] . "</td>";
            echo "<td>" . $h['strFullNameREQ'] . "</td>";
            echo "<td>" . $h['strFullNameSPV'] . " / " . $dtSubmitSPV . "</td>";
            echo "<td>" . $h['strFullNameMGR'] . " / " . $dtSubmitMGR . "</td>";
            echo "<td>" . $h['strFullNameDIR'] . " / " . $dtSubmitDIR . "</td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($h['ID'] == $d['ID']) {
                    echo $d['strKdItem'] . "<br>";
                }
            }
            echo "</td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($h['ID'] == $d['ID']) {
                    echo $d['strDeskripsi'] . "<br>";
                }
            }
            echo "</td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($h['ID'] == $d['ID']) {
                    echo $d['strNmDept'] . "<br>";
                }
            }
            echo "</td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($h['ID'] == $d['ID']) {
                    echo $d['iJumlah'] . "<br>";
                }
            }
            echo "</td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($h['ID'] == $d['ID']) {
                    echo $d['strUnit'] . "<br>";
                }
            }
            echo "</td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($h['ID'] == $d['ID']) {
                    echo $d['iJumlahStok'] . "<br>";
                }
            }
            echo "</td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($h['ID'] == $d['ID']) {
                    echo $d['strKeteranganREQ'] . "<br>";
                }
            }
            echo "</td>";

            echo "<td></td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($d['stsACCSPV'] > 0 && $d['stsACCMGR'] > 0 && $d['stsACCDIR'] > 0)
                    if ($d['stsACCSPV'] == 2 || $d['stsACCMGR'] == 2 || $d['stsACCDIR'] == 2)
                        $stsItem = "Pending Approval";
                    else $stsItem = "Disetujui";
                else $stsItem = "Ditolak";
                if ($h['ID'] == $d['ID']) {
                    echo $stsItem . "<br>";
                }
            }
            echo "</td>";

            echo "<td>";
            foreach ($detail as $d) {
                if ($h['ID'] == $d['ID']) {
                    echo $d['tgllpbj'] . "<br>";
                }
            }
            echo "</td>";

            echo "</tr>";
            $i++;
        }
        ?>
    </tbody>
</table>

<script>
    $(document).ready(function() {
        $('#tbllpbj').DataTable({
            scrollX: true,
            columnDefs: [{
                    width: '5%',
                    targets: 0
                },
                {
                    width: '50%',
                    targets: 1
                },
                {
                    width: '20%',
                    targets: 2
                },
                {
                    width: '10%',
                    targets: 3
                },
                {
                    width: '15%',
                    targets: 4
                },
                {
                    width: '15%',
                    targets: 5
                },
                {
                    width: '15%',
                    targets: 6
                },
                {
                    width: '15%',
                    targets: 7
                },
                {
                    width: '15%',
                    targets: 8
                },
                {
                    width: '15%',
                    targets: 9
                },
                {
                    width: '15%',
                    targets: 10
                },
                {
                    width: '15%',
                    targets: 11
                },
                {
                    width: '15%',
                    targets: 12
                },
                {
                    width: '15%',
                    targets: 13
                },
                {
                    width: '15%',
                    targets: 14
                },
                {
                    width: '15%',
                    targets: 15
                },
                {
                    width: '15%',
                    targets: 16
                }
            ],
            autoWidth: false // Disable automatic column width calculation
        });
    });
</script>