<?php include 'header.php'; ?>
<style>
/* Glassy Badges */
.badge-glass-success { background-color: rgba(18, 184, 132, 0.15); color: #109f72; border: 1px solid rgba(18, 184, 132, 0.3); padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 500;}
.badge-glass-info { background-color: rgba(86, 204, 242, 0.15); color: #007bb5; border: 1px solid rgba(86, 204, 242, 0.3); padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 500;}
.badge-glass-warning { background-color: rgba(242, 201, 76, 0.15); color: #b28900; border: 1px solid rgba(242, 201, 76, 0.3); padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 500;}
.badge-glass-danger { background-color: rgba(235, 87, 87, 0.15); color: #c9302c; border: 1px solid rgba(235, 87, 87, 0.3); padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 500;}
.badge-glass-primary { background-color: rgba(47, 128, 237, 0.15); color: #0056b3; border: 1px solid rgba(47, 128, 237, 0.3); padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 500;}
.badge-glass-secondary { background-color: rgba(108, 117, 125, 0.15); color: #495057; border: 1px solid rgba(108, 117, 125, 0.3); padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 500;}

/* Table Card */
.custom-table-card {
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.05);
    border: none;
    margin-top: 15px;
}
.custom-table-card .table {
    margin-bottom: 0;
}
.custom-table-card .table thead th {
    border-bottom: 2px solid #f0f0f0;
    color: #444;
    font-weight: 600;
    background: #fdfdfd;
}
.custom-table-card .table tbody tr {
    transition: background-color 0.2s;
}
.custom-table-card .table tbody tr:hover {
    background-color: #f8f9fa;
}

/* Dark Theme Adjustments */
body.dark-mode .custom-table-card,
body.dark .custom-table-card {
    background: #2a2c33;
    box-shadow: 0 4px 20px rgba(0,0,0,0.2);
}
body.dark-mode .custom-table-card .table thead th,
body.dark .custom-table-card .table thead th {
    background: #31343d;
    color: #fff;
    border-bottom-color: #3f424c;
}
body.dark-mode .custom-table-card .table tbody tr:hover,
body.dark .custom-table-card .table tbody tr:hover {
    background-color: #353842;
}
body.dark-mode .table,
body.dark .table {
    border-color: #3f424c !important;
}
</style>

<div class="container-fluid">
    <h4 style="margin-bottom: 15px; font-weight: 600;"><i class="bi bi-graph-up"></i> Statistics / Earn</h4>

    <div class="card custom-table-card">
        <div class="card-body" style="padding: 0;">
            <table class="table" id="dt" style="margin-bottom: 0;">
                <thead>
                    <tr>
                        <th class="p-l">ID</th>
                        <th>Username</th>
                        <th>Affiliate Link</th>
                        <th>Note</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <form id="changebulkForm" method="post">
                    <tbody>
                        <?php if(!empty($earn) && (is_array($earn) || is_object($earn))): ?>
                        <?php foreach($earn as $earnItem ): ?>
                        <tr>
                            <td class="p-l" style="font-weight:500;">#<?php echo $earnItem["earn_id"] ?></td>
                            <td><?php echo isset($user["username"]) ? $user["username"] : 'User' ?></td>
                            <td><a class="text-primary" target="_blank" href="<?php echo $earnItem["link"] ?>"><i class="bi bi-link-45deg"></i> <?php echo $earnItem["link"] ?></a></td>
                            <td><?php echo isset($earnItem["earn_note"]) ? $earnItem["earn_note"] : '' ?></td>
                            <td>
                                <?php 
                                    $stClass = 'secondary';
                                    if(isset($earnItem['status'])) {
                                        if($earnItem['status'] == 'Available' || $earnItem['status'] == 'Funds Granted') $stClass = 'success';
                                        if($earnItem['status'] == 'Under Review') $stClass = 'warning';
                                        if($earnItem['status'] == 'Rejected') $stClass = 'danger';
                                    }
                                ?>
                                <span class="badge badge-glass-<?= $stClass ?>"><?php echo isset($earnItem["status"]) ? $earnItem["status"] : '' ?></span>
                            </td>

                            <td>
                                <div class="dropdown pull-right">
                                    <button type="button" class="btn badge-glass-primary btn-xs dropdown-toggle" style="border-radius: 20px; padding: 4px 12px; font-weight: 600;" data-toggle="dropdown">Action <span class="caret"></span></button>
                                    <ul class="dropdown-menu">
                                        <li><a href="#" data-toggle="modal" data-target="#confirmChange" data-href="<?=site_url("admin/earn/earn_review/".$earnItem["earn_id"])?>">Under Review</a></li>
                                        <li><a href="#" data-toggle="modal" data-target="#modalDiv" data-action="earn_note" data-id="<?php echo $earnItem["earn_id"] ?>">Edit Note</a></li>
                                        <li><a href="#" data-toggle="modal" data-target="#confirmChange" data-href="<?=site_url("admin/earn/earn_grant/".$earnItem["earn_id"])?>">Funds Granted</a></li>
                                        <li><a href="#" data-toggle="modal" data-target="#confirmChange" data-href="<?=site_url("admin/earn/earn_reject/".$earnItem["earn_id"])?>">Reject</a></li>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php else: ?>
                        <tr>
                            <td colspan="6" class="text-center" style="padding: 40px; color: #999; font-size: 16px;">
                                <i class="bi bi-inbox" style="font-size: 40px; display: block; margin-bottom: 10px;"></i>
                                No data found
                            </td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                    <input type="hidden" name="bulkStatus" id="bulkStatus" value="0">
                </form>
            </table>
        </div>
    </div>
</div>

<div class="modal modal-center fade" id="confirmChange" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static">
   <div class="modal-dialog modal-dialog-center" role="document">
      <div class="modal-content">
         <div class="modal-body text-center">
            <h4>Are you sure you want to update the status?</h4>
            <div align="center">
               <a class="btn btn-primary" href="" id="confirmYes">Yes</a>
               <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
            </div>
         </div>
      </div>
   </div>
</div>

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