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

<style>
    /* === ULTRA ADVANCED ADMIN UI === */
    .cp-card {
        border-radius: 15px;
        box-shadow: 0 8px 30px rgba(0,0,0,0.1);
        border: none;
        background: #fff;
        margin-top: 20px;
        margin-bottom: 40px;
    }
    .cp-header {
        background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
        color: white;
        border-radius: 15px 15px 0 0;
        padding: 25px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .cp-header h3 { margin: 0; font-size: 20px; font-weight: 700; color: #fff; letter-spacing: 0.5px; }
    
    .table-custom thead th {
        text-transform: uppercase;
        font-size: 11px;
        font-weight: 800;
        letter-spacing: 1px;
        color: #475569;
        background-color: #f1f5f9;
        border-bottom: 2px solid #e2e8f0 !important;
        padding: 18px 15px;
    }
    .table-custom tbody td {
        vertical-align: middle !important;
        padding: 20px 15px;
        border-bottom: 1px solid #f1f5f9;
        font-size: 14px;
        color: #1e293b;
    }
    .table-custom tbody tr:hover { background-color: #f8fafc; transition: 0.3s; }
    
    .copy-btn { cursor: pointer; color: #3b82f6; font-size: 14px; margin-left: 8px; transition: 0.2s; }
    .copy-btn:hover { color: #2563eb; transform: scale(1.2); }

    /* Premium Badges */
    .badge-soft-success { background-color: #d1fae5; color: #065f46; border: 1px solid #a7f3d0; padding: 6px 12px; border-radius: 6px; }
    .badge-soft-warning { background-color: #fef3c7; color: #92400e; border: 1px solid #fde68a; padding: 6px 12px; border-radius: 6px; }
    .badge-soft-danger { background-color: #fee2e2; color: #991b1b; border: 1px solid #fecaca; padding: 6px 12px; border-radius: 6px; }
    .badge-soft-info { background-color: #e0f2fe; color: #075985; border: 1px solid #bae6fd; padding: 6px 12px; border-radius: 6px; }

    .detail-label { font-size: 10px; text-transform: uppercase; color: #94a3b8; font-weight: 800; display: block; margin-bottom: 2px; }
    .detail-value { font-weight: 700; color: #0f172a; font-size: 14px; }
    
    .cred-box {
        background: #fff1f2;
        border: 1px dashed #fb7185;
        padding: 10px;
        border-radius: 10px;
        font-family: 'Courier New', Courier, monospace;
        font-size: 12.5px;
        margin-top: 8px;
        box-shadow: inset 0 2px 4px rgba(0,0,0,0.02);
    }
    .wa-link { color: #22c55e; font-size: 16px; margin-left: 10px; transition: 0.3s; }
    .wa-link:hover { transform: scale(1.3); color: #16a34a; }
</style>

<div class="container-fluid" style="padding-top: 20px;">
  
  <div class="row mb-4">
    <div class="col-md-12">
        <form action="" method="get" class="form-inline bg-white p-4 rounded shadow-sm d-flex align-items-center" style="gap: 15px; border-left: 5px solid #3b82f6;">
            <div class="form-group">
                <label class="mr-2 font-weight-bold text-muted">Search By:</label>
                <select name="search_type" class="form-control shadow-sm" style="min-width: 160px; border-radius: 8px;">
                    <option value="domain" <?= (isset($_GET['search_type']) && $_GET['search_type'] == 'domain') ? 'selected' : '' ?>>Domain Name</option>
                    <option value="username" <?= (isset($_GET['search_type']) && $_GET['search_type'] == 'username') ? 'selected' : '' ?>>Client Username</option>
                </select>
            </div>
            <div class="form-group">
                <input type="text" name="search" class="form-control shadow-sm" placeholder="Enter keyword..." value="<?= isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>" style="min-width: 300px; border-radius: 8px;">
            </div>
            <button type="submit" class="btn btn-primary px-4 shadow" style="border-radius: 8px; font-weight: 600;"><i class="fa fa-filter mr-2"></i> Apply Filter</button>
            <?php if(isset($_GET['search'])): ?>
                <a href="<?= site_url('admin/child-panels') ?>" class="btn btn-light border px-4" style="border-radius: 8px;">Clear</a>
            <?php endif; ?>
        </form>
    </div>
  </div>

  <div class="row">
    <div class="col-lg-12">
      <div class="cp-card">
        <div class="cp-header">
          <h3><i class="fa fa-shield mr-2"></i> Child Panels Power Manager</h3>
          <span class="badge" style="background: rgba(255,255,255,0.15); font-size: 14px; padding: 8px 16px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.2);">
            <i class="fa fa-database mr-1"></i> Total: <?php echo count($payments); ?>
          </span>
        </div>
        
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-custom mb-0" id="dt">
              <thead>
                <tr>
                  <th>ID & Client Info</th>
                  <th>Panel Credentials</th>
                  <th>Plan Details</th>
                  <th>Timeline & Expiry</th>
                  <th>Status</th>
                  <th class="text-right">Management</th>
                </tr>
              </thead>
              <tbody>
                <?php if(!empty($payments)): ?>
                    <?php foreach($payments as $payment): 
                      // TIME CALCULATION LOGIC
                      $expiry_date = new DateTime($payment['renewal_date']);
                      $now = new DateTime("now");
                      $diff = $now->diff($expiry_date);
                      $days_left = (int)$diff->format('%R%a');
                      
                      // Dynamic Time Badge
                      if($days_left > 15) { $badge_class = 'badge-soft-success'; $icon = 'check-circle'; }
                      elseif($days_left > 0) { $badge_class = 'badge-soft-warning'; $icon = 'clock-o'; }
                      else { $badge_class = 'badge-soft-danger'; $icon = 'exclamation-triangle'; }
                      
                      $time_text = ($days_left > 0) ? $days_left.' Days Remaining' : 'Expired!';
                      $duration = isset($payment['duration_months']) ? $payment['duration_months'] : 1;
                    ?>
                    <tr>
                      <td>
                        <span class="badge" style="background: #0f172a; color: #38bdf8; font-size: 11px; margin-bottom: 8px;">#<?php echo $payment["id"] ?></span>
                        <div class="d-flex align-items-center">
                            <div style="width: 35px; height: 35px; background: #f1f5f9; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin-right: 10px;">
                                <i class="fa fa-user text-primary"></i>
                            </div>
                            <span style="font-weight: 800; font-size: 15px; color: #334155;"><?php echo htmlspecialchars($payment["username"]); ?></span>
                        </div>
                      </td>
                      
                      <td>
                        <span class="detail-label">Access URL:</span>
                        <div class="d-flex align-items-center">
                            <a href="http://<?php echo htmlspecialchars($payment["domain"]); ?>" target="_blank" class="detail-value text-primary" style="text-decoration: none; border-bottom: 1px dashed;">
                                <i class="fa fa-external-link mr-1 small"></i><?php echo htmlspecialchars($payment["domain"]); ?>
                            </a>
                            <a href="https://wa.me/?text=Hello, your panel <?php echo $payment['domain']; ?> is ready." target="_blank" class="wa-link" title="Contact on WhatsApp"><i class="fa fa-whatsapp"></i></a>
                        </div>
                        <div class="cred-box">
                            <div class="mb-1"><span class="text-muted" style="font-size: 10px;">ADMIN:</span> <span style="font-weight: bold; color: #1e293b;"><?php echo htmlspecialchars($payment["child_panel_username"]); ?></span></div>
                            <div>
                                <span class="text-muted" style="font-size: 10px;">PASS:</span> 
                                <span class="text-danger font-weight-bold" id="pass-<?php echo $payment["id"]; ?>"><?php echo htmlspecialchars($payment["child_panel_password"]); ?></span>
                                <i class="fa fa-clone copy-btn" onclick="copyPassword('pass-<?php echo $payment["id"]; ?>')" title="Copy Password"></i>
                            </div>
                        </div>
                      </td>
                      
                      <td>
                        <span class="detail-label">Subscription:</span>
                        <span class="detail-value text-success" style="font-size: 16px;">
                            <?php echo $payment["charged_amount"].' '.$payment["child_panel_currency"]; ?>
                        </span>
                        <div class="mt-2">
                            <span class="badge badge-soft-info" style="font-size: 11px;"><i class="fa fa-calendar-check-o mr-1"></i> <?php echo $duration; ?> Month(s)</span>
                        </div>
                      </td>
                      
                      <td>
                        <span class="detail-label">Expiry Date:</span>
                        <span class="detail-value text-dark" style="font-size: 13px;"><?php echo date("d M, Y", strtotime($payment["renewal_date"])); ?></span>
                        <div class="mt-2">
                            <span class="badge <?php echo $badge_class; ?>" style="font-weight: 700; font-size: 11px;">
                                <i class="fa fa-<?php echo $icon; ?> mr-1"></i> <?php echo $time_text; ?>
                            </span>
                        </div>
                        <small class="text-muted d-block mt-1" style="font-size: 10px;">Ordered: <?php echo date("d-m-y", strtotime($payment["created_on"])); ?></small>
                      </td>
                      
                      <td>
                        <?php if($payment["child_panel_status"] == 'Active'): ?>
                           <span class="badge" style="background: #dcfce7; color: #15803d; border: 1px solid #bbf7d0; padding: 8px 15px; border-radius: 30px; font-weight: 800; font-size: 11px;"><i class="fa fa-circle mr-1 small"></i> ACTIVE</span>
                        <?php elseif($payment["child_panel_status"] == 'Pending'): ?>
                           <span class="badge" style="background: #fef3c7; color: #92400e; border: 1px solid #fde68a; padding: 8px 15px; border-radius: 30px; font-weight: 800; font-size: 11px;"><i class="fa fa-spinner fa-spin mr-1"></i> PENDING</span>
                        <?php else: ?>
                           <span class="badge" style="background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; padding: 8px 15px; border-radius: 30px; font-weight: 800; font-size: 11px;"><i class="fa fa-ban mr-1"></i> <?php echo strtoupper($payment["child_panel_status"]); ?></span>
                        <?php endif; ?>
                      </td>
                      
                      <td class="text-right">
                        <div class="dropdown">
                          <button type="button" class="btn btn-light border btn-sm dropdown-toggle shadow-sm" data-toggle="dropdown" style="border-radius: 8px; font-weight: 700; padding: 8px 15px;">
                            <i class="fa fa-cog mr-1"></i> Manage
                          </button>
                          <ul class="dropdown-menu dropdown-menu-right" style="border-radius: 12px; border: none; box-shadow: 0 10px 30px rgba(0,0,0,0.15); padding: 10px; min-width: 180px;">
                            <li class="dropdown-header" style="font-size: 10px; font-weight: 900; color: #94a3b8; text-transform: uppercase;">Control Status</li>
                            <li><a href="<?php echo site_url("admin/child-panels/".$payment["id"]."/activate");?>" class="py-2"><i class="fa fa-check-circle text-success mr-2"></i> Activate Panel</a></li>
                            <li><a href="<?php echo site_url("admin/child-panels/".$payment["id"]."/suspend");?>" class="py-2"><i class="fa fa-pause-circle text-warning mr-2"></i> Suspend Panel</a></li>
                            <li role="separator" class="divider my-2"></li>
                            <li><a href="<?php echo site_url("admin/child-panels/".$payment["id"]."/delete");?>" onclick="return confirm('Danger! Delete this panel forever?');" class="py-2 text-danger"><i class="fa fa-trash mr-2"></i> Remove Record</a></li>
                          </ul>
                        </div>
                      </td>
                    </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="6" class="text-center py-5">
                            <i class="fa fa-folder-open-o fa-4x text-light mb-3"></i>
                            <h4 class="text-muted">No child panels match your criteria!</h4>
                            <p class="text-muted small">Try adjusting your filters or check back later.</p>
                        </td>
                    </tr>
                <?php endif; ?>
              </tbody>
            </table>
          </div>
          
          <?php if(isset($paginationArr["count"]) && $paginationArr["count"] > 1 ): ?>
             <div class="p-4 border-top">
                   <ul class="pagination pull-right mb-0">
                      <?php if( $paginationArr["current"] != 1 ): ?>
                         <li class="prev"><a href="<?php echo site_url("admin/child-panels/1/".$search_link) ?>"><i class="fa fa-angle-double-left"></i></a></li>
                         <li class="prev"><a href="<?php echo site_url("admin/child-panels/".$paginationArr["previous"]."/".$search_link) ?>"><i class="fa fa-angle-left"></i></a></li>
                      <?php endif; ?>
                      
                      <?php for ($page=1; $page<=$paginationArr["count"]; $page++): 
                               $active = ($paginationArr["current"] == $page) ? 'class="active"' : '';
                      ?>
                         <li <?php echo $active; ?>><a href="<?php echo site_url("admin/child-panels/".$page."/".$search_link) ?>"><?php echo $page; ?></a></li>
                      <?php endfor; ?>
                      
                      <?php if( $paginationArr["current"] != $paginationArr["count"] ): ?>
                         <li class="next"><a href="<?php echo site_url("admin/child-panels/".$paginationArr["next"]."/".$search_link) ?>"><i class="fa fa-angle-right"></i></a></li>
                         <li class="next"><a href="<?php echo site_url("admin/child-panels/".$paginationArr["count"]."/".$search_link) ?>"><i class="fa fa-angle-double-right"></i></a></li>
                      <?php endif; ?>
                   </ul>
                   <div class="clearfix"></div>
             </div>
          <?php endif; ?>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
function copyPassword(elementId) {
    var text = document.getElementById(elementId).innerText;
    var elem = document.createElement("textarea");
    document.body.appendChild(elem);
    elem.value = text;
    elem.select();
    document.execCommand("copy");
    document.body.removeChild(elem);
    alert("Copied to clipboard: " + text);
}
</script>

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