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

    // 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($_GET["order_no"])){
      //echo "masuk sini";
      $order_no = $_GET["order_no"];
       // update stok ke detail produk
       $query_sql = "UPDATE order_data SET 
              order_kirim = 1
        WHERE order_no='$order_no'";
        if (mysqli_query($conn, $query_sql)) {
        }
    }
?> 


<!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;
}

table tr:hover td {
            background-color: #e7f3fe;
        }



.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;
}

.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 */ 

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


$username = $_SESSION['username'];

?>
<head>
</head>
<body>
<center>
<button class="btn info" onclick="window.location.href='orderlist.php'">Daftar Pesanan</button>
<button class="btn info" onclick="window.location.href='productionlist.php'">Daftar Produksi</button>

<h2>&nbsp;&nbsp;Daftar Pesanan</h2>
</center>
<div style="overflow-x:auto;">
<table>
<form action="admlist.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">

    </div>
    </div>
    <th></th>
    <th></th>
  </tr>
</form>
<tr>
  <?php
        function tglIndonesia($indo_tgl){
          $tr   = trim($indo_tgl);
          $indo_tgl    = str_replace(array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'), array('Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jum\'at', 'Sabtu', 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'), $tr);
          return $indo_tgl;
        }

        
        
        $query_sql_flight = "SELECT DISTINCT DATE_FORMAT(order_datetime, '%d-%m-%Y') AS tanggal,
                            DATE_FORMAT(order_datetime, '%a, %d-%m-%Y') AS tanggal_format
                            FROM produk_temp
                                LEFT JOIN order_data ON order_no = temp_master
                                WHERE order_datetime IS NOT NULL AND (DATE_FORMAT(order_datetime, '%Y-%m-%d') >= DATE_FORMAT(NOW(),'%Y-%m-%d'))
                            ORDER BY order_datetime ASC";
        $result_flight = mysqli_query($conn, $query_sql_flight);
        while($mas_flight = mysqli_fetch_array($result_flight)){
            $total_hari = 0;
            $tanggal_format = tglIndonesia($mas_flight["tanggal_format"]);
            echo "<tr bgcolor='#FFA800' align='center'>";
            echo "<th colspan=100>&nbsp;&nbsp;<font size=3>$tanggal_format</th>";
            echo "</tr>";
            ?>
            </tr>
                <tr bgcolor="#d1d1d1" align="center">
                <th>No</th>
                <th>Barang</th>
                <th>Jumlah</th>
                <th>Harga</th>
                <th>Subtotal</th>
                <th></th>
            </tr>
            <?php

            $query_sql_no = "SELECT DISTINCT order_no, order_total, order_nama, order_nowa, order_alamat, order_kirim, 
            DATE_FORMAT(order_datetime, '%H:%i') AS jam_pesan FROM produk_temp  
                          LEFT JOIN order_data ON order_no = temp_master
                          WHERE DATE_FORMAT(order_datetime, '%d-%m-%Y') LIKE ('$mas_flight[tanggal]')
                          ORDER BY order_kirim ASC";
                          $result_no = mysqli_query($conn, $query_sql_no);
            while($mas_no = mysqli_fetch_array($result_no)){
              if ($mas_no['order_kirim'] == 1) {
                  //echo "masuk atas";
                ?> <tr bgcolor="#77c904">
              <?php } else {
                ?> <tr bgcolor="#ebebeb">
              <?php }
            ?>

                    <td colspan=2><font size=2><b><?php echo safe_urldecode($mas_no['order_nama']).' | '.$mas_no['order_nowa'].' | '.safe_urldecode($mas_no['order_alamat']).' | No : '.$mas_no['order_no'].' | Jam : '.$mas_no['jam_pesan']?></td>
                    <td colspan=1><b><button class="btn success" onclick="window.location.href='orderlist.php?order_no=<?php echo $mas_no['order_no'];?>'" type="button">Kirim</button></td>
                    <td colspan=1><b><button class="btn warning" onclick="window.location.href='cetak_pesanan_satuan.php?order_no=<?php echo $mas_no['order_no'];?>'" type="button">Cetak</button></td>
                    <td colspan=1><b><button class="btn info" onclick="window.location.href='input_ongkir.php?order_no=<?php echo $mas_no['order_no'];?>'" type="button">Print Nota</button></td>
                    <td colspan=1><b><button class="btn danger" onclick="window.location.href='delete_confirm1.php?order_no=<?php echo $mas_no['order_no'];?>'" type="button">Hapus</button>
                    
                    </td>
                  </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 = $mas_no[order_no]";
                  $result = mysqli_query($conn, $query_sql);
                  while($mas = mysqli_fetch_array($result)){
                  ?>
                      <tr>
                        <td align="center"><?=$no++?></td>
                        <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
                  
                  }$no=1; 
                  ?>
                    <tr bgcolor="#ffffff">
                      <td colspan=3><font size=2><b></td>
                      <td colspan=1><font size=2><b>Total</td>
                      <td colspan=2><font size=2><b><?php echo number_format($mas_no['order_total'])?></td>
                    </tr>
                  <?php $total_hari = $total_hari + $mas_no['order_total'];
            }
            ?>
                    <tr bgcolor="#f5f0ff">
                      <td colspan=3><font size=2><b></td>
                      <td colspan=1><font size=3><b>Subtotal</td>
                      <td colspan=2><font size=3><b><?php echo number_format($total_hari)?></td>
                    </tr>  
            <?php
        }    
                ?>
        

</table>
</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>