<?php
session_start();
include_once("../../../../../lib/konfig.php");
include_once("../../../../../lib/function.php");

$id = $_GET['id'];

$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);

?>

<table id="tbllpbj1" class="display" style="width:100%">
    <thead>
        <tr>
            <th>No</th>
            <th>Kode LPBJ</th>
            <th>Requestor</th>
            <th>SPV</th>
            <th>MGR</th>
            <th>DIR</th>
            <th>Option</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);

        $query = "SELECT  HEAD.ID, 
                    HEAD.strKdLPBJ, 
                    HEAD.strFullNameREQ, 
                    HEAD.strFullNameSPV, 
                    HEAD.strFullNameMGR, 
                    HEAD.strFullNameDIR, 
                    HEAD.dtCreate, 
                    HEAD.dtSubmitSPV, 
                    HEAD.dtSubmitMGR, 
                    HEAD.dtSubmitDIR, 
                    HEAD.dtSubmitAccurate, 
                    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 
                    WHERE DET.stsAktif=1 AND HEAD.stsAktif=1 AND list_item.stsAktif=1 AND HEAD.ID = " . $id;




        $i = 1;
        //echo $query;
        //die($query);
        $result = $MySQLi->query($query);
        while ($row = $result->fetch_array()) {
            $tombolEdit = "";
            if ($row['dtSubmitSPV'] == "1000-01-01")
                $dtSubmitSPV = "Pending";
            else
                $dtSubmitSPV = $row['dtSubmitSPV'];

            if ($row['dtSubmitMGR'] == "1000-01-01")
                $dtSubmitMGR = "Pending";
            else
                $dtSubmitMGR = $row['dtSubmitMGR'];

            if ($row['dtSubmitDIR'] == "1000-01-01")
                $dtSubmitDIR = "Pending";
            else {
                $dtSubmitDIR = $row['dtSubmitDIR'];
                if ($row['dtSubmitAccurate'] == "1000-01-01" && $row['dtSubmitDIR'] <> "1000-01-01" && strtoupper($_SESSION['strJobPosition']) == "ACCURATE" && $row['sumACCDIR'] > 0)
                    $tombolEdit = "<a href='index.php?menu=persetujuan-lpbj&edit=1&lpbj=" . $row['strKdLPBJ'] . "' class='btn btn-space btn-primary'>Edit</a>";
                else {
                    if (strtoupper($_SESSION['strJobPosition']) == "PURCHASING" && $row['stsDone'] <> 1 && $row['dtSubmitAccurate'] <> "1000-01-01" && $row['sumACC_Accurate'] > 0)
                        $tombolEdit = "<a href='index.php?menu=edit-lpbj&lpbj=" . $row['strKdLPBJ'] . "' class='btn btn-space btn-primary'>Edit</a><a href='transaksi/persediaan/pemesanan/simpan.edit.pesan.barang.php?opt=done&id=" . $row['ID'] . "' class='btn btn-space btn-primary'>Done</a>";
                }
            }

            echo "<tr>
                    <td>" . $i . "</td>
                    <td>" . $row['strKdLPBJ'] . "</td>
                    <td>" . $row['strFullNameREQ'] . " / " . $row['dtCreate'] . "</td>
                    <td>" . $row['strFullNameSPV'] . " / " . $dtSubmitSPV . "</td>
                    <td>" . $row['strFullNameMGR'] . " / " . $dtSubmitMGR . "</td>
                    <td>" . $row['strFullNameDIR'] . " / " . $dtSubmitDIR . "</td>
                    <td>" . $tombolEdit . "<a href='transaksi/persediaan/pemesanan/print.pesan.barang.php?ID=" . $row['ID'] . "' class='btn btn-space btn-primary' target='_blank'>Print Preview</a></td>
                </tr>";
            $i++;
        }
        ?>
    </tbody>
</table>

<script>
    $(document).ready(function() {
        $('#tbllpbj1').DataTable({
            scrollX: true,
        });
    });
</script>