<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js">
</script>
<style>

.modal2 {
    /*display: none;*/
    position: fixed;
    z-index: 99999;
    left: 0; top: 0;
    width: 100%; height: 100%;
    overflow: auto;
    background-color: rgba(0,0,0,0.5);
    backdrop-filter:blur(3px);
}

.modal-content2 {
    background-color: #fff;
    margin: auto;
    padding: 20px;
    border-radius: 8px;
    width: 80%;
    max-width: 500px;
    animation: fadeIn 0.3s;
    margin-top:50px;
}
.modal-header{
    padding: 15px;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

}
.modal-body{
    padding: 15px;
    /*border-top: 1px dashed #ccc;*/
    border-bottom: 1px solid #ddd;
}
.modal-body p{
    text-align: justify-all;
    font-family: sans-serif;
}

.modal-footer{
    padding-bottom: 10px;
}

.modal-header2, .modal-footer2 {
    font-weight: bold;
}

.modal-footer2{
    /*border-top: 1px solid #ddd;*/
    display: flex;
    justify-content: space-between;
}
.yes{
    display: flex;
}
.no{
    display: none;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-20px); }
    to { opacity: 1; transform: translateY(0); }
}
.success-animation { 
    width: 100%;
    display: flex;
    text-align: center;
    justify-content: center;
    align-items: center; 
}

.modal-body p{
    text-align: justify;
}
.plate-btn{
    width: 100%;
    display: flex;
    justify-content: space-around;
    gap:10px;
    padding-top: 10px;
}
.plate-btn button{
    width: 100%;
    padding: 5px;
    color: #fff;
}

.plate-btn .btn1{
    background: #DE3341;
}
.plate-btn .btn1:hover{
    background: #bb1e2c;
}


@media(max-width: 992px){
    .modal-content2{
        width: 90%;
        margin-top:50px;
    }
    .modal-body p{
        font-size: 13px;
    }
    .modal-body span{
        /*font-size: 14px;*/
    }
    .modal-footer2{
        display: flex;
        justify-content: space-between;
        gap:20px;
    }
    .modal-footer2 button{
        font-size: 12px;
    }
    .plate-btn{
        flex-direction: column;
    }
}

/*The Print Invoic*/
@media print {
  @page{
    size: A4;
    margin: 10mm;
  }
  body {
    background: #fff !important;
    margin: 0 !important;
    padding: 0 !important;
  }
  body * {
    visibility: hidden; 
  }
  /* Hide or simplify sidebar */
  .sideNav {
    position: static !important;
    height: auto !important;
    transform: none !important;
    background: none !important;
    color: #000 !important;
  }

  /* Adjust main content area */
  .page-overall {
    padding-left: 0 !important;
    width: 100% !important;
  }

  /* Optional: hide mobile toggle buttons and other nav */
  .mobile-btn, .bar-status {
    display: none !important;
  }
  #invoiceArea, #invoiceArea * {
    visibility: visible!important;
  }

  #invoiceArea {
    position: static!important;
    left: 0;
    top: 0;
    width: 100%;
    height: auto!important;
    margin: 0 auto!important;
    overflow: hidden;
    page-break-inside: avoid!important;
  }

  
  /* Force one-page fit */
   .invoice-container {
     page-break-after: avoid !important;
     page-break-before: avoid !important;
     page-break-inside: avoid !important;
     max-height: 97vh !important;
     overflow: hidden !important;
   }
  .invoice-container *{
    page-break-inside: avoid;
  }
  .invoice-container h4, .invoice-container p, .invoice-container span {
    font-size: 11px !important;
    line-height: 1.2 !important;
  }

  

  .btns {
    display: none !important;
  }

  .logo-area img {
    width: 50px !important;
    height: auto !important;
    object-fit: contain !important;
    display: block !important;
    margin: 0 !important;
  }
  .middle-icon{
    display: flex!important;
  }
  .modal-header{
    display: none!important;
  }
  .icon-bg{
    width: 55px;
    height: 55px;
    background: #E3F3EE!important;
    border:none!important;

  }
  .icon-bg i{
    font-size: 28px!important;
    border:1px solid #E3F3EE!important;
    border-radius: 50%!important;
  }
}


/*Invoice show starts here*/
.invoice-container {
  max-width: 500px;
  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;
}
.invoice-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.logo-area img{
  max-width: 50px;
    height: auto;
    /*border: 2px solid #ccc;*/
    border-radius: 5px;
    display: block;
}
.invoice-title{
  text-align: right;
}
.invoice-title h4{
  font-size: 18px;
  font-weight: bold;
  font-family: sans-serif;
  color: #261756;
  margin: 0;
}
.invoice-title small {
  font-size: 12px;
  color: #777;
}
/*Middle Icon start here*/
.middle-icon{
  display: flex;
  width: 100%;
  text-align: center;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap:10px;
}
.icon-bg{
  /*padding: 15px;*/
  width: 55px;
  height: 55px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  border-radius: 50%;
  background: #E3F3EE;

}
.icon-bg i{
  font-size: 28px;
  border:none;
}
.middle-icon h4{
  font-size: 18px;
  font-weight: bold;
}
/*End of middle icon*/

.reciept-block{
  width: 100%;
}
.reciept-block p {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  margin: 4px 0;
}
.span-left{
  font-size: 12px;
  color: #888;
}
.span-right{
  font-weight: bold;
  font-size: 12px
}

.items-list {
  margin-top: 15px;
}
.items-list h5 {
  font-size: 14px;
  color: #555;
  margin-bottom: 5px;
}
.items-list ul {
  padding-left: 20px;
  margin: 0;
  list-style: disc;
  font-size: 13px;
}
.items-list li {
  margin: 2px 0;
  word-wrap: break-word;
}

.total h4{
  text-align: right;
  font-size: 14px;
  font-weight: bold;
}

/* Keep Animation icon smaller */
.checkmark {
    width: 60px;
    height: 60px;
}

</style>
<div class="modal2" id="theModal">
  <div class="modal-content2">
    <div class="invoice-container" id="invoiceArea">
      <div class="invoice-top" style="padding: 10px;">
        <div class="logo-area">
          <img src="logo/goodness.jpg" alt="logo" class="logo-print">
        </div>
        <div class="invoice-title">
          <h4>DE-GOODNESS ACADEMY</h4>
          <small>Official Payment Receipt</small>
        </div>
      </div>
      
      <div class="middle-icon">
        <div class="icon-bg">
          <i class="fa fa-check-circle text-success"></i>
        </div>
        <h4>Payment Successful!</h4>
      </div>
      <div class="modal-body">
        <hr style="border:1px dashed black; margin-top: -5px;">
        <div class="reciept-block">
          <p><span class="span-left">Name</span>
            <span class="span-right"><?php echo strtoupper($surname) ?>, <?=$otherName?></span>
          </p>
          <p><span class="span-left">Admission No</span>
            <span class="span-right"><?=$adminNo?></span>
          </p>
          <p><span class="span-left">Class</span>
            <span class="span-right"><?=$refClass?></span>
          </p>
          <p><span class="span-left">Session</span>
            <span class="span-right"><?=$refSession?></span>
          </p>
          <p><span class="span-left">Payment Method</span>
            <span class="span-right"><?=$refMethod?></span>
          </p>
          <p><span class="span-left">Date</span>
            <span class="span-right">
              <?php echo $formatDate = date('F j, Y', strtotime($refDate));?>
            </span>
          </p>
        </div>
        <div class="items-list">
          <h5>Items Paid For</h5>
          <ul>
            <?php
            $refItems = "SELECT * FROM payment_history WHERE reference = ?";
            $refStmt = $pdo->prepare($refItems);
            $refStmt->execute([$refId]);

            while ($itemRow = $refStmt->fetch(PDO::FETCH_ASSOC)) {
              $dataItems = $itemRow['items'];
            
             echo "<li>$dataItems</li>";

            }
            $itemAmt = "SELECT SUM(amount) AS totalPrice 
                FROM payment_history WHERE reference = ?";
            $amtQuery = $pdo->prepare($itemAmt);
            $amtQuery->execute([$refId]);
            $querRow = $amtQuery->fetch(PDO::FETCH_ASSOC);
            $refPrice = $querRow['totalPrice'];
            ?>
          </ul>
        </div>
        <hr style="border:1px dashed black">
        <div class="total">
          <h4>TOTAL AMOUNT: ₦<?php echo number_format($refPrice, 2); ?></h4>
        </div>
      </div>
    </div>
    <form action="" method="POST">
      <div class="modal-footer2">
        <div class="plate-btn">
          <button type="button" class="btn btn-sm btn1 text-white" 
            onclick="closeModal2()"><?php echo $msgCloseBtn;?>
            <i class="fa fa-window-close"></i>
          </button>

          <button type="button" 
            class="btn btn-sm info text-white"
            onclick="printInvoice()"> 
            <?php echo $msgActionBtn;?> 
            <i class="fa fa-print"></i>
          </button>

          <button type="button" 
            class="btn btn-sm primary text-white"
            onclick="downloadPDF()"> 
            <?php echo $msgActionBtn2;?> <i class="fa fa-file-pdf"></i>
          </button>
        </div>
      </div>
    </form> 
  </div>
</div>

<script>
  function printInvoice() {   
    const invoice = document.getElementById('invoiceArea');
      invoice.scrollIntoView({ behavior: 'instant', block: 'start' });
      setTimeout(() => {
        window.print();
      }, 200);
    
  }
  


  function downloadPDF() {
    const element = document.getElementById('invoiceArea');
    
    // Temporarily swap animated SVG for static icon
    document.querySelector('.checkmark').style.display = 'none';
    document.querySelector('.mark-text').style.display = 'none';
    document.querySelector('.middle-icon').style.display = 'flex';

    const opt = {
      margin: 0.2,
      filename: 'receipt.pdf',
      image: { type: 'jpeg', quality: 0.98 },
      html2canvas: { scale: 2 },
      jsPDF: { unit: 'in', format: 'a4', orientation: 'portrait' },
      pagebreak: { mode: ['avoid-all'] }
    };

    html2pdf().set(opt).from(element).save().then(() => {
      // Restore animation view
      document.querySelector('.checkmark').style.display = 'block';
      document.querySelector('.middle-icon').style.display = 'none';
    });
  }


</script>
<script>
    function closeModal2(){
        document.getElementById('theModal').style.display="none";
    }
</script>


