<?php

session_start();

//including the database connection file
include_once("../../../../admin/db/saucerer.php");

if (empty($_GET['code']) & empty($_GET['email'])) {
  header('location: ../../../');
}

$order_code = $_GET['code'];

$sql = "SELECT * FROM checkout WHERE order_code = '$order_code'";

              //use for MySQLi-OOP
              $query = $conn->query($sql);
              while($row = $query->fetch_assoc()){

    $order_code = $row['order_code'];
    $fname = $row['fname'];
    $lname = $row['lname'];
    $email = $row['email'];
    $phone = $row['phone'];
    $address = $row['address'];
    $location = $row['location'];
    $image = $row['image'];
    $subtotal = $row['subtotal'];
    $delivery = $row['delivery'];
    $discount = $row['discount'];
    $total = $row['total'];
    $name=$row['name']; 
    $quantity=$row['quantity'];
    $price=$row['price'];
    $image=$row['image'];

   }

?>


<?php

$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
  case "add":
    if(!empty($_POST["quantity"])) {
      $productByCode = $db_handle->runQuery("SELECT * FROM checkout WHERE code='" . $_GET["code"] . "'");
      $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'type'=>$productByCode[0]["type"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"], 'image'=>$productByCode[0]["image"]));
      $productByCode = $db_handle->runQuery("SELECT * FROM home_products WHERE code='" . $_GET["code"] . "'");
      $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'type'=>$productByCode[0]["type"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"], 'image'=>$productByCode[0]["image"]));

      
      if(!empty($_SESSION["cart_item"])) {
        if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
          foreach($_SESSION["cart_item"] as $k => $v) {
              if($productByCode[0]["code"] == $k) {
                if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                  $_SESSION["cart_item"][$k]["quantity"] = 0;
                }
                $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
              }
          }
        } else {
          $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
        }
      } else {
        $_SESSION["cart_item"] = $itemArray;
      }
    }
  break;
  case "remove":
    if(!empty($_SESSION["cart_item"])) {
      foreach($_SESSION["cart_item"] as $k => $v) {
          if($_GET["code"] == $k)
            unset($_SESSION["cart_item"][$k]);        
          if(empty($_SESSION["cart_item"]))
            unset($_SESSION["cart_item"]);
      }
    }
  break;
  case "empty":
    unset($_SESSION["cart_item"]);
  break;  
}
}
?>



<!doctype html>
<html class="no-js" xmlns:og="http://ogp.me/ns#">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Orders | Irungi Kitchen &copy;</title>
    
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Place favicon.ico in the root directory -->
    <link rel="icon" href="../../../../img/fav-icon.png" type="image/x-icon" />


    <!--Animations-->
    <link rel="stylesheet" type="text/css" href="../../../../css/animations/style.css">

    <link rel="stylesheet" type="text/css" href="../../../../css/animations/attention/blink.css">
    <link rel="stylesheet" type="text/css" href="../../../../css/animations/attention/bounce.css">
    <link rel="stylesheet" type="text/css" href="../../../../css/animations/attention/flicker.css">
    <link rel="stylesheet" type="text/css" href="../../../../css/animations/attention/jello.css">
    <link rel="stylesheet" type="text/css" href="../../../../css/animations/attention/pulsate.css">
    <link rel="stylesheet" type="text/css" href="../../../../css/animations/attention/shake.css">
    <link rel="stylesheet" type="text/css" href="../../../../css/animations/attention/vibrate.css">
    <link rel="stylesheet" type="text/css" href="../../../../css/animations/attention/wobble.css">

    <!-- Modernizr JS -->
    <script src="../../../../assets/js/vendor/modernizr-2.8.3.min.js"></script>

    <style type="text/css">

body {
  background-image: url(../../../../img/error-bg1.jpg);
  background-attachment: fixed;
  background-position: top;
}

/*Logo Section*/
.logo img{
  border-radius: 100px;
}

table {
  padding: 30px;
  border-radius: 10px;
  background: #f195b2;
}

/*Welcome Section*/
.welcome-section .welcome-content a{
  color: #000;
  text-decoration: underline;
}


/*Wishlist Section*/
.wishlist-section .cart-table .table th {
  text-align: center;
  width: 120px;
  padding-bottom: 20px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.4);
}

.wishlist-section .cart-table .table:last-child th {
  text-align: center;
  padding-bottom: 10px;
}

.wishlist-section .cart-table .table td {
  text-align: center;
  padding-top: 10px;
}

.wishlist-section .cart-table .table img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 3px;
}

.wishlist-section .cart-table .table .pro-title {
  width: 200px;
}

.wishlist-section .cart-table .table .pro-thumbnail {
  width: 100px;
}



/*Home Link Button*/
.home_btn a{
  background: #f195b2;
  color: #000;
  text-decoration: none;
  text-transform: uppercase;
  line-height: 36px;
  padding: 10px 40px;
  font-size: 14px;
  font-weight: 600;
  border-radius: 3px;
  border: none;
  cursor: pointer;
  transition: 1s;
}

.home_btn a:hover {
  color: #f195b2;
  background: #000;
}


/*Copyright Section*/
.copyright-section .copyright-content p:first-child{
  color: rgba(0, 0, 0, 0.7);
  margin-top: 40px;
}

.copyright-section .copyright-content p:last-child{
  color: rgba(0, 0, 0, 0.7);
  font-size: 12px;
}

.copyright-section .copyright-content a{
  color: rgba(0, 0, 0, 0.7);
  text-decoration: none;
}


    </style>
</head>

<body>

    <div id="main-wrapper">

      <div class="logo" align="center"> <a href="../../../../?action=empty"><img src="../../../../img/il.png" alt="Irungi Kitchen"></a> </div>

      <!-- Welcome Section Start -->
        <div class="welcome-section">
            <div class="container">
                <div class="row align-items-center">
                    <div class="">
                        <div class="welcome-content" align="center">
                            <h2>Thank You! <span><?php echo $lname;?></span></h2>
                            <p>We've received your order as below, an invoice has been emailed to your address.</p>
                        </div>
                    </div>
            </div>
        </div>
        <!-- Welcome Section End -->


        <!--Wishlist section start-->
        <div class="wishlist-section">
            <div class="container">
                <div class="row">
                  <div align="center">
                  <?php

                  echo "<img src='../../../../admin/orders/qr/$order_code.png' width='100px;' style='border-radius: 10px;'>";

                  ?>
                  </div>

                    <div class="col-12">
                        <!-- Cart Table -->
                        <div class="cart-table">
                            <table class="table" align="center">
                                <thead>
                                    <tr>
                                        <th class="pro-thumbnail">Image</th>
                                        <th class="pro-title">Product</th>
                                        <th class="pro-price">Price</th>
                                        <th class="pro-stock">Quantity</th>
                                        <th class="pro-stock">Total</th>
                                    </tr>
                                </thead>
                                <tbody>

<?php


$sql = "SELECT * FROM checkout WHERE order_code = '$order_code' AND email='$email'";

              //use for MySQLi-OOP
              $query = $conn->query($sql);
              while($row = $query->fetch_assoc()){

                $item_total = $row['quantity'] * $row['price'];

                                    echo "<tr>
                                        <td>
                                             <img src='../../../../admin/images/".$row['image']."'></td>
                                        <td class='pro-title' style='line-height: 45px;'><a>
                                            ".$row['name']."</a>
                                             </td>
                                        <td class='pro-price'><span>
                                            ".number_format($row['price'])."/-
                                        </span></td>
                                        <td class='pro-stock' align='center'><span class='in-stock'>
                                            ".$row['quantity']."</span></td>
                                        <td class='pro-stock'><span class='total'>
                                            ".number_format($item_total)."/-</span></td>
                                    </tr>";

                                }

                                ?>

                                </tbody>
                            </table> <br>

                            <table class="table table" align="center">
                                <thead>
                                    <tr>
                                        
                                        <th class="pro-price">Subtotal</th>
                                        <th class="pro-price">Delivery</th>
                                        <th class="pro-stock">Total</th>
                                    </tr>
                                </thead>
                                <tbody>

                                <?php


$sql = "SELECT SUM(subtotal), delivery, total FROM checkout WHERE order_code = '$order_code' AND email='$email' ORDER BY id LIMIT 1";

              //use for MySQLi-OOP
              $query = $conn->query($sql);
              while($row = $query->fetch_assoc()){

                                    echo "<tr>";
                                        
                                        echo "<td class='pro-price' style='line-height: 45px;'><a>
                                            ".number_format($row['SUM(subtotal)'])."/-</a>
                                             </td>";
                                        echo "<td class='pro-price' style='line-height: 45px;'>";
                                        if ($row['delivery']) {
                                        echo "<span>".number_format($row['delivery']) ."/-</span>";
                                        } else {
                                          echo "<span>-</span>";
                                        }
                                        echo "</td>";
                                        echo "<td class='pro-stock' style='line-height: 45px;font-weight: bold;'><span class='in-stock'>
                                            ".number_format($row['total'])."/-</span></td>";
                                    echo "</tr>";

                                }

                                ?>

                                </tbody>
                            </table>
                        </div>
                    </div> <br>

                    <!-- Welcome Section Start -->
        <div class="welcome-section">
            <div class="container">
                <div class="row align-items-center">
                    <div class="">
                        <div class="welcome-content" align="center">
                            <p>We'll call you shortly</p>
                            <p>Check your email for more details or <a href="tel:+256707681086">call us</a>.</p>
                        </div>
                    </div>
            </div>
        </div> <br>
        <!-- Welcome Section End -->

                   <div class="home_btn" align="center"><a href="../../../../?action=empty"> Home </a></div>

                </div>
            </div>
        </div>
        <!--Wishlist section end-->
        
        
         <!-- Copyright Section Start -->
        <div class="copyright-section">
            <div class="container">
                <div class="row align-items-center">
                    <div class="">
                        <div class="copyright-content" align="center">
                            <p>Irungi Kitchen © <script>document.write(new Date().getFullYear());</script></p>
                            <p>Powered by <a target="_blank" href="https://customassistinc.com">Custom Assist Inc.</a></p>
                        </div>
                    </div>
            </div>
        </div>
        <!-- Copyright Section End -->

        
    </div>


    

</body>

</html>