
<?php    
// Apabila user belum login
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
  echo "<link href=\"../../css/style_login.css\" rel=\"stylesheet\" type=\"text/css\" />
        <div id=\"login\"><h1 class=\"fail\">Untuk mengakses modul, Anda harus login dulu.</h1>
        <p class=\"fail\"><a href=\"../../index.php\">LOGIN</a></p></div>";  
}
// Apabila user sudah login dengan benar, maka terbentuklah session
else{
  // fungsi untuk check box Tag (Berita Terkait) di form input dan edit berita 
  function GetCheckBox($table, $key, $Label, $Nilai='') {
    include "../config/koneksi.php";
    $s = "SELECT * FROM $table ORDER BY $Label";
    $u = mysqli_query($konek, $s);
    $_arrNilai = explode(',', $Nilai);
    $str = '';
    while ($t = mysqli_fetch_array($u)) {
      $_ck = (array_search($t[$key], $_arrNilai) === false)? '' : 'checked';
      $str .= "<input type=\"checkbox\" name='".$key."[]' value=\"$t[$key]\" $_ck>$t[$Label] ";
    }
    return $str;
  }

  $aksi = "modul/mod_berita/aksi_berita.php";

  // mengatasi variabel yang belum di definisikan (notice undefined index)
  $act = isset($_GET['act']) ? $_GET['act'] : ''; 

  switch($act){
    // Tampil Berita
    default:
      echo "<h2>Produk</h2>
          <p><input type=\"button\" value=\"Tambah Produk\" onclick=window.location.href=\"?module=berita&act=tambahberita\" class=\"btn btn-sm btn-primary\"></p>
          <div class=\"bg-secondary rounded h-100 p-4\">
<div class=\"table-responsive\">
          <table class=\"table\">
          <thead>
          <tr class=\"text-white\"><th>No</th><th>Gambar</th><th>Produk</th><th>Aksi</th></tr>
          </thead>
            <tbody>"; 
      
      if ($_SESSION['leveluser']=='admin'){
        $query  = "SELECT * FROM berita ORDER BY id_berita DESC";
        $tampil = mysqli_query($konek, $query);
      }
      else{
        $query  = "SELECT * FROM berita WHERE username='$_SESSION[namauser]' ORDER BY id_berita DESC";
        $tampil = mysqli_query($konek, $query);
      }
  
      $no = 1;
      while($r=mysqli_fetch_array($tampil)){
        $tgl_posting=tgl_indo($r['tanggal']);
        echo "<tr style=\"border-color:#6C7293\"><td>$no</td>
        <td><img src=\"../foto_berita/$r[gambar]\" width=\"200\" ></td>
                  <td width=\"350\">$r[judul]</td>
               
                  
		              <td><a href=\"?module=berita&act=editberita&id=$r[id_berita]\" class=\"btn btn-sm btn-primary\">Edit</a> | <a href=\"$aksi?module=berita&act=hapus&id=$r[id_berita]\" class=\"btn btn-sm btn-primary\">Hapus</a></td>
		          </tr>";
        $no++;
      }
      echo "</tbody>
        </table>
        </div></div>";
    break;    
  
    case "tambahberita":
      echo "<h2>Tambah Produk</h2>
          <form method=\"POST\" enctype=\"multipart/form-data\" action=\"$aksi?module=berita&act=input\">
          <table class=\"table text-start align-middle table-bordered table-hover mb-0\">
          <tr class=\"text-white\"><td>Judul   </td><td> <input type=\"text\" name=\"judul\" class=\"form-control\" style=\"background-color: #fff;\"></td></tr>

        
     
           <tr class=\"text-white\"><td>Keterangan</td><td>   <textarea name=\"isi_berita\" id=\"lokomedia\" style=\"width: 800px; height: 250px;\"></textarea></td></tr>
           <tr><td>Gambar    </td><td> <input type=\"file\" name=\"foto\" size=\"50\"> 
                                         <br> - Tipe gambar harus JPG (disarankan lebar gambar 350 px).</td></tr>






            <tr><td colspan=\"2\"><input type=\"submit\" value=\"Simpan\" class=\"btn btn-sm btn-primary\">
                                  <input type=\"button\" value=\"Batal\" onclick=\"self.history.back()\" class=\"btn btn-sm btn-primary\"></td></tr>
            </table>
            </form><br>";
    break;
       
    case "editberita":
      if ($_SESSION['leveluser']=='admin'){
        $query = "SELECT * FROM berita WHERE id_berita='$_GET[id]'";
        $hasil = mysqli_query($konek, $query);
      }
      else{
        $query = "SELECT * FROM berita WHERE id_berita='$_GET[id]' AND username='$_SESSION[namauser]'";
        $hasil = mysqli_query($konek, $query);
      }

      $r = mysqli_fetch_array($hasil);

      echo "<h2>Edit Produk</h2>
          <form method=\"POST\" enctype=\"multipart/form-data\" action=\"$aksi?module=berita&act=update\">
          <input type=\"hidden\" name=\"id\" value=\"$r[id_berita]\">
          <input type=\"hidden\" name=\"fotoku\" value=\"$r[gambar]\">
          <table class=\"table text-start align-middle table-bordered table-hover mb-0\">
          <tr class=\"text-white\"><td>Judul   </td><td> <input type=\"text\" name=\"judul\" value=\"$r[judul]\" class=\"form-control\" style=\"background-color: #fff;\"></td></tr>


          ";

      echo "<tr class=\"text-white\"><td>Keterangan</td><td> 
            <textarea id=\"lokomedia\" name=\"isi_berita\" style=\"width: 800px; height: 350px;\">$r[isi_berita]</textarea></td></tr>
            
            <tr class=\"text-white\"><td>Gambar</td><td> ";
            if ($r['gambar']!=''){
              echo "<img src=\"../foto_berita/$r[gambar]\" width=\"200\">";  
            }
            else{
              echo "Produk tidak ada gambarnya";
            }
      echo "</td></tr>
            <tr class=\"text-white\"><td>Ganti Gambar</td><td> <input type=\"file\" name=\"foto\" size=\"50\"><br>
                    - Apabila gambar tidak diganti, dikosongkan saja.</td></tr>";
      
    

      echo "
            <tr><td colspan=\"2\"><input type=\"submit\" value=\"Update\" class=\"btn btn-sm btn-primary\">
                                  <input type=\"button\" value=\"Batal\" onclick=\"self.history.back()\" class=\"btn btn-sm btn-primary\"></td></tr>
            </table>
            </form><br>";
    break;  
  }
}
?>
