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

$akses = "";

if ($_SESSION['strDivision'] == "HQ")
    $akses = "";
elseif ($_SESSION['strJobPosition'] == "Purchasing" || $_SESSION['strJobPosition'] == "Accurate") {
    // $where=$where." AND strKdLPBJ like '".$_SESSION['strKdLokasi']."%' ";
    $akses = $akses . " ";
    $having = $having . "HAVING countDetails >= 1 AND
                        NOT (sumACCSPV = 0) AND
                        NOT (sumACCMGR = 0) AND
                        NOT (sumACCDIR = 0)";
} else {
    switch ($_SESSION['strJenisAkses']) {
        case "DIR":
            $akses = "";
            break;
        case "MGR":
            $akses = $akses . " AND (employee_Email='" . $_SESSION['strEmail'] . "' OR strMailSPV='" . $_SESSION['strEmail'] . "' OR strMailMGR='" . $_SESSION['strEmail'] . "')";
            break;
        case "SPV":
            $akses = $akses . " AND (employee_Email='" . $_SESSION['strEmail'] . "' OR strMailSPV='" . $_SESSION['strEmail'] . "')";
            break;
        case "STAFF":
            $akses = $akses . " AND employee_Email='" . $_SESSION['strEmail'] . "'";
            break;
    }
}

switch ($sts) {
    case "done":
        $where = " AND dtSubmitSPV > '1000-01-01' AND dtSubmitMGR > '1000-01-01' AND dtSubmitDIR > '1000-01-01' ";
        break;
    case "pending":
        $where = " AND (dtSubmitSPV = '1000-01-01' OR dtSubmitMGR = '1000-01-01' OR dtSubmitDIR = '1000-01-01')";
        break;
    case "all":
        $where = "";
        break;
}

switch ($_SESSION['strJenisAkses']) {
    case "DIR":
        $filter = "";
        break;
    case "MGR":
        $filter = $where . " AND (employee_Email='" . $_SESSION['strEmail'] . "' OR strMailSPV='" . $_SESSION['strEmail'] . "' OR strMailMGR='" . $_SESSION['strEmail'] . "')";
        break;
    case "SPV":
        $filter = $where . " AND (employee_Email='" . $_SESSION['strEmail'] . "' OR strMailSPV='" . $_SESSION['strEmail'] . "')";
        break;
    case "STAFF":
        $filter = $where . " AND employee_Email='" . $_SESSION['strEmail'] . "'";
        break;
}

if ($item <> "") {
    $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.item_ID='" . $item . "' and DET.stsAktif=1 AND 
            HEAD.stsAktif=1 AND 
            list_item.stsAktif=1 AND
            (DATE_FORMAT(HEAD.dtCreate, '%Y-%m-%d') BETWEEN '$mulai' AND '$hingga')" . $filter . $akses . " order by HEAD.ID;";
} else {
    $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 
    list_item.stsAktif=1 AND
    (DATE_FORMAT(HEAD.dtCreate, '%Y-%m-%d') BETWEEN '$mulai' AND '$hingga')" . $filter . $akses . " order by HEAD.ID;";
}


$query = "SELECT  HEAD.ID,             
            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 DET.stsAktif=1 AND 
            HEAD.stsAktif=1 AND 
            list_item.stsAktif=1 AND
            (DATE_FORMAT(HEAD.dtCreate, '%Y-%m-%d') BETWEEN '$mulai' AND '$hingga')" . $filter . $akses . ";";

$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>