<?php
    include 'session.php';
    include 'style.php';
    include 'navbar.php'; 
?> 


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Add icon library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
table {
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border: 1px solid #ddd;
}

th, td {
  text-align: left;
  padding: 5px;
  font-size: 12px;
}

tr:nth-child(even){background-color: #f2f2f2}

.pagination a {
  color: black;
  float: left;
  padding: 8px 16px;
  text-decoration: none;
  transition: background-color .3s;
  background-color: #e7f3fe;
}

.pagination a.active {
  background-color: dodgerblue;
  color: white;
}

.pagination a:hover:not(.active) {background-color: #ddd;}

input[type=text], select {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  font-size: medium;
}

.btn {
  background-color: DodgerBlue;
  border: none;
  color: white;
  padding: 6px 8px;
  font-size: 14px;
  cursor: pointer;
}

/* Darker background on mouse-over */
.btn:hover {
  background-color: RoyalBlue;
}

</style>
<?php
$status=0;
$nama='';
$isi='';
$jawaban='';
$petugas='';
$jabatan='';


$username = $_SESSION['username'];

    $query_sql = "SELECT * FROM jenis";
    if(isset($_GET["nama"])){
        if($_GET["nama"]!=''){
            //echo "masuk";
            // $_GET["nama"];
            $nama = $_GET["nama"];
            $query_sql = $query_sql." WHERE (jenis_nama LIKE '%$nama%') OR (produk_keterangan LIKE '%$nama%')";
            //echo $query_sql;
            $status=1;
            //$tampilMas = "SELECT * FROM comments LIMIT $mulai, $halaman";
        }
    }
    if(isset($_GET["isi"])){
        if($_GET["isi"]!=''){
            //echo "masuk";
            //echo $_GET["isi"];
            $isi = $_GET["isi"];
        
            if ($status==1) {
                $query_sql = $query_sql." AND nowa LIKE '%$isi%'";
            } else {
                $query_sql = $query_sql." WHERE nowa LIKE '%$isi%'";
            }
            //echo $query_sql;
            $status=1;
            //$tampilMas = "SELECT * FROM comments LIMIT $mulai, $halaman";
        }
    }

    if(isset($_GET["jawaban"])){
        if($_GET["jawaban"]!=''){
            //echo "masuk";
            //echo $_GET["jawaban"];
            $jawaban = $_GET["jawaban"];
            if ($status==1) {
                $query_sql = $query_sql." AND id LIKE '%$jawaban%'";
            } else {
                $query_sql = $query_sql." WHERE id LIKE '%$jawaban%'";
            }
            //echo $query_sql;
            $status=1;
            //$tampilMas = "SELECT * FROM comments LIMIT $mulai, $halaman";
        }
    }

    if(isset($_GET["petugas"])){
        if($_GET["petugas"]!=''){
            //echo "masuk";
            //echo $_GET["petugas"];
            $petugas = $_GET["petugas"];
            if ($status==1) {
                $query_sql = $query_sql." AND pelayanan LIKE '%$petugas%'";
            } else {
                $query_sql = $query_sql." WHERE pelayanan LIKE '%$petugas%'";
            }
            //echo $query_sql;
            $status=1;
            //$tampilMas = "SELECT * FROM comments LIMIT $mulai, $halaman";
        }
    }



?>
<head>
</head>
<body>

<h2>&nbsp;&nbsp;Daftar Master Jenis</h2>
<div style="overflow-x:auto;">
<table>
<form action="jenislist.php" method="GET">
<button id="myBtn"  hidden>Button</button>
<button id="myBtn1"  hidden>Button</button>
<button id="myBtn2"  hidden>Button</button>
<button id="myBtn3"  hidden>Button</button>
  <tr>

    <div class="topnav">
    <div class="search-container">
    <th colspan=3><input id="myInput" type="text" value="<?php echo $nama; ?>" placeholder="Search" name="nama"></th>
    </div>
    </div>
    <th></th>
    <th></th>
  </tr>
</form>
  <tr>
    <th>No</th>
    <th>Nama</th>
    <th>Keterangan</th>
  </tr>
  <?php
                //include "koneksi.php";
                $halaman = 20; /* page halaman*/
                $page =isset($_GET["halaman"]) ? (int)$_GET["halaman"] : 1;
                $mulai =($page>1) ? ($page * $halaman) - $halaman : 0;
                
                //$query_sql = "SELECT * FROM comments";
                $result = mysqli_query($conn, $query_sql);
                //$result =mysqli_query("SELECT * FROM comments");
                $total = mysqli_num_rows($result);
                $pages = ceil($total/$halaman);
                

                $tampilMas = $query_sql." LIMIT $mulai, $halaman";
                $result_tampilMas = mysqli_query($conn, $tampilMas);
                $no = $mulai+1;

                
                while($mas = mysqli_fetch_array($result_tampilMas)){
            ?>
  <tr>
            <td align="center"><?=$no++?></td>
            <td><?php echo $mas['jenis_nama']?></td>
            <td><?php echo $mas['jenis_keterangan']?></td>
            <?php /*
            <td>
                <form action="editable.php" method="POST">
                    <button name="edit" type="submit" value="<?php echo $mas['comments_id']; ?>" class="btn"><i class="fa fa-pencil"></i></button>
                </form>
            </td>
            */ ?>
  </tr>
    <?php  
        }
    ?>                
</table>
</div>
    <?php
        $halaman_sekarang = 1;
        $panjang_pagination = $pages;
        $start = 0;
        $end = $pages;
        if(isset($_GET["halaman"])){
            $halaman_sekarang = $_GET["halaman"];
        }
        $awal = 1;
        //$akhir = 1;
        if ($halaman_sekarang != 1) {
            $awal = $halaman_sekarang-1;
        }

        if ($halaman_sekarang != $pages) {
            $akhir = $halaman_sekarang+1;
        }

        if ($pages > 10) {
            $panjang_pagination = 10;
            if ($halaman_sekarang > 5) {
                //echo "masuk";
                $start = $halaman_sekarang - 5;
            }

            if ($halaman_sekarang > $pages-5) {
               $start = $pages-10;
               //echo $start;
            }
        }

        


    ?>
    <div class="pagination">
        <a href="jenislist.php?halaman=1&nama=<?php echo $nama;?>&isi=<?php echo $isi;?>&jawaban=<?php echo $jawaban;?>&petugas=<?php echo $petugas;?>">First</a>
        <a href="jenislist.php?halaman=<?php echo $awal; ?>&nama=<?php echo $nama;?>&isi=<?php echo $isi;?>&jawaban=<?php echo $jawaban;?>&petugas=<?php echo $petugas;?>">&laquo;</a>
        <?php
            for ($i=1; $i<=$panjang_pagination ; $i++){
                $start++;
                if ($start==$halaman_sekarang) {
        ?>
            <a class="active" href="jenislist.php?halaman=<?php echo $start; ?>&nama=<?php echo $nama;?>&isi=<?php echo $isi;?>&jawaban=<?php echo $jawaban;?>&petugas=<?php echo $petugas;?> style="text-decoration:none">   <?php echo $start; ?></a>
        <?php } else { ?>
            <a href="jenislist.php?halaman=<?php echo $start; ?>&nama=<?php echo $nama;?>&isi=<?php echo $isi;?>&jawaban=<?php echo $jawaban;?>&petugas=<?php echo $petugas;?>" style="text-decoration:none">   <?php echo $start; ?></a>
        <?php
            }   
        }
        ?>
        <a href="jenislist.php?halaman=<?php echo $akhir; ?>&nama=<?php echo $nama;?>&isi=<?php echo $isi;?>&jawaban=<?php echo $jawaban;?>&petugas=<?php echo $petugas;?>">&raquo;</a>
        <a href="jenislist.php?halaman=<?php echo $pages; ?>&nama=<?php echo $nama;?>&isi=<?php echo $isi;?>&jawaban=<?php echo $jawaban;?>&petugas=<?php echo $petugas;?>">Last</a>
    </div>
    
</body>
</html>

<script>
var input = document.getElementById("myInput");
    input.addEventListener("keypress", function(event) {
    if (event.key === "Enter") {
        event.preventDefault();
        document.getElementById("myBtn").click();
    }
});

var input = document.getElementById("myInput1");
    input.addEventListener("keypress", function(event) {
    if (event.key === "Enter") {
        event.preventDefault();
        document.getElementById("myBtn1").click();
    }
});

var input = document.getElementById("myInput2");
    input.addEventListener("keypress", function(event) {
    if (event.key === "Enter") {
        event.preventDefault();
        document.getElementById("myBtn2").click();
    }
});

var input = document.getElementById("myInput3");
    input.addEventListener("keypress", function(event) {
    if (event.key === "Enter") {
        event.preventDefault();
        document.getElementById("myBtn3").click();
    }
});
</script>
