<style>
    /* Modal styling */
    .custom-modal {
        display: none;
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgb(0, 0, 0);
        background-color: rgba(0, 0, 0, 0.4);
    }

    .custom-modal-content {
        margin: 15% auto;
        padding: 20px;
        border: 1px solid #888;
        width: 80%;
        background-color: #fff;
        text-align: center;
    }

    .custom-modal-content img {
        max-width: 100%;
        height: auto;
    }

    .close-btn {
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
    }

    .close-btn:hover,
    .close-btn:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
    }
</style>
<!-- ============================================================== -->
<!-- wrapper  -->
<!-- ============================================================== -->
<div class="dashboard-wrapper">
    <div class="container-fluid  dashboard-content">
        <!-- ============================================================== -->
        <!-- pageheader -->
        <!-- ============================================================== -->
        <div class="row">
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                <div class="page-header">
                    <h2 class="pageheader-title">Persetujuan Tambah LPBJ</h2>
                    <div class="page-breadcrumb">
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="index.php" class="breadcrumb-link"><?php echo namahari_ID(date('Y-m-d')) . ", " . date('d-m-Y'); ?>, Dashboard</a></li>
                                <li class="breadcrumb-item">Pembelian</a></li>
                                <li class="breadcrumb-item"><a href="?menu=daftar-lpbj" class="breadcrumb-link">LPBJ</a></li>
                                <li class="breadcrumb-item active" aria-current="page"><a href="?menu=tambah-lpbj" class="breadcrumb-link">Persetujuan Tambah LPBJ</a></li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
        <!-- ============================================================== -->
        <!-- end pageheader -->
        <!-- ============================================================== -->

        <?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'];
        ?>
        <div class="row">
            <form id="basicform" 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 ?";
                ?>
                <!-- ============================================================== -->
                <!-- basic form -->
                <!-- ============================================================== -->
                <div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12">
                    <div class="card">
                        <h5 class="card-header">Form Pesanan</h5>
                        <div class="card-body">
                            <div class="form-group">
                                <label for="inputLPBJ">No LPBJ : </label>
                                <input id="inputLPBJ" type="text" name="strKdLPBJ" data-parsley-trigger="change" readonly class="form-control" value="<?php echo $row['strKdLPBJ']; ?>">
                            </div>
                            <div class="form-group">
                                <label for="inputTanggal">Tanggal</label>
                                <input id="inputTanggal" type="text" name="dtCreate" readonly data-parsley-trigger="change" class="form-control" value="<?php echo date("d-m-Y"); ?>">
                            </div>
                            <div class="form-group">
                                <label for="inputDivisi">Divisi</label>
                                <input id="inputDivisi" type="text" name="Divisi" readonly data-parsley-trigger="change" class="form-control" value="<?php echo $row['strNmDivisiRequestor']; ?>">
                            </div>
                        </div>
                    </div>
                </div>
                <!-- ============================================================== -->
                <!-- end basic form -->
                <!-- ============================================================== -->

                <!-- ============================================================== -->
                <!-- data table  -->
                <!-- ============================================================== -->
                <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="mb-0">Data List LPBJ</h5>
                        </div>
                        <input type="hidden" name="iLevelREQ" value="<?php echo $iLevelREQ; ?>">
                        <div class="card-body">
                            <div class="table-responsive">
                                <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>
                                    <tfoot>
                                        <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>
                                    </tfoot>
                                </table>
                                <br><br>
                                <?php if (isset($_GET['edit']) && $_GET['edit'] == 1 && $_SESSION['strJobPosition'] == "Accurate") {
                                    echo "<div class='form-group row'>
                                                    <label class='col-12 col-sm-3 col-form-label text-sm-right'>Done ?</label>
                                                    <div class='col-12 col-sm-8 col-lg-6 pt-1'>
                                                        <div class='switch-button switch-button-success'>
                                                            <input type='checkbox' name='stsDone[]' checked value='1' id='inputDone'><span>
                                                                <label for='inputDone'></label></span>
                                                        </div>
                                                    </div>
                                                </div>";
                                } else {
                                ?>
                                    <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
                                }
                                ?>
                            </div>
                        </div>
                    </div>

                    <div id="customModal" class="custom-modal">
                        <div class="custom-modal-content">
                            <span class="close-btn">&times;</span>
                            <img id="modalImage" src="" alt="Gambar Ukuran Penuh">
                        </div>
                    </div>

                    <div class="col-sm-6 pl-0">
                        <p class="text-left">
                            <?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>";
                                }
                            }
                            /*
                                    if($_GET['level']=="SPV" && $stsACCSPV_buffer == $iTotal) 
                                        $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>"; 
                                    elseif($_GET['level']=="MGR" && $stsACCMGR_buffer == $iTotal && $stsACCSPV_buffer < $iTotal)
                                        $button = "<button type='submit' name='submit' class='btn btn-space btn-primary'>Submit</button>";
                                    elseif($_GET['level']=="DIR" && $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="";
                                    */
                            echo $button;
                            ?>
                        </p>
                    </div>
                </div>
                <!-- ============================================================== -->
                <!-- end data table  -->
                <!-- ============================================================== -->
            </form>
        </div>
    </div>
    <!-- ============================================================== -->
    <!-- footer -->
    <!-- ============================================================== -->
    <div class="footer">
        <div class="container-fluid">
            <div class="row">
                <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
                    Copyright © 2024 TPC-PACK, Developed By <a href="mailto: jockie@netportal.my.id">JC Comp</a>.
                </div>
                <!--<div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
                            <div class="text-md-right footer-links d-none d-sm-block">
                                <a href="javascript: void(0);">About</a>
                                <a href="javascript: void(0);">Support</a>
                                <a href="javascript: void(0);">Contact Us</a>
                            </div>
                        </div>-->
            </div>
        </div>
    </div>
    <!-- ============================================================== -->
    <!-- end footer -->
    <!-- ============================================================== -->
</div>
<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>