<?php include 'header.php'; ?>

<div class="container-fluid">
   <div class="row">
      <div class="col-lg-12">
         
         <!-- 🔍 Search Box Card -->
         <div class="panel panel-default" style="margin-bottom: 20px;">
            <div class="panel-body">
               <form class="form-inline pull-right" action="<?=site_url("admin/refill")?>" method="get">
                  <div class="input-group">
                     <input type="text" name="search" class="form-control" value="<?=$search_word?>" placeholder="Search...">
                     <span class="input-group-btn">
                        <select class="form-control search-select" name="search_type" style="width: auto; border-radius: 0;">
                           <option value="refill_apiid" <?php if( $search_where == "refill_apiid" ): echo 'selected'; endif; ?> >Refill ID</option>
                           <option value="order_id" <?php if( $search_where == "order_id" ): echo 'selected'; endif; ?> >Order ID</option>
                        </select>
                        <button type="submit" class="btn btn-default"><span class="fa fa-search" aria-hidden="true"></span></button>
                     </span>
                  </div>
               </form>
            </div>
         </div>

         <!-- 📋 Refill Data Table Card -->
         <div class="panel panel-default">
            <div class="panel-heading">
               <h3 class="panel-title"><i class="bi bi-arrow-repeat"></i> Refill Management</h3>
            </div>
            
            <!-- 🔥 Table Responsive Container (Fixes Mobile UI) -->
            <div class="table-responsive">
               <table class="table table-striped table-bordered table-hover" style="margin-bottom: 0;">
                  <thead>
                     <tr>
                        <th>ID</th>
                        <th>Username</th>
                        <th>Order ID</th>
                        <th>Link</th>
                        <th>Service Name</th>
                        <th>Date</th>
                        <th>Status</th>
                        <th class="text-right">Action</th>
                     </tr>
                  </thead>
                  <tbody>
                     <?php foreach( $refills as $refill ): ?>
                     <tr>
                        <!-- Refill ID -->
                        <td> 
                           <?php echo $refill["id"] ?>
                           <?php if( $refill["refill_apiid"] != 0 ): echo '<br><span class="label label-default" style="font-size: 10px;">API: '.$refill["refill_apiid"].'</span>'; endif; ?>
                        </td>
                         
                        <!-- Username -->
                        <td>
                           <?php echo $refill["username"]; ?>
                           <?php if( $refill["refill_where"] == "api" ): echo '<br><span class="label label-primary" style="font-size: 10px;">API</span>'; endif; ?> 
                        </td>
         
                        <!-- Order ID -->
                        <td>
                           <a target="_blank" href="<?php echo site_url("admin/orders?search=".$refill["order_id"]."&search_type=order_id"); ?>"><?php echo $refill["order_id"]; ?></a>
                           <?php if( $refill["order_apiid"] != 0 ): echo '<br><span class="label label-default" style="font-size: 10px;">API: '.$refill["order_apiid"].'</span>'; endif; ?>
                        </td>
                             
                        <!-- Link (Shortened for UI fix) -->
                        <td>
                           <a target="_blank" href="<?php echo $refill["order_url"]; ?>" title="<?php echo $refill["order_url"]; ?>" style="display:inline-block; max-width:120px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">
                              <i class="fa fa-external-link"></i> <?php echo $refill["order_url"]; ?>
                           </a>
                        </td>
                        
                        <!-- Service Name -->
                        <td><?php echo $refill["service_name"]; ?></td>
                        
                        <!-- Date -->
                        <td style="white-space: nowrap;"><?php echo date("Y-m-d H:i", strtotime($refill["creation_date"])); ?></td>
                        
                        <!-- Status Badge -->
                        <td>
                           <?php 
                               $st = strtolower($refill["refill_status"]);
                               $badge = "label-default";
                               if($st == "completed") $badge = "label-success";
                               elseif($st == "inprogress" || $st == "refilling") $badge = "label-info";
                               elseif($st == "pending") $badge = "label-warning";
                               elseif($st == "rejected" || $st == "error" || $st == "failed") $badge = "label-danger";
                           ?>
                           <span class="label <?php echo $badge; ?>"><?php echo ucfirst($refill["refill_status"]); ?></span>
                        </td>
                        
                        <!-- Actions Dropdown -->
                        <td class="text-right">
                           <div class="dropdown">
                             <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>
                             <ul class="dropdown-menu dropdown-menu-right">
                                <li><a href="#" data-toggle="modal" data-target="#confirmChange" data-href="<?=site_url("admin/refill/refill_refilling/".$refill["id"])?>"><i class="fa fa-spinner text-info"></i> Mark as Refilling</a></li>
                                <li><a href="#" data-toggle="modal" data-target="#confirmChange" data-href="<?=site_url("admin/refill/refill_complete/".$refill["id"])?>"><i class="fa fa-check text-success"></i> Mark as Complete</a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="#" data-toggle="modal" data-target="#confirmChange" data-href="<?=site_url("admin/refill/refill_reject/".$refill["id"])?>"><i class="fa fa-times text-danger"></i> Mark as Reject</a></li>
                                <li><a href="#" data-toggle="modal" data-target="#confirmChange" data-href="<?=site_url("admin/refill/refill_Error/".$refill["id"])?>"><i class="fa fa-exclamation-triangle text-danger"></i> Mark as Error</a></li>
                             </ul>
                           </div>
                        </td>
                     </tr>
                     <?php endforeach ?>
                  </tbody>
               </table>
            </div>
         </div>

         <!-- 🔢 Pagination -->
         <?php if( $paginationArr["count"] > 1 ): ?>
         <div class="row">
            <div class="col-sm-12 text-center">
               <nav>
                  <ul class="pagination" style="margin-top: 0;">
                    <?php if( $paginationArr["current"] != 1 ): ?>
                     <li class="prev"><a href="<?php echo site_url("admin/refill/1/".$status.$search_link) ?>">&laquo;</a></li>
                     <li class="prev"><a href="<?php echo site_url("admin/refill/".$paginationArr["previous"]."/".$status.$search_link) ?>">&lsaquo;</a></li>
                     <?php
                         endif;
                         for ($page=1; $page<=$pageCount; $page++):
                           if( $page >= ($paginationArr['current']-9) and $page <= ($paginationArr['current']+9) ):
                     ?>
                     <li class="<?php if( $page == $paginationArr["current"] ): echo "active"; endif; ?> "><a href="<?php echo site_url("admin/refill/".$page."/".$status.$search_link) ?>"><?=$page?></a></li>
                     <?php endif; endfor;
                           if( $paginationArr["current"] != $paginationArr["count"] ):
                     ?>
                     <li class="next"><a href="<?php echo site_url("admin/refill/".$paginationArr["next"]."/".$status.$search_link) ?>" data-page="1">&rsaquo;</a></li>
                     <li class="next"><a href="<?php echo site_url("admin/refill/".$paginationArr["count"]."/".$status.$search_link) ?>" data-page="1">&raquo;</a></li>
                     <?php endif; ?>
                  </ul>
               </nav>
            </div>
         </div>
         <?php endif; ?>

      </div>
   </div>
</div>

<!-- ⚙️ Action Confirmation Modal -->
<div class="modal fade" id="confirmChange" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static">
   <div class="modal-dialog" role="document">
      <div class="modal-content">
         <div class="modal-body text-center" style="padding: 30px;">
            <h4 style="margin-bottom: 20px;">Are you sure you want to update the status?</h4>
            <div align="center">
               <a class="btn btn-primary" href="" id="confirmYes" style="padding: 6px 20px;">Yes, Update</a>
               <button type="button" class="btn btn-default" data-dismiss="modal" style="padding: 6px 20px;">Cancel</button>
            </div>
         </div>
      </div>
   </div>
</div>

<!-- jQuery Script to handle Modal Links dynamically -->
<script>
document.addEventListener("DOMContentLoaded", function() {
    $('#confirmChange').on('show.bs.modal', function(e) {
        $(this).find('#confirmYes').attr('href', $(e.relatedTarget).data('href'));
    });
});
</script>

<?php include 'footer.php'; ?>