<?php
$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
$query = "SELECT * 
                        FROM tpc_h_lpbj 
                        WHERE strKdLPBJ='" . $_GET['lpbj'] . "' AND 
                                stsAktif=1
                        ";
$result = $MySQLi->query($query);
if ($result->num_rows == 0)
    echo "ERROR : DATA NOT FOUND";
else $row = $result->fetch_array();
$strFullNameDIR = $row['strFullNameDIR'];
$strFullNameMGR = $row['strFullNameMGR'];
$strFullNameSPV = $row['strFullNameSPV'];
$strFullNameREQ = $row['strFullNameREQ'];
$strMailDIR = $row['strMailDIR'];
$strMailMGR = $row['strMailMGR'];
$strMailSPV = $row['strMailSPV'];
$strMailREQ = $row['employee_Email'];
$_SESSION['strEmailREQ'] = $row['employee_Email'];
$_SESSION['strKdLPBJ'] = $row['strKdLPBJ'];
$iLevelREQ = $row['iLevelREQ'];
?>


<main id="main" class="main">

    <div class="pagetitle">
        <h1>Persetujuan Tambah LPBJ</h1>
        <nav>
            <ol class="breadcrumb">

            </ol>
        </nav>
    </div><!-- End Page Title -->
    <form id="basicform" class="row g-3" data-parsley-validate="" action="transaksi/persediaan/pemesanan/simpan.persetujuan.pesan.barang.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="h_lpbj_ID" value="<?php echo $row['ID']; ?>">
        <?php
        if (isset($_GET['level'])) {
            echo "<input type='hidden' name='level' value='" . $_GET['level'] . "'>";
            $option = "Option";
        } else $option = "Reject ?";
        ?>
        <section class="section">
            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">
                            <br>
                            <div class="row g-3">
                                <div class="col-md-4">
                                    <label class="form-label">No LPBJ</label>
                                    <input type="text" class="form-control" name="strKdLPBJ" placeholder="No LPBJ" value="<?php echo $row['strKdLPBJ']; ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">Tanggal</label>
                                    <input type="text" class="form-control" name="dtCreate" placeholder="Tanggal" value="<?php echo date("d-m-Y"); ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">Divisi</label>
                                    <input type="text" class="form-control" name="Divisi" placeholder="Divisi" value="<?php echo $row['strNmDivisiRequestor']; ?>" readonly>
                                </div>
                            </div><!-- End No Labels Form -->
                        </div>
                    </div>
                </div>

                <input type="hidden" name="iLevelREQ" value="<?php echo $iLevelREQ; ?>">

                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">
                            <br>
                            <table id="example" class="table table-striped table-bordered" style="width:100%">
                                <thead>
                                    <tr>
                                        <th>Kode</th>
                                        <th>Deskripsi</th>
                                        <th>Jumlah</th>
                                        <th>Satuan</th>
                                        <th>Jumlah Stok</th>
                                        <th>Keterangan</th>
                                        <th>Tanggal dibutuhkan</th>
                                        <th>Departement</th>
                                        <th>Gambar</th>
                                        <th><?php echo $option; ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $i = 0;
                                    $where = "D_LPBJ.stsACCSPV > 0 AND D_LPBJ.stsACCMGR > 0 AND D_LPBJ.stsACCDIR > 0";

                                    $stsACCSPV_buffer = 0;
                                    $stsACCMGR_buffer = 0;
                                    $stsACCDIR_buffer = 0;
                                    $stsACC_Accurate_buffer = 0;
                                    $query = "SELECT  ITEM.strKdItem,
                                                                ITEM.strDeskripsi, 
                                                                H_LPBJ.employee_Email AS strMailREQ, 
                                                                H_LPBJ.dtSubmitDIR, 
                                                                H_LPBJ.dtSubmitMGR, 
                                                                H_LPBJ.dtSubmitSPV, 
                                                                H_LPBJ.dtSubmitAccurate, 
                                                                H_LPBJ.dtCreate AS dtSubmitREQ, 
                                                                D_LPBJ.ID, 
                                                                D_LPBJ.item_strUnit AS strUnit, 
                                                                D_LPBJ.iJumlah, 
                                                                D_LPBJ.iJumlahStok, 
                                                                D_LPBJ.strNmFile, 
                                                                D_LPBJ.strKeteranganREQ, 
                                                                D_LPBJ.strKeteranganDIR, 
                                                                D_LPBJ.strKeteranganMGR, 
                                                                D_LPBJ.strKeteranganSPV, 
                                                                D_LPBJ.dtDibutuhkan, 
                                                                D_LPBJ.stsACCSPV, 
                                                                D_LPBJ.stsACCMGR, 
                                                                D_LPBJ.stsACCDIR, 
                                                                D_LPBJ.stsACC_Accurate, 
                                                                DEPT.strNmDept 
                                                        FROM tpc_d_lpbj AS D_LPBJ 
                                                        LEFT JOIN tpc_h_lpbj AS H_LPBJ 
                                                            ON D_LPBJ.h_lpbj_ID = H_LPBJ.ID 
                                                        LEFT JOIN item AS ITEM 
                                                            ON D_LPBJ.item_ID=ITEM.ID 
                                                        LEFT JOIN tpc_dept AS DEPT 
                                                            ON D_LPBJ.dept_ID=DEPT.ID
                                                        WHERE D_LPBJ.stsAktif=1 AND 
                                                                H_LPBJ.stsAktif=1 AND 
                                                                H_LPBJ.strKdLPBJ='" . $_GET['lpbj'] . "' AND 
                                                                " . $where . " 
                                                        ORDER BY D_LPBJ.ID ASC 
                                                        ";
                                    //die($query);
                                    $result = $MySQLi->query($query);
                                    $dtSubmitREQ = "";
                                    $dtSubmitSPV = "";
                                    $dtSubmitMGR = "";
                                    $dtSubmitDIR = "";
                                    $dtSubmitAccurate = "";
                                    while ($row = $result->fetch_array()) {
                                        $i++;
                                        $stsREJECT_Checked = "";
                                        $stsACC_Checked = "";
                                        $stsACCSPV_buffer = $stsACCSPV_buffer + $row['stsACCSPV'];
                                        $stsACCMGR_buffer = $stsACCMGR_buffer + $row['stsACCMGR'];
                                        $stsACCDIR_buffer = $stsACCDIR_buffer + $row['stsACCDIR'];
                                        $stsACC_Accurate_buffer = $stsACC_Accurate_buffer + $row['stsACC_Accurate'];

                                        $dtSubmitREQ = $row['dtSubmitREQ'];
                                        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")
                                            $dtSubmitAccurate = "pending";
                                        else $dtSubmitAccurate = $row['dtSubmitAccurate'];

                                        if ($row['stsACC_Accurate'] < 2) {
                                            if ($row['stsACC_Accurate'] == 1)
                                                $stsACC_Checked = "";
                                            else
                                                $stsACC_Checked = "checked";
                                        } elseif ($row['stsACC_Accurate'] == 2 && $row['stsACCDIR'] < 2) {
                                            if ($row['stsACCDIR'] == 1) {
                                                $stsACC_Checked = "checked";
                                                $stsREJECT_Checked = "";
                                            } else {
                                                $stsACC_Checked = "";
                                                $stsREJECT_Checked = "checked";
                                            }
                                        } elseif ($row['stsACC_Accurate'] == 2 && $row['stsACCDIR'] == 2 && $row['stsACCMGR'] < 2) {
                                            if ($row['stsACCMGR'] == 1) {
                                                $stsACC_Checked = "checked";
                                                $stsREJECT_Checked = "";
                                            } else {
                                                $stsACC_Checked = "";
                                                $stsREJECT_Checked = "checked";
                                            }
                                        } elseif ($row['stsACC_Accurate'] == 2 && $row['stsACCDIR'] == 2 && $row['stsACCMGR'] == 2 && $row['stsACCSPV'] < 2) {
                                            if ($row['stsACCSPV'] == 1) {
                                                $stsACC_Checked = "checked";
                                                $stsREJECT_Checked = "";
                                            } else {
                                                $stsACC_Checked = "";
                                                $stsREJECT_Checked = "checked";
                                            }
                                        } else {
                                            $stsACC_Checked = "checked";
                                            $stsREJECT_Checked = "";
                                        }

                                        if ($stsACC_Checked == $stsREJECT_Checked && $stsACC_Checked == "" && !isset($_GET['edit'])) {
                                            $stsACC_Checked = "checked";
                                            $stsREJECT_Checked = "";
                                        }
                                        if (isset($_GET['edit']) && $_GET['edit'] == 1 && $_SESSION['strJobPosition'] == "Accurate") {
                                            $stsACC = "<div class='form-group row'>
                                                                    <div class='col-12 col-sm-8 col-lg-6 pt-1'>
                                                                        <div class='switch-button switch-button-danger'>
                                                                            <input type='checkbox' " . $stsACC_Checked . " value='1' name='stsACC[" . $row['ID'] . "]' id='inputACC" . $row['ID'] . "'>
                                                                            <span><label for='inputACC" . $row['ID'] . "'></label></span>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                                ";
                                        } else {
                                            $stsACC = "
                                                                <label class='custom-control custom-radio custom-control-inline'>
                                                                    <input type='radio' name='stsACC" . $row['ID'] . "' class='custom-control-input' value='1' " . $stsACC_Checked . "><span class='custom-control-label'>Terima</span>
                                                                </label>
                                                                <label class='custom-control custom-radio custom-control-inline'>
                                                                    <input type='radio' name='stsACC" . $row['ID'] . "' class='custom-control-input' value='0' " . $stsREJECT_Checked . "><span class='custom-control-label'>Tolak</span>
                                                                </label>
                                                                ";
                                        }
                                        $img = "";
                                        if (isset($row['strNmFile']) && $row['strNmFile'] <> "")
                                            $img = "<img src='assets/images/lpbj_pic/thumb_{$row['strNmFile']}' width='50px' height='50px' class='thumbnail' data-image='assets/images/lpbj_pic/{$row['strNmFile']}'>";
                                        echo "<tr>
                                                            <td>" . $row['strKdItem'] . "</td>
                                                            <td>" . $row['strDeskripsi'] . "</td>
                                                            <td>" . $row['iJumlah'] . "</td>
                                                            <td>" . $row['strUnit'] . "</td>
                                                            <td>" . $row['iJumlahStok'] . "</td>
                                                            <td>
                                                                <label for='exampleFormControlTextarea1'>" . $row['strKeteranganREQ'] . "<br>" . $row['strKeteranganSPV'] . "<br>" . $row['strKeteranganMGR'] . "<br>" . $row['strKeteranganDIR'] . "</label>
                                                                <textarea class='form-control' name='strKeterangan" . $row['ID'] . "' id='exampleFormControlTextarea1' rows='3'></textarea>
                                                            </td>
                                                            <td>" . $row['dtDibutuhkan'] . "</td>
                                                            <td>" . $row['strNmDept'] . "</td>
                                                            <td>" . $img . "</td>
                                                            <td>" . $stsACC . "</td>
                                                        </tr>";
                                    }
                                    $iTotal = $i * 2;
                                    ?>
                                </tbody>
                            </table>
                            <table id="example" class="table table-striped table-bordered" style="width:100%">
                                <thead>
                                    <tr>
                                        <th>Mengetahui</th>
                                        <th>Menyetujui</th>
                                        <th>Memeriksa</th>
                                        <th>Diajukan oleh</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    <tr>
                                        <td>
                                            <?php
                                            if ($dtSubmitDIR <> "pending")
                                                echo date_ID($dtSubmitDIR) . "<br><br><img width='25%' height='25%' src='assets/images/Approved.png'>";
                                            else echo "&nbsp;";
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            //if($stsACCMGR_buffer<$iTotal)
                                            if ($dtSubmitMGR <> "pending")
                                                echo date_ID($dtSubmitMGR) . "<br><br><img width='25%' height='25%' src='assets/images/Approved.png'>";
                                            else echo "&nbsp;";
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            if ($dtSubmitSPV <> "pending")
                                                echo date_ID($dtSubmitSPV) . "<br><br><img width='25%' height='25%' src='assets/images/Approved.png'>";
                                            else echo "&nbsp;";
                                            ?>
                                        </td>
                                        <td><?php echo date_ID($dtSubmitREQ) . "<br><br>Requested"; ?></td>
                                    </tr>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th><?php echo $strFullNameDIR; ?></th>
                                        <th><?php echo $strFullNameMGR; ?></th>
                                        <th><?php echo $strFullNameSPV; ?></th>
                                        <th><?php echo $strFullNameREQ; ?></th>
                                    </tr>
                                </tfoot>
                            </table>
                            <?php
                            //echo $iTotal."<br>".$stsACCDIR_buffer."<br>".$stsACCMGR_buffer."<br>".$stsACCSPV_buffer;
                            $button = "";
                            if (isset($_GET['level'])) {
                                switch ($_GET['level']) {
                                    case "SPV":
                                        if ($stsACCSPV_buffer == $iTotal)
                                            $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>";
                                        else $button = "";
                                        break;
                                    case "MGR":
                                        if ($strMailMGR == $strMailSPV && $stsACCSPV_buffer == $iTotal)
                                            $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>";
                                        elseif ($strMailMGR <> $strMailSPV && $stsACCMGR_buffer == $iTotal && $stsACCSPV_buffer < $iTotal)
                                            $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>";
                                        else $button = "";
                                        break;
                                    case "DIR":
                                        if ($strMailDIR == $strMailSPV && $stsACCSPV_buffer == $iTotal)
                                            $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>";
                                        elseif ($strMailDIR == $strMailMGR && $stsACCMGR_buffer == $iTotal && $stsACCSPV_buffer < $iTotal)
                                            $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>";
                                        elseif ($strMailDIR <> $strMailSPV && $strMailDIR <> $strMailMGR && $stsACCDIR_buffer == $iTotal && $stsACCMGR_buffer <  $iTotal && $stsACCSPV_buffer < $iTotal)
                                            $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>";
                                        else $button = "";
                                        break;
                                }
                            } else {
                                if (isset($_GET['edit']) && $_GET['edit'] == 1) {
                                    $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>";
                                }
                            }
                            echo $button;
                            ?>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </form>
</main>

<script src="assets/libs/js/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        var modal = $('#customModal');
        var modalImage = $('#modalImage');

        $('.thumbnail').on('click', function() {
            var imageUrl = $(this).data('image');
            modalImage.attr('src', imageUrl);
            modal.show();
        });

        $('.close-btn').on('click', function() {
            modal.hide();
        });

        $(window).on('click', function(event) {
            if ($(event.target).is(modal)) {
                modal.hide();
            }
        });
    });
</script>