<?php 
include ('includes/header.php');

$table_name = "ibof";
$page = "testes.php";

// Delete a demo
if(isset($_GET['delete'])){
    $db->delete($table_name, 'id = :id',[':id' => $_GET['delete']]);
    echo "<script>window.location.href='".$page."?status=2'</script>";
}

// 1. Get the list of all your DNS to cross-reference with the demos
$dns_list = $db->select('dns', 'id, title, url', '', '');
$dns_map = [];
if (!empty($dns_list)) {
    foreach ($dns_list as $d) {
        $dns_map[$d['id']] = $d['title'] . ' <br><small class="text-muted">' . $d['url'] . '</small>';
    }
}

// 2. Get only the records that are demos (demo_start > 0)
$demos = $db->select($table_name, '*', 'demo_start > 0', 'id DESC');

?>

<div class="container-fluid pt-4 px-4">
    <div class="row g-4">
        <div class="col-12">
            <div class="bg-secondary rounded h-100 p-4">
                <h2 class="mb-4">Generated Auto Demos</h2>
                
                <div class="table-responsive">
                    <table class="table text-center align-middle">
                        <thead>
                            <tr>
                                <th># ID</th>
                                <th>Encrypted MAC</th>
                                <th>IPTV Username</th>
                                <th>Server (DNS)</th>
                                <th>Start Date</th>
                                <th>Duration / Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            if (!empty($demos)) {
                                foreach ($demos as $row) {
                                    $id = $row['id'];
                                    $mac = $row['macad'];
                                    $user = $row['username'];
                                    $pass = $row['password'];
                                    $plid = $row['plid'];
                                    
                                    // Identify DNS
                                    $servidor_dns = isset($dns_map[$plid]) ? $dns_map[$plid] : '<span class="text-danger">Deleted/Unknown DNS</span>';
                                    
                                    // Time calculations
                                    $start = intval($row['demo_start']);
                                    $duration = intval($row['demo_duration']);
                                    $end = $start + $duration;
                                    
                                    // Date format
                                    $start_date = date('d/m/Y', $start);
                                    
                                    // Generic duration text
                                    if ($duration == 3600) {
                                        $texto_duracion = "1 Hour";
                                    } elseif ($duration == 10800) {
                                        $texto_duracion = "3 Hours";
                                    } elseif ($duration == 21600) {
                                        $texto_duracion = "6 Hours";
                                    } elseif ($duration == 86400) {
                                        $texto_duracion = "24 Hours";
                                    } else {
                                        $texto_duracion = round($duration / 3600, 1) . " Hours";
                                    }
                                    
                                    // Visual status
                                    $is_blocked = (substr($pass, -1) === '_');
                                    $now = time();
                                    
                                    if ($is_blocked) {
                                        $status_badge = "<span class='badge bg-danger' style='font-size: 13px;'>Blocked</span>";
                                    } else if ($now > $end) {
                                        $status_badge = "<span class='badge bg-warning' style='font-size: 13px;'>Expired</span>";
                                    } else {
                                        $status_badge = "<span class='badge bg-success' style='font-size: 13px;'>Active</span>";
                                    }
                            ?>
                                <tr>
                                    <td><?= $id; ?></td>
                                    <td style="font-family: monospace; color: #ffc107;"><?= $mac; ?></td>
                                    <td><?= $user; ?></td>
                                    <td style="color: #add8e6;"><?= $servidor_dns; ?></td>
                                    <td><?= $start_date; ?></td>
                                    <td>
                                        <strong><?= $texto_duracion; ?></strong><br>
                                        <div class="mt-2"><?= $status_badge; ?></div>
                                    </td>
                                    <td>
                                        <a class="btn btn-square btn-primary m-2" href="<?php echo $page; ?>?delete=<?php echo $id; ?>" onclick="return confirm('Are you sure you want to delete this Demo record?');">
                                            <i class="fa fa-trash"></i>
                                        </a>
                                    </td>
                                </tr>
                            <?php 
                                } 
                            } else {
                            ?>
                                <tr>
                                    <td colspan="7" class="py-5">There are currently no generated demos.</td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>

            </div>
        </div>
    </div>
</div>
<?php include ('includes/footer.php');?>