<?php
if (isset($where) && $where <> "") {
    $query = "SELECT  HEAD.strMailDIR, 
            HEAD.strMailMGR, 
            HEAD.strMailSPV, 
            HEAD.strKdLPBJ,
            HEAD.strFullNameREQ,
            DATE_FORMAT(HEAD.dtCreate, '%d %b %Y') as dtCreate,
            HEAD.stsUrgent
            FROM tpc_h_lpbj AS HEAD 
            WHERE " . $where . " AND 
            HEAD.stsAktif = 1 
            GROUP BY HEAD.ID;"; //echo $query;
    $result = $MySQLi->query($query);
?>

    <main id="main" class="main">
        <div class="pagetitle">
            <h1>Selamat Datang <?= $_SESSION['strNama']; ?></h1>
            <nav>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                    <li class="breadcrumb-item active">Dashboard</li>
                </ol>
            </nav>
        </div>

        <section class="section dashboard">
            <div class="row">

                <div class="col-lg-12">
                    <div class="row">

                        <?php
                        while ($row = $result->fetch_array()) {
                            if ($_SESSION['usrLevel'] == 1)
                                $strHyperlink = "http://" . $strNmDomain . "/home/index2.php?menu=persetujuan-lpbj&session_data=" . $row['strMailDIR'] . "&lpbj=" . $row['strKdLPBJ'] . "&level=DIR";
                            elseif ($_SESSION['usrLevel'] == 2)
                                $strHyperlink = "http://" . $strNmDomain . "/home/index2.php?menu=persetujuan-lpbj&session_data=" . $row['strMailMGR'] . "&lpbj=" . $row['strKdLPBJ'] . "&level=MGR";
                            elseif ($_SESSION['usrLevel'] == 3)
                                $strHyperlink = "http://" . $strNmDomain . "/home/index2.php?menu=persetujuan-lpbj&session_data=" . $row['strMailSPV'] . "&lpbj=" . $row['strKdLPBJ'] . "&level=SPV";
                            else
                                $strHyperlink = "";
                            //echo "<a href='" . $strHyperlink . "'>" . $row['strKdLPBJ'] . "<br></a>";
                            echo "<div class='col-xxl-2 col-xl-4' onclick='apr(\"" . $strHyperlink . "\")' style='cursor:pointer;'>
                                    <div class='card info-card customers-card'>
                                        <div class='card-body'><br>                                            
                                            <div class='d-flex align-items-center'>
                                                <div class='ps-3'>
                                                    <h6>" . $row['strKdLPBJ'] . "</h6>
                                                    <span class='text-primary small pt-1 fw-bold'><i class='bi bi-person'></i>&nbsp;</span> <span class='text-muted small pt-2 ps-1'>" . $row['strFullNameREQ'] . "</span>
                                                    <br>";
                            if ($row['stsUrgent'] == 1) {
                                echo                    "<span class='badge rounded-pill bg-danger' style='float:right;'>Urgent</span>";
                            }


                            echo                   "<span class='text-primary small pt-1 fw-bold'><i class='bi bi-calendar-month'></i>&nbsp;</span> <span class='text-muted small pt-2 ps-1'>" . $row['dtCreate'] . "</span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>";
                        }
                        ?>

                    <?php
                }
                    ?>

                    </div>
                </div>

            </div>
        </section>

    </main>

    <script type="text/javascript">
        function apr(url) {
            let timerInterval;
            Swal.fire({
                title: "Loading...",
                timer: 1000,
                timerProgressBar: false,
                didOpen: () => {
                    Swal.showLoading();
                    const timer = Swal.getPopup().querySelector("b");
                    timerInterval = setInterval(() => {
                        timer.textContent = `${Swal.getTimerLeft()}`;
                    }, 100);
                },
                willClose: () => {
                    clearInterval(timerInterval);
                    window.open(url, "_self");
                }
            });
        }
    </script>