<?php include ("header.php");?>
<div class="dashboard-body">
<div class="d-flex justify-content-between mb-3 align-items-center dashboardBodyNav">
   <!---->
</div>
<style>
   .table-wrapper {
   width: 100%!important;
   overflow-x: auto!important;
   }
   @media only screen and (max-width: 899px){
   .hide--desktop {
   display: none!important;
   }
   }
   @media only screen and (min-width: 900px){
   .hide--mobile {
   display: none!important;
   }
   }
   .thumb {
   width: 30px;
   height: 30px;
   margin-bottom: 15px;
   -webkit-border-radius: 50%;
   border: 2px solid #ffffff;
   box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.2);
   }
   .thumbi {
   width: 50px;
   height: 50px;
   margin-bottom: 15px;
   -webkit-border-radius: 50%;
   border: 2px solid #ffffff;
   }
   .table-responsive {
   display: block!important;
   width: 100%!important;
   overflow-x: auto!important;
   }
   .table {
   width: 100%!important;
   min-width: 300px!important; 
   table-layout: auto!important; 
   white-space: nowrap!important; 
   }
   .fade.show {
   opacity: 1;
   }
   .table th, .table td {
   padding: 8px!important; 
   text-align: left!important;
   }
   @media (max-width: 768px) {
   .table th, .table td {
   padding: 4px!important; 
   }
   }
   @media (max-width: 768px) {
   .offcanvas {
   }
   }
   @media (min-width: 769px) {
   .modal {
   display: none;
   }
   }
   .offcanvas {
   visibility: visible!important;
   }
   .modal {
   visibility: visible!important; 
   }
   .hidden{
   display: none!important;
   } 
   .offcanvas-backdrop {
   background-color: #0000000a!important;
   }
</style>
<div class="row justify-content-end gy-3 align-items-center justify-content-between">
<div class="col-lg-4">
   <h4 class="mb-0">Deposit History</h4>
</div>
<div class="col-4 text-end">
   <button type="button" class="btn btn--primary outline flex-fill btn--lg depositBtn">
   Deposit                            </button>
</div>
<div class="col-4">
   <div class="d-flex gap-3">
      <form action="" class="flex-fill" autocomplete="off">
         <div class="input-group">
            <input type="text" name="search" class="form-control form--control" value="" placeholder="Search by transactions" id="search">
            <button class="input-group-text bg-primary text-white">
            <i class="las la-search"></i>
            </button>
         </div>
      </form>
   </div>
</div>
<div class="col-md-12">
   <div class="table-wrapper">
      <div class="table-responsive">
         <table class="table">
            <thead>
               <tr>
                  <th>Trx Type</th>
                  <th class="hide--desktop">Date</th>
                  <th>Amount</th>
                  <th class="hide--desktop">Detail</th>
                  <th class="hide--desktop">status</th>
                  <th>View</th>
               </tr>
            </thead>
            <tbody>
               <?php
                  $sql = "SELECT `id`, `trx_id`, `user_id`, `amount`, `currency`, `rate`, `gateway`, `type`, `status`, `created_at`, `updated_at` FROM `transactions` WHERE `user_id` = ? AND type = 'Deposit'";
                  
                  $stmt = $conn->prepare($sql);
                  $stmt->bind_param("i", $user_id); 
                  $stmt->execute();
                  
                  // Get the result
                  $result = $stmt->get_result();
                  
                  // Loop through all transactions using while
                  while ($transaction = $result->fetch_assoc()) {?>                         
               <?php
                  // Given date (2024-11-16 15:24:03)
                  $date_string = $transaction['created_at'];
                  
                  // Create DateTime object from the given date
                  $given_date = new DateTime($date_string);
                  
                  // Get the current date and time
                  $current_date = new DateTime();
                  
                  // Calculate the difference between the two dates
                  $interval = $current_date->diff($given_date);
                  
                  // Format the difference
                  if ($interval->y > 0) {
                      $int = $interval->y . ' years ago';
                  } elseif ($interval->m > 0) {
                      $int = $interval->m . ' months ago';
                  } elseif ($interval->d > 0) {
                      $int = $interval->d . ' days ago';
                  } elseif ($interval->h > 0) {
                      $int = $interval->h . ' hours ago';
                  } elseif ($interval->i > 0) {
                      $int = $interval->i . ' minutes ago';
                  } else {
                      $int = 'Just now';
                  }
                  
                  ?>
               <tr>
                  <td data-label="Trx Type">
                     <div class="text-start text-lg-start">
                        <div class="d-flex">
                           <div>
                              <img src="assets/images/currency/668eb02db15df1720627245.png" class="thumb">                                              
                           </div>
                           &nbsp; &nbsp;
                           <div>
                              <span class="fw-bold"><?=$transaction['type'];?> <span> <?=$transaction['gateway'];?></span></span> 
                              <br>
                              <small> <?=$transaction['created_at'];?></small>
                           </div>
                        </div>
                     </div>
                  </td>
                  <td class="hide--desktop" data-label="Date">
                     <div class="text-start text-lg-start">
                        <?=$transaction['created_at'];?><br><?=$int;?>
                     </div>
                  </td>
                  <td class="text-center" data-label="Amount">
                     <span class="fw-bold  text--warning ">
                     + $ <?=$transaction['amount'];?>
                     </span>
                  </td>
                  <td class="hide--desktop" data-label="Detail"><?=$transaction['type'];?> Via <?=$transaction['gateway'];?></td>
                  <td class="hide--desktop" data-label="status">
                     <div class="text-start text-lg-start">
                        <span type="button" class="badge badge--warning"><?=$transaction['status'];?></span>
                     </div>
                  </td>
                  <td data-label="View">
                     <div class="d-flex justify-content-between w-50%">
                        <div type="button" class="btn btn--primary outline flex-fill btn--sm viewTransaction<?=$transaction['id'];?>" id="transactionButton<?=$transaction['id'];?>">
                           <i class="fa fa-eye"></i> View
                        </div>
                        
                     </div>
                  </td>
               </tr>
               <div class="modal fade" id="transaction<?=$transaction['id'];?>-modal" role="dialog" aria-hidden="true" style="display: none;">
                  <div class="modal-dialog modal-dialog-centered">
                     <div class="modal-content">
                        <div class="modal-header">
                           <h5 class="modal-title" id="transactionModalLabel<?=$transaction['id'];?>">
                              <div class="d-flex">
                                 <div>
                                    <img src="assets/images/currency/668eb02db15df1720627245.png" class="thumbi">                                              
                                 </div>
                                 &nbsp; &nbsp;
                                 <div>
                                    <span class="fw-bold"><?=$transaction['type'];?> <span><?=$transaction['gateway'];?></span></span> 
                                    <br>
                                    <small> <?=$transaction['created_at'];?></small>
                                 </div>
                              </div>
                           </h5>
                           <button type="button" class="close" data-dismiss="modal" aria-label="Close"><i class="fa fa-times"></i></button>
                        </div>
                        <div class="offcanvas-header">
                           <h4 class="mb-0 fs-18 offcanvas-title">
                              <div class="d-flex">
                                 <div>
                                    <span style="font-size: 2rem;">0.005012 <span><?=$transaction['currency'];?></span></span> 
                                    <br>
                                    <small> <?=$transaction['trx_id'];?></small>
                                 </div>
                              </div>
                           </h4>
                        </div>
                        <div class="modal-body">
                           <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Status</span>
                               <span class="text-end">
                                    <span type="button" class="btn btn--sm outline flex-fill btn--warning"><?=$transaction['status'];?></span>
                               </span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Amount</span>
                               <span class="text-end">$<?=$transaction['amount'];?></span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start"> Address</span>
                               <span class="text-end">Company Address</span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Transaction ID</span>
                               <span class="text-end">#<?=$transaction['trx_id'];?></span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Transaction Type</span>
                               <span class="text-end"><?=$transaction['type'];?> Via <?=$transaction['gateway'];?></span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Category Type</span>
                               <span class="text-end"><?=$transaction['type'];?></span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Time/Date</span>
                               <span class="text-end"><?=$transaction['created_at'];?><br><?=$int;?></span>
                            </div>
                        </div>
                     </div>
                  </div>
               </div>
               <div class="offcanvas offcanvas-end p-5" tabindex="-1" id="transaction<?=$transaction['id'];?>-canvas" aria-labelledby="offcanvasLabel" aria-modal="true" role="dialog">
                  <div class="offcanvas-header">
                     <h4 class="mb-0 fs-18 offcanvas-title">
                        <div class="d-flex">
                           <!-- <div>
                              <img src="assets/images/currency/668eb02db15df1720627245.png" class="thumbi">                                              
                           </div> -->
                           &nbsp; &nbsp;
                           <div>
                              <span class="fw-bold"><?=$transaction['type'];?> <span><?=$transaction['gateway'];?></span></span> 
                              <br>
                              <small> <?=$transaction['created_at'];?></small>
                           </div>
                        </div>
                     </h4>
                     <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close" id="close-transaction<?=$transaction['id'];?>-canvas">
                     <i class="fa fa-times"></i>
                     </button>
                  </div>
                  <div class="offcanvas-header">
                     <h4 class="mb-0 fs-18 offcanvas-title">
                        <div class="d-flex">
                           <div>
                              <span style="font-size: 2rem;">0.005012 <span><?=$transaction['currency'];?></span></span> 
                              <br>
                              <small> <?=$transaction['trx_id'];?></small>
                           </div>
                        </div>
                     </h4>
                  </div>
                  <hr>
                  <div class="offcanvas-body">
                     <center>
                        <a type="button" style="color: red;" onclick="if (!window.__cfRLUnblockHandlers) return false; cancelTransaction('deposit/delete/<?=$transaction['trx_id'];?>')"> Cancel <?=$transaction['type'];?>
                        </a>
                     </center>
                     <br>
                     <div class="d-flex justify-content-between m-3 mb-5">
                        <span class="text-start">Status</span>
                        <span class="text-end">
                        <span type="button" class="btn btn--sm outline flex-fill btn--warning"><?=$transaction['status'];?></span>
                        </span>
                     </div>
                     <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Amount</span>
                               <span class="text-end">$<?=$transaction['amount'];?></span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start"> Address</span>
                               <span class="text-end">Company Address</span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Transaction ID</span>
                               <span class="text-end">#<?=$transaction['trx_id'];?></span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Transaction Type</span>
                               <span class="text-end"><?=$transaction['type'];?> Via <?=$transaction['gateway'];?></span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Category Type</span>
                               <span class="text-end"><?=$transaction['type'];?></span>
                            </div>
                            <div class="d-flex justify-content-between m-3 mb-5">
                               <span class="text-start">Time/Date</span>
                               <span class="text-end"><?=$transaction['created_at'];?><br><?=$int;?></span>
                            </div>
                     <hr>
                  </div>
               </div>
               <script type="text/javascript">
                  $(document).ready(function() {
                     function toggleModalAttributes() {
                         if ($(window).width() < 768) { // Assuming mobile view is for width < 768px
                             $('#transactionButton<?=$transaction['id'];?>').attr({
                                 'data-toggle': 'modal',
                                 'data-target': '#transaction<?=$transaction['id'];?>-modal'
                             });
                         } else {
                             $('#transactionButton<?=$transaction['id'];?>').removeAttr('data-toggle data-target');
                         }
                     }
                  
                  // Call the function on page load
                  toggleModalAttributes();
                  
                  // Call the function on window resize
                  $(window).resize(function() {
                  toggleModalAttributes();
                  });
                  });
                  
                  // Ensure the modal close button works
                  $('#transaction<?=$transaction['id'];?>-modal .btn-close').on('click', function() {
                  $('#transaction<?=$transaction['id'];?>-modal').modal('hide');
                  });
               </script>
               <script type="text/javascript">
                  "use strict";
                  (function($) {
                    $(document).ready(function() {
                                              $('.viewTransaction<?=$transaction['id'];?>').on('click', function(e) {
                        e.preventDefault();
                        if (window.innerWidth > 768) { // Mobile view
                          
                          $('#transaction<?=$transaction['id'];?>-canvas').addClass('show');
                        }
                      });
                  
                      
                  
                      $('#close-transaction<?=$transaction['id'];?>-canvas').on('click', function() {
                        $('#transaction<?=$transaction['id'];?>-canvas').removeClass('show');
                      });
                                            });
                  })(jQuery);
                  
                  
                  
                  
               </script>
               <?php }?>
            </tbody>
         </table>
      </div>
   </div>
</div>
<?php include "modals.php";?>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous" type="text/javascript"></script>