<!doctype html>
<html lang="en"  class="w-[640px]">
   <head>
   <?php echo view('user/layout/head');?>
   </head>
   <style>
      .pagination {
      display: flex;
      justify-content: center;
      margin-top: 20px;
      }
      .pagination-btn {
      background: #007bff;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 5px;
      cursor: pointer;
      margin: 0 5px;
      }
      .pagination-btn:disabled {
      background: #6c757d;
      cursor: not-allowed;
      }
      #pageInfo {
      align-self: center;
      margin: 0 10px;
      }
   </style>
   <body class="section-overflow">
      <!--banner start -->
      <section class="banner-bg overall-bg ">
      <?php
        $this->db = db_connect();
        $this->session = session();
        $this->common_model = model('CommonModel');
        $user=$this->session->get('uusersid');
        ?>
         <div class="container">
            <header class="pt-3 pb-3">
               <div class="row">
                  <div class="col-1">
                     <div class="back-arrow">
                        <a href="<?php echo base_url();?>/revenue">
                           <iconify-icon icon="bi:arrow-left-circle-fill"></iconify-icon>
                        </a>
                     </div>
                  </div>
                  <div class="col-8">
                     <div class="title title-top py-2">
                        <h2 class="text-primary aleo" style="font-size:25px !important">Profit Records : <?php echo $datee;?></h2>
                     </div>
                  </div>
                  <div class="col-3">
                     <div class="common-icon d-flex align-items-center justify-content-end">
                        <div class="wallet-icon header-icon me-2">
                           <a href="<?php echo base_url();?>/user/asset" class="text-decoration-none ">
                              <iconify-icon icon="ph:wallet-bold"></iconify-icon>
                           </a>
                        </div>
                        <div class="header-icon">
                           <a href="<?php echo base_url();?>/news" class="text-decoration-none">
                              <iconify-icon icon="mdi:notifications-none"></iconify-icon>
                           </a>
                        </div>
                     </div>
                  </div>
               </div>
            </header>
         </div>
         <section class="dash-wrap coin-wrap">
            <div class="container">
               <div class="row">
                  <div class="col-12" id="myItems">
                  <?php 
                     if(!empty($sell)) { 
                     
                     foreach ($sell as $key => $value) {
                     if($value->avg_price > 0){ 
                         $get_gas=0;
                         if($value->order_id!='')
                         {
                          $description="fuel fee deduction for order no ".$value->order_id;
                         
                          $get_gas_fee=$this->db->query("select * from history where description like '%$description%' and uusersid='".$user."' and type in('deducted_gaswallet','fuel')")->getRow();
                          //$query = $this->db->getLastQuery();
                     //echo (string)$query;
                     
                          if($get_gas_fee)
                          {
                              $get_gas=$get_gas_fee->amount;
                          }
                         }
                         
                        
                     ?>
                     <div class="trade-record">
                        <div class="record-sub-heading d-flex align-items-center">
                           <iconify-icon icon="simple-icons:binance" class="text-primary me-2"></iconify-icon>
                           <h3 class="text-white fw-normal">Order number : <?php if($value->order_id){ echo $value->order_id; }else{ echo '---'; }?>  </h3>
                        </div>
                        <div class="trade-record-content d-flex">
                           <ul class="list-unstyled trade-record-list trade-record-list-one text-start">
                              <li>Exchange</li>
                              <li>Profit</li>
                              <li>Gas Fee</li>
                              <li>Date and Time</li>
                             
                           </ul>
                           <ul class="list-unstyled trade-record-list trade-record-list-two text-end">
                              <li class="text-primary"><?php echo strtoupper($value->pair);?></li>
                              <li><?php echo $value->comm_price;?></li>
                              <li class="text-success"> <?php echo $get_gas;?></li>
                             
                              <li><?php  if($value->time){echo date('Y-m-d H:i:s',(substr($value->time,0,-3)));}else{ echo '---' ;} ?></li>
                           </ul>
                        </div>
                     </div>
                     <?php } }
                     }
                     ?>

                     
                     
                     <!---banner end---->
                  </div>
                  <div class="pagination">
                        <button id="prevPage" class="pagination-btn" onclick="changePage('prev')">« Previous</button>
                        <span id="pageInfo" class="text-white"></span>
                        <button id="nextPage" class="pagination-btn" onclick="changePage('next')">Next »</button>
                      </div>
               </div>
            </div>
         </section>
         <!--footer start -->
         <?php echo view('user/layout/footer');?>
         <!--footer end -->
      </section>
     
      <?php echo view('user/layout/footer-script');?>
      <script>
         AOS.init({
         });
      </script>
       <script src="https://code.jquery.com/jquery-3.7.1.js"></script>
      <script src="https://code.jquery.com/ui/1.13.3/jquery-ui.js"></script>
      <script>
         let currentPage = 1;
         const itemsPerPage = 5; // Number of items per page
         let totalPages =1;
         
         
         document.addEventListener('DOMContentLoaded', function() {
         loadPage(currentPage);
         });
         
         
         
         function loadPage(page)
         {
         
         var uusersid='<?php echo $this->session->get('uusersid');?>';
         $.ajax({
             'method': 'POST',
             'url': '<?php echo base_url();?>/profit_records/<?php echo $datee;?>',
              data:{'uusersid':uusersid,"page":page,"itemsPerPage":itemsPerPage},
             success:function(data)
             {
               
                 var data= JSON.parse(data);
                 // console.log(data.totalPages);
                 // console.log(page);
                 const myItems = document.getElementById('myItems');
                 myItems.innerHTML = '';
         
                 if (data.items.length === 0) {
                     myItems.innerHTML = `<div class="faq_accordian mb-3">
                             <div class="accordion" style="margin:10px;text-align:center;color:red;"> No Records Found</div></div>`;
                 }
                 else
                 {
         
                 data.items.forEach(item => {
                     myItems.innerHTML += ` <div class="trade-record">
                        <div class="record-sub-heading d-flex align-items-center">
                           <iconify-icon icon="simple-icons:binance" class="text-primary me-2"></iconify-icon>
                           <h3 class="text-white fw-normal">Order number :  ${item.orderNumber} </h3>
                        </div>
                        <div class="trade-record-content d-flex">
                           <ul class="list-unstyled trade-record-list trade-record-list-one text-start">
                              <li>Exchange</li>
                              <li>Profit</li>
                              <li>Gas Fee</li>
                              <li>Date and Time</li>
                             
                           </ul>
                           <ul class="list-unstyled trade-record-list trade-record-list-two text-end">
                              <li class="text-primary">${item.exchange}</li>
                              <li>${item.profit}</li>
                              <li class="text-success"> ${item.gas_fee}</li>
                             
                              <li>${item.dateTime}</li>
                           </ul>
                        </div>
                     </div>`; });
                 }

                
            
             totalPages=data.totalPages;
         
             //document.getElementById('total_count').innerHTML=data.totalitems;
              document.getElementById('prevPage').disabled = !data.hasPrev;
                 document.getElementById('nextPage').disabled = !data.hasNext;
                 document.getElementById('pageInfo').innerText = `Page ${page} of ${data.totalPages}`;
             }
           })
         
         
           
         }
              
         function changePage(direction) {
         
         if (direction === 'prev' && currentPage > 1) {
            currentPage--;
         } else if (direction === 'next' && currentPage < totalPages) {
            currentPage++;
         }
         
         loadPage(currentPage);
         }
      </script>
   </body>
</html>
