<?php
require('connection.inc.php');
require('security.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!isset($_SESSION['user_id'])) {
    header("Location: log-in.php");
    exit;
}

$user_id = $_SESSION['user_id'];

// Fetch only logged-in user's paid orders
$stmt = $conn->prepare("SELECT * FROM orders WHERE user_id = ? AND payment_status = 'Paid' ORDER BY created_at DESC");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
?>


<!doctype html>
<html class="no-js" lang="zxx">
<head>
      <meta charset="utf-8">
      <meta http-equiv="x-ua-compatible" content="ie=edge">
      <title>LoveBeri</title>
      <meta name="description" content="">
      <meta name="viewport" content="width=device-width, initial-scale=1">

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

      <!-- CSS here -->
      <link rel="stylesheet" href="assets/css/bootstrap.min.css">
      <link rel="stylesheet" href="assets/css/animate.css">
      <link rel="stylesheet" href="assets/css/swiper-bundle.css">
      <link rel="stylesheet" href="assets/css/slick.css">
      <link rel="stylesheet" href="assets/css/magnific-popup.css">
      <link rel="stylesheet" href="assets/css/spacing.css">
      <link rel="stylesheet" href="assets/css/meanmenu.css">
      <link rel="stylesheet" href="assets/css/nice-select.css">
      <link rel="stylesheet" href="assets/css/fontawesome.min.css">
      <link rel="stylesheet" href="assets/css/icon-dukamarket.css">
      <link rel="stylesheet" href="assets/css/jquery-ui.css">
      <link rel="stylesheet" href="assets/css/main.css">
   </head>
   <body>


      <!-- Scroll-top -->
      <button class="scroll-top scroll-to-target" data-target="html">
         <i class="icon-chevrons-up"></i>
      </button>
      <!-- Scroll-top-end-->


      <!-- header-area-start -->
     <?php 
     include 'header.php'
     ?>
      <!-- header-area-end -->

      <main>

         <!-- breadcrumb-area-start -->
         <div class="breadcrumb__area pt-5 pb-5">
            <div class="container">
               <div class="row">
                  <div class="col-lg-12">
                     <div class="tp-breadcrumb__content">
                        <div class="tp-breadcrumb__list">
                           <span class="tp-breadcrumb__active"><a href="index.php">Home</a></span>
                           <span class="dvdr">/</span>
                           <span>Orders</span>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
        
         <style>
    .order-wrapper {
      max-width: 1050px;
      margin: 50px auto;
      padding: 20px;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    .order-wrapper h1 {
      text-align: center;
      font-size: 32px;
      font-weight: bold;
      margin-bottom: 30px;
      letter-spacing: 1px;
    }

    .order-box {
      background-color: #fff;
      padding: 20px;
      border-radius: 12px;
      box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
    }

    .order-table {
      width: 100%;
      border-collapse: collapse;
      margin-bottom: 20px;
    }

    .order-table th {
      background-color: #e9f1ff;
      padding: 10px;
      text-align: left;
      font-weight: 600;
      border: 1px solid #ddd;
    }

    .order-table td {
      padding: 10px;
      border: 1px solid #ddd;
    }

    .order-address {
      font-size: 15px;
      margin: 10px 0;
    }

    .order-address strong {
      color: #000;
    }

    hr {
      margin: 20px 0;
      border: none;
      border-top: 1px solid #ccc;
    }

    .product-title {
      font-weight: 600;
      font-size: 18px;
      margin-bottom: 10px;
    }

    .product-box {
      display: flex;
      align-items: center;
      gap: 20px;
    }

    .product-box img {
      width: 80px;
      height: auto;
      border-radius: 8px;
      border: 1px solid #eee;
    }

    .product-details a {
      font-weight: 600;
      text-decoration: underline;
      color: #007bff;
    }

    .product-details p {
      margin: 5px 0;
      font-size: 15px;
    }

  </style>
         <div class="order-wrapper">
  <h1>ORDERS</h1>

 <div class="order-box">
  <?php if ($result->num_rows > 0): ?>
    <table class="order-table">
      <thead>
        <tr>
          <th>Order Id</th>
          <th>Name</th>
          <th>Phone No.</th>
          <th>Email</th>
          <th>Amount</th>
          <th>Order Date</th>
          <th>Address</th>
          <th>Detail</th>
        </tr>
      </thead>
      <tbody>
        <?php while ($row = $result->fetch_assoc()): ?>
        <tr>
          <td><?= htmlspecialchars($row['order_id']) ?></td>
          <td><?= htmlspecialchars($row['first_name'] . ' ' . $row['last_name']) ?></td>
          <td><?= htmlspecialchars($row['phone']) ?></td>
          <td><?= htmlspecialchars($row['email']) ?></td>
          <td><strong>₹<?= number_format($row['subtotal'], 2) ?></strong></td>
          <td><?= date("d-m-Y", strtotime($row['created_at'])) ?></td>
          <td><?= htmlspecialchars($row['address']) ?></td>
          <td><a href="orderdetail.php?order_id=<?= urlencode($row['order_id']) ?>">View</a></td>
        </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  <?php else: ?>
<div class="text-center">
      <img src="assets/img/allnew/ordernotfound.gif" height="300">
      <h4 style="text-align: center; font-weight: bold;">No orders found.</h4>
    <a href="product.php" class="text-danger ">Shop Now</a>
</div>
    
  <?php endif; ?>
  <hr>
</div>

</div>
      </main>

     <?php 
     include "footer.php"
     ?>

      <script src="assets/js/jquery.js"></script>
      <script src="assets/js/waypoints.js"></script>
      <script src="assets/js/bootstrap.bundle.min.js"></script>
      <script src="assets/js/swiper-bundle.js"></script>
      <script src="assets/js/nice-select.js"></script>
      <script src="assets/js/slick.js"></script>
      <script src="assets/js/magnific-popup.js"></script>
      <script src="assets/js/counterup.js"></script>
      <script src="assets/js/wow.js"></script>
      <script src="assets/js/isotope-pkgd.js"></script>
      <script src="assets/js/imagesloaded-pkgd.js"></script>
      <script src="assets/js/countdown.js"></script>
      <script src="assets/js/ajax-form.js"></script>
      <script src="assets/js/jquery-ui.js"></script>
      <script src="assets/js/meanmenu.js"></script>
      <script src="assets/js/main.js"></script>
   </body>
</html>
