<?php session_start();?>
<?php include'includes/header.php'; ?>
<?php include'includes/sidebar.php';?>
<?php include'includes/functions.php';?>
<?php include'includes/navbar.php'; ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
<style>

   /* body {
      font-family: 'Segoe UI', sans-serif;
      background-color: #f9f9f9;
      margin: 0;
      padding: 20px;
    }*/
.invoice-container {
  max-width: 800px;
  margin: auto;
  background: #fff;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.invoice-header {
  text-align: center;
  border-bottom: 2px solid #eee;
  margin-bottom: 20px;
}

.invoice-header h2 {
  margin: 0;
  color: #333;  
}

.invoice-details {
  margin-top: 20px;
}

.invoice-details p {
  margin: 5px 0;
  font-size: 16px;
}

.summary {
  background-color: #f1f1f1;
  padding: 15px;
  margin-top: 20px;
  border-radius: 8px;
}

.summary h3 {
  margin-bottom: 10px;
  color: #444;
}

.summary p {
  margin: 5px 0;
}

.btns {
  margin-top: 30px;
  text-align: center;
}

.btns button {
  margin: 10px;
  padding: 10px 20px;
  font-size: 15px;
  cursor: pointer;
  border: none;
  border-radius: 5px;
  background-color: #1976d2;
  color: white;
  transition: background 0.3s;
}

.btns button:hover {
  background-color: #004ba0;
}

@media print {
  body * {
    visibility: hidden;
  }

  #invoiceArea, #invoiceArea * {
    visibility: visible;
  }

  #invoiceArea {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
  }

  .btns {
    display: none !important;
  }

  .logo-area img {
    width: 60px !important;
    height: auto !important;
    object-fit: contain !important;
    display: block !important;
    margin: 0 !important;
  }
}

.logo-area img{
 
  max-width: 60px;
    height: auto;
    border: 2px solid #ccc;
    border-radius: 5px;
    display: block;
}
.invoice-title{
  width: 100%;
  text-align: center;
}
.invoice-title h4{
  font-size: 30px;
  font-weight: bold;
  font-family: sans-serif;
}
.invoice-title span{
  font-size: 18px;
  font-weight: bold;
  font-family: sans-serif;
}
.invoice-cust{
  padding-top: 20px;
}
.invoice-cust h4{
  font-size: 17px;
}
.invoice-cust span{
  font-size: 16px;
  font-weight: bold;
}
.total h4{
  font-size: 18px;
  font-weight: bold;
}
.summary h4{
  font-weight: 18px;
  font-size: 18px;
}
.summary span{
  font-size: 15px;
  font-family: sans-serif;
  font-weight: bold;
}

</style>
<?php
if (isset($_SESSION['customer'])) {
 $customer = $_SESSION['customer'];
}?>
<?php

  $getSum = "SELECT SUM(price) AS totalPrice, SUM(quantity) 
            AS totalQty, COUNT(product) AS totalPdt 
            FROM catalog WHERE customer = ?";
  $sumpdo = $pdo->prepare($getSum);
  $sumpdo->execute([$customer]);
  $dataSum    = $sumpdo->fetch(PDO::FETCH_ASSOC);
  $summazion  = $dataSum['totalPrice'];
  $totalQty   = $dataSum['totalQty'];
  $totalPdt   = $dataSum['totalPdt'];

  $created = date('Y-m-d H:i:s');
  ?>
  <div class="invoice-container" id="invoiceArea">
    <div class="invoice-top">
      <div class="logo-area">
        <img src="logo/logo.jpg" alt="logo" class="logo-print">
      </div>
      <div class="invoice-title">
        <h4>DIVINE PRODUCT ENT.</h4>
        <span>Your business address</span>
      </div>
    </div>
      <div class="invoice-cust mt-3">  
        <h4>Name: <?php echo $customer;?></h4>
        <span>Invoice No: INV-1233</span>
        <label><?php echo $created;?></label>
      </div>
    <div class="card-header text-center">
        <h5>Invoice Summary</h5>
    </div>
    <table class="table table-hover table-bordered table-striped">
      <tr>
        <th>S/N</th>
        <th>Product Name</th>
        <th>Quantity</th>
        <th>Price</th>
      </tr>
      <tbody>
      <?php
      // trigger the print button 
      $prt = "SELECT * FROM catalog WHERE customer = ?";
      $prtpdo = $pdo->prepare($prt);
      $prtpdo->execute([$customer]);
      $sn = 0;
      while ($prtRow = $prtpdo->fetch(PDO::FETCH_ASSOC)) {
        $requestProduct = $prtRow['product'];
        $requestQty    = $prtRow['quantity'];
        $paidPrice     = $prtRow['price'];
        $sn = $sn + 1;
        
      ?>
        <tr>
          <td><?php echo $sn;?></td>
          <td><?php echo $requestProduct?></td>
           <td><?php echo $requestQty?></td>
          <td>₦<?php echo $paidPrice;?></td>
        </tr>
        <?php };?>
        
      </tbody>
    </table>

    <div class="total">
      <h4>TOTAL: ₦<?php echo $summazion;?></h4>
    </div>

    <div class="summary" style="background: none">
      <h4>SUMMARY:</h4>
      <span>No of Products: <?php echo $totalPdt; ?></span><br>
      <span>No of Quantity: <?php echo $totalQty;?></span>
    </div>
  </div>

  
 

  <!-- <div class="btns">
    <button onclick="printInvoice()">🖨️ Print Invoice</button>
    <button onclick="downloadPDF()">⬇️ Download PDF</button>
  </div> -->

  <script>
    function printInvoice() {   
      window.print();
      
    }
    // printInvoice();
    // setInterval(printInvoice, 10000);

    function downloadPDF() {
      const element = document.getElementById('invoiceArea');
      html2pdf().from(element).save('invoice.pdf');
    }
  </script>
<?php include'includes/footer.php';?>