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

    $status_masakan_mey = 0;
    $status_flabby = 0;

    // Data nama/alamat kadang kesimpen ke DB dalam kondisi ter-urlencode
    // (bahkan dobel-encode), jadi pas ditampilkan mentah keluar %252B dkk.
    // Fungsi ini decode berulang sampai stabil, aman dipakai walau
    // datanya cuma ke-encode sekali atau malah tidak ke-encode sama sekali.
    function safe_urldecode($str) {
        $decoded = $str;
        for ($i = 0; $i < 5; $i++) {
            $next = urldecode($decoded);
            if ($next === $decoded) break;
            $decoded = $next;
        }
        return $decoded;
    }

    if(isset($_POST["order_no"])){

        $order_no = $_POST["order_no"];
        $ongkir = $_POST["ongkir"];

        $query_sql = "SELECT * FROM produk_temp 
                        LEFT JOIN order_data ON order_data.order_no = produk_temp.temp_master
                        LEFT JOIN produk ON produk.produk_id = produk_temp.temp_produk
                        LEFT JOIN jenis ON produk.produk_jenis = jenis.jenis_id
        WHERE order_no = '$order_no'";
    
        $result = mysqli_query($conn, $query_sql);
    
        while ($row = mysqli_fetch_array($result))
        {
            $order_no = $row['order_no'];
            $nama = safe_urldecode($row['order_nama']);
            $alamat = safe_urldecode($row['order_alamat']);
            $nowa = $row['order_nowa'];
            $order_total = $row['order_total'];
            $diskpersen = $row['order_disk_persen'];
            $diskrp = $row['order_disk_rupiah'];
            if ($row['jenis_nama']=='Masakan Mey') {
              $status_masakan_mey = 1;
            }

            if ($row['jenis_nama']=='Flabby Patty') {
              $status_flabby = 1;
            }
        }
    }
    
?>


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
    font-family:'Segoe UI', Roboto, Oxygen, Ubuntu,   Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
}

.coupon {
  border: 5px dotted #bbb;
  width: 90%;
  border-radius: 5px;
  margin: 0 auto;
  max-width: 600px;
}

.container {
  padding: 2px 16px;
  background-color: #f1f1f1;
}

.promo {
  background: #ccc;
  padding: 3px;
}

.expire {
  color: red;
}

/* zebra table */
table {
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 12px;
}

th, td {
  text-align: left;
  padding: 4px;
}


/* label */
.label {
  color: white;
  padding: 5px; 
  font-family: Arial;
  font-size: small;
  line-height:1.75;
}
.success {background-color: #04AA6D;} /* Green */
.info {background-color: #2196F3;} /* Blue */
.warning {background-color: #ff9800;} /* Orange */
.danger {background-color: #f44336;} /* Red */ 
.other {background-color: #e7e7e7; color: black;} /* Gray */ 

/*button */
.btn {
  border: none;
  color: white;
  padding: 14px 100px;
  font-size: 16px;
  cursor: pointer;
  width:100%;
}

.btnflight {
  border: none;
  color: white;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
  width:40%;
}

.success {background-color: #04AA6D;} /* Green */
.success:hover {background-color: #46a049;}

* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 10px;
}

#myTable th, #myTable td {
  text-align: left;
  padding: 8px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header, #myTable tr:hover {
  background-color: #f1f1f1;
}

.watermark {
    position: absolute;
    color: lightgray;
    opacity: 0.25;
    font-size: 3em;
    width: 100%;
    top: 50%; 
 
    right: 30%;   
    text-align: center;
    z-index: 0;
}
</style>
</head>

<body>
<br>
<div class="coupon">
  <div class="container">
    <?php if ($status_masakan_mey==1) { ?>
      <img src="masakanmey_logo.PNG" alt="Avatar" style="width:40%; float:right;">
      <h3>Masakan Mey Receipt</h3>
    <?php } else if ($status_flabby==1) { ?>
      <img src="flabbypatty.jpeg" alt="Avatar" style="width:25%; float:right;">
      <h3>Flabby Patty Receipt</h3>
    <?php } else { ?>
      <img src="header.PNG" alt="Avatar" style="width:40%; float:right;">
      <h3>IniSpikoe Receipt</h3>
    <?php } ?>
     
  </div>
  
    <div class="container" style="background-color:white">
        <table>
        <tr>
            <td>No. Nota</td>
            <td><b><?php echo $order_no; ?></td>
        </tr>
        <tr>
            <td>Nama</td>
            <td><b><?php echo $nama; ?></td>
        </tr>
        <tr>
            <td>Alamat</td>
            <td><b><?php echo $alamat; ?></td>
        </tr>
        <tr>
            <td>No WA</td>
            <td><b><?php echo $nowa; ?></td>
        </tr>
        </table>
<hr>
        <table id="myTable">
        <div class="watermark">
          <?php if ($status_masakan_mey==1) { ?>
            <img src="masakanmey_logo.PNG" alt="Avatar" style="width:40%; float:right;">
          <?php } else if ($status_flabby==1) { ?>
            <img src="flabbypatty.jpeg" alt="Avatar" style="width:40%; float:right;">
          <?php } else { ?>
            <img src="header.PNG" alt="Avatar" style="width:40%; float:right;">
          <?php } ?>  
        </div>
            <tr class="header">
                <th style="width:40%;">Barang</th>
                <th style="width:10%;">Jumlah</th>
                <th style="width:30%;">Harga</th>
                <th style="width:20%;">Subtotal</th>
            </tr>

            <?php
                  $query_sql = "SELECT
                                	DATE_FORMAT(order_datetime, '%H:%i') AS jam_pesan,
                                  produk_temp.*,
                                  order_data.*,
                                  produk.*
                                FROM produk_temp 
                                  LEFT JOIN order_data ON order_no = temp_master
                                  LEFT JOIN produk ON produk_id = temp_produk
                                  WHERE order_no = $order_no";
                  $result = mysqli_query($conn, $query_sql);
                  while($mas = mysqli_fetch_array($result)){
                  ?>
                      <tr>

                        <td><?php echo $mas['produk_nama']?></td>
                        <td><?php echo $mas['temp_jumlah']?></td>
                        <td><?php echo number_format($mas['temp_harga'])?></td>
                        <td><?php echo number_format($mas['temp_harga']*$mas['temp_jumlah'])?></td>
                      </tr>
                  <?php
                  
                  }
            ?>
            <tr bgcolor="#ffffff">
                      <td colspan=2><font size=1><b></td>
                      <td colspan=1><font size=1><b>SubTotal</td>
                      <td colspan=1><font size=1><b><?php echo number_format($order_total)?></td>
            </tr>
            <tr bgcolor="#ffffff">
                      <td colspan=2><font size=1><b></td>
                      <td colspan=1><font size=1>Disk (%) - <?php echo number_format($diskpersen)?>%</td>
                      <td colspan=1><font size=1>(<?php echo number_format($diskpersen/100*$order_total)?>)</td>
            </tr>
            <tr bgcolor="#ffffff">
                      <td colspan=2><font size=1><b></td>
                      <td colspan=1><font size=1>Disk (Rp)</td>
                      <td colspan=1><font size=1>(<?php echo number_format($diskrp)?>)</td>
            </tr>
            <tr bgcolor="#ffffff">
                      <td colspan=2><font size=1><b></td>
                      <td colspan=1><font size=1>Ongkir</td>
                      <td colspan=1><font size=1><?php echo number_format($ongkir)?></td>
            </tr>
            <tr bgcolor="#ffffff">
                      <td colspan=2><font size=1><b></td>
                      <td colspan=1><font size=1><b>Grand Total</td>
                      <td colspan=1><font size=1><b><?php echo number_format($order_total-($diskpersen/100*$order_total)-$diskrp+$ongkir)?></td>
            </tr>



        </table>
    </div>

    <div class="container">
    <p>
      <br><br><span class="promo">BCA 4700365056 an Meyliana</span></p>
    <p class="expire">Thank You for Order</p>
  </div>
 
</div>

</body>
</html>