<?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{
  $aksi = "modul/mod_hubungi/aksi_hubungi.php";

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

  switch($act){
    // Tampil Hubungi Kami
    default:
      echo "<h2>Data Penjual</h2>  
          <div class=\"table-responsive\">
          <table class=\"table text-start align-middle table-bordered table-hover mb-0\">
          <thead>
          <tr class=\"text-white text-center\"><th>No</th><th>Nama</th><th>Email</th><th>Tgl Daftar</th><th>Aksi</th></tr>
          </thead>
            <tbody>";

      $query  = "SELECT * FROM user_pendaftar ORDER BY id DESC";
      $tampil = mysqli_query($konek, $query);
 
      $no = 1;
      while ($r=mysqli_fetch_array($tampil)){  
        $tanggal=tgl_indo($r['tanggal']);
        echo "<tr><td>$no</td>
        <td>$r[nama]</td>
                  <td>$r[email]</td>
                  
                  <td>$tanggal</a></td>
                  <td><a href=\"$aksi?module=hubungi&act=hapus&id=$r[id]\" class=\"btn btn-sm btn-primary\">Hapus</a> </td>
              </tr>";
        $no++;
      }
      echo "</tbody>
        </table><br> </div>";
    break;

case "editorder":
      $query = "SELECT * FROM hubungi WHERE id_hubungi='$_GET[id]'";
      $hasil = mysqli_query($konek, $query);
      $r     = mysqli_fetch_array($hasil);


      $tgl  = tgl_indo($r['tanggal']);

      echo"
       <form method=\"POST\" action=\"$aksi?module=hubungi&act=update\">
      <input type=\"hidden\" name=\"id\" value=\"$r[id_hubungi]\">
      <table class=\"table text-start align-middle table-bordered table-hover mb-0\">


       <tr class=\"text-white\"><td><b>Id Order</b></td>    <td>:   $r[orders]</td></tr>
       <tr class=\"text-white\"><td><b>Layanan</b></td>    <td>:   $r[layanan]</td></tr>
     
       
       <tr class=\"text-white\"><td><b>Person</b></td>    <td>:   $r[person] person</td></tr>
      
       <tr class=\"text-white\"><td><b>Nama</b></td>    <td>:   $r[nama_pengirim]</td></tr>
       
       <tr class=\"text-white\"><td><b>Email</b></td>    <td>:   $r[email]</td></tr>
       <tr class=\"text-white\"><td><b>Telphone</b></td>    <td>:   $r[phone]</td></tr>
        
       <tr class=\"text-white\"><td><b>Tanggal Booking </b></td>    <td>:   $r[berangkat]</td></tr>
       <tr class=\"text-white\"><td><b>Tanggal Pemesanan</b></td>    <td>:   $tgl</td></tr>


    

      ";

     
      echo "
      


          </table>
          </form>";
    break;




    case "balasemail":
      $query = "SELECT * FROM hubungi WHERE id_hubungi='$_GET[id]'";
      $hasil = mysqli_query($konek, $query);
      $r     = mysqli_fetch_array($hasil);
      
      echo "<h2>Balas Email</h2>
          <form method=\"POST\" action=\"?module=hubungi&act=kirimemail\">
          <table class=\"table text-start align-middle table-bordered table-hover mb-0\">
          <tr class=\"text-white\"><td>Kepada</td><td> <input type=\"text\" name=\"email\" value=\"$r[email]\"></td></tr>
          <tr class=\"text-white\"><td>Subjek</td><td> <input type=\"text\" name=\"subjek\" value=\"Re: $r[subjek]\"></td></tr>
          <tr class=\"text-white\"><td>Pesan</td> <td>   <textarea name=\"pesan\" id=\"lokomedia\" style=\"width: 800px; height: 180px;\"><br><br>    
          -----------------------------------------------------------------------------------------------------------------------------------------------<br>
          $r[pesan]</textarea></td></tr>
          <tr ><td colspan=\"2\"><input type=\"submit\" value=\"Kirim\" 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>";
    break;
    
    case "kirimemail":
      $query = "SELECT nama_pemilik,email FROM identitas LIMIT 1";
      $hasil = mysqli_query($konek, $query);
      $r     = mysqli_fetch_array($hasil);
      
      $kepada = $_POST['email']; 
      $subjek = $_POST['subjek'];
      $pesan  = $_POST['pesan'];
       
      $dari  = "from: $r[nama_pemilik] <$r[email]> \r\n";
      $dari .= "Content-type: text/html \r\n"; // isi email support html

      mail($email,$subjek,$pesan,$dari);
      
      echo "<h2>Status Email</h2>
            <p>Email telah terkirim.</p>";	 		  
    break;  
  }
}
?>
