
<?php
  
  session_start();

  include_once "../counter/db/config.php";

  include '../../admin/db/saucerer.php';

// Define variables and initialize with empty values
$id = $user_id = $name = $email = $phone = $role = $priviledges = $password = $confirm_password = $image = $cover_image = $token = $access = "";
$id_err = $user_id_err = $name_err = $email_err = $phone_err = $role_err = $priviledges_err = $password_err = $confirm_password_err = $image_err = $cover_image_err = $token_err = $access_err = "";
 
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] != true){

  $loggedin = "";

  header("location: ../secure/?page=contact");
    exit;
  
    
} elseif(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] == true){

  $loggedin = $_SESSION['loggedin'];
  $id = $_SESSION['id'];
  $token = $_SESSION['token'];


// of college_data table 

$sql = "SELECT * FROM secure WHERE token='$token'"; 

$result = $conn->query($sql); 

  
// Display data on web page 

while($row = mysqli_fetch_array($result)) { 

  $id = $row['id'];
  $name = $row['name'];
  $token = $row['token'];

}
    
}


$sql = "UPDATE contact SET board='1'";
$conn->query($sql);


// Generating a random number
date_default_timezone_set('Africa/Kampala');
$today = date("His-dmY",time());

$code1 = rand(1,9); 
$code2 = rand(1,9);
$code3 = $code1 * $code2; 
$tokens = "$code3-$today";

  ?>

<?php

$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
  case "add":
if(!empty($_POST["quantity"])) {
      $productByCode = $db_handle->runQuery("SELECT * FROM shop WHERE id='" . $_GET["id"] . "'");
      $itemArray = array($productByCode[0]["id"]=>array('name'=>$productByCode[0]["name"], 'id'=>$productByCode[0]["id"], 'quantity'=>$_POST["quantity"], 'cat'=>$productByCode[0]["cat"], 'label_1'=>$productByCode[0]["label_1"], 'price_1'=>$productByCode[0]["price_1"], 'label_2'=>$productByCode[0]["label_2"], 'price_2'=>$productByCode[0]["price_2"], 'label_3'=>$productByCode[0]["label_3"], 'price_3'=>$productByCode[0]["price_3"], 'image'=>$productByCode[0]["image"]));
      

      
      if(!empty($_SESSION["cart_item"])) {
        if(in_array($productByCode[0]["id"],array_keys($_SESSION["cart_item"]))) {
          foreach($_SESSION["cart_item"] as $k => $v) {
              if($productByCode[0]["id"] == $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["id"] == $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>Contact | 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" />


    <!--Boxicons-->
  <link href="../assets/boxicon.min.css" rel="stylesheet">

    <!--Fontawesome 6-->
    <link href="../css/fontawesome/css/all.css" rel="stylesheet">


    <!--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 {
  width: 400px;
  padding: 30px;
  margin-bottom: 20px;
  border-radius: 10px;
  border-top-left-radius: 0;
  background: #f195b2;
}

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


/*Wishlist Section*/
.wishlist-section .cart-table .table tr:first-child td i{
  padding-right: 5px;
  padding-bottom: 20px;
}

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

.wishlist-section .cart-table .table .pro-price a,
.wishlist-section .cart-table .table .pro-title a{
  text-decoration: none;
  color: black;
}



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

.wishlist-section .cart-table .table .in-stock {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.7);
}

.wishlist-section .cart-table .table .pro-price {
  padding-left: 20px;
}



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

a.view:hover {
  color: #f195b2;
  background: #fff;
}


/*Home Link Button*/
.home_btn a{
  background: #f195b2;
  color: #000;
  text-decoration: none !important;
  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 i {
  margin-top: 10px;
  margin-right: 10px;
}

.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="../"><img src="../../img/il.png" alt="Irungi Kitchen"></a> </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">

      <?php

              $sql = "SELECT * FROM contact";

              //use for MySQLi-OOP
              $query = $conn->query($sql);

              if($row = $query->fetch_assoc() == 0) {

                      echo"<h2>NO MESSAGES</h2>";

                      echo "<div class='home_btn' align='center'><a href='../'><i class='bx bx-caret-left'></i> GO BACK </a></div>";

          }else {

                  echo"<h2>MESSAGES</h2>";

                  }

               $sql = "SELECT COUNT(*) FROM contact WHERE seen=''";

              //use for MySQLi-OOP
              $query = $conn->query($sql);

              while($row = $query->fetch_assoc()) {

                if($row['COUNT(*)']) {

                    echo "<div class='home_btn' align='center'><a href='seen'><i class='fa fa-refresh'></i> MARK ALL AS SEEN </a></div>";

                    }

                  }

         ?>
                            
                        </div>
                    </div>
            </div>
        </div> <br>
        <!-- Welcome Section End -->


        <!--Wishlist section start-->
        <div class="wishlist-section">
            <div class="container">
                <div class="row">

                    <div class="col-12">
                        <!-- Cart Table -->
                        <div class="cart-table">




<?php


$sql = "SELECT * FROM contact ORDER BY id DESC";

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

                date_default_timezone_set('Africa/Kampala');

                $start_count = date("Y-m-d H:i:s", strtotime($row['timestamp'] . "+1 days"));

                $today = date("Y-m-d H:i:s",time()); 

                $start_diff = new DateTimeImmutable($row['timestamp']); 

                $endd_diff = new DateTimeImmutable($today);

                $duration = $start_diff->diff($endd_diff);

                $today_diff = new DateTimeImmutable($today);

                $days_diff = $today_diff->diff($start_diff);

                echo "<table class='table' align='center'>";
                              
                        echo "<tbody>";

                                    echo "<tr>";
                                        echo "<td><i class='bx bx-user'></i> ".$row['name']."</td>";

                                    echo "</tr>";

                                    echo "<tr>";
                                        echo "<td class='pro-title'><span>
                                            <a href='tel:".$row['phone']."'>".$row['phone']."
                                        </a></span></td>";
                                        echo "<td class='pro-price'><a href='mailto:".$row['email']."'>
                                            ".$row['email']."</a>
                                             </td>";
                                    echo "</tr>";

                                    echo "<tr>";
                                        echo "<td>Message</td>";
                                        echo "<td class='pro-price' style='line-height: 45px;'><span>
                                            ".substr($row['message'], 0, 20)."
                                        </span></td>";
                                    echo "</tr>";

                                    echo "<tr>";
                                        echo "<td class='pro-stock'>";

                                        echo "<span class='in-stock'>";



    if ($days_diff->format('%i')==0 & $days_diff->format('%h')==0 & $days_diff->format('%d')==0  & $days_diff->format('%m')==0 & $days_diff->format('%y')==0) {
    echo "Just Now";

    } elseif ($days_diff->format('%i')>=0 & $days_diff->format('%h')>=0 & $days_diff->format('%d')>=0  & $days_diff->format('%m')>=0 & $days_diff->format('%y')==1) {

        echo "Last Year on ".date("D, jS M", strtotime($row['timestamp']))."";

    } elseif ($days_diff->format('%i')>=0 & $days_diff->format('%h')>=0 & $days_diff->format('%d')>=0  & $days_diff->format('%m')>=0 & $days_diff->format('%y')>=1) {

        echo "".$days_diff->format("%y years")." ago on ".date("D, jS M", strtotime($row['timestamp']))."";

    } elseif ($days_diff->format('%i')==1 & $days_diff->format('%h')==0 & $days_diff->format('%d')==0) {
    echo "".$days_diff->format("%i minute")." ago";

    } elseif ($days_diff->format('%i')>=0 & $days_diff->format('%h')==0 & $days_diff->format('%d')==0) {
    echo "".$days_diff->format("%i minutes")." ago";

    } elseif ($days_diff->format('%h')==1 & $days_diff->format('%d')==0 & $days_diff->format('%m')==0) {
    echo "".$days_diff->format("%h hour")." ago";

    } elseif ($days_diff->format('%h')>=0 & $days_diff->format('%d')==0 & $days_diff->format('%m')==0) {
    echo "".$days_diff->format("%h hours")." ago";

    } elseif ($days_diff->format('%d')==1) {

        echo "Yesterday at ".date("H:i", strtotime($row['timestamp']))."";

     } elseif ($days_diff->format('%m')==0) {

        echo "".$days_diff->format("%d days")." ago at ".date("H:i", strtotime($row['timestamp']))."";

     } elseif ($days_diff->format('%m')==1) {

        echo "Last Month on ".date("jS D", strtotime($row['timestamp']))."";

     }  elseif ($days_diff->format('%m')>=1) {

        echo "".$days_diff->format("%m months")." ago on ".date("jS D", strtotime($row['timestamp']))."";

     }                    

                                        echo "</span>";

                                        echo "</td>";

                                        echo "<td><a href='chat/?id=".$row['id']."' class='view'>VIEW DETAILS</a></td>";
                                    echo "</tr>";

                        echo "</tbody>";

                    echo "</table>";

                                }

                                ?>

                            
                        </div>
                    </div>

                </div>
            </div>
        </div> <br> <br>
        <!--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>