//index.php
<!DOCTYPE html>
<?php 
include ('includes/header.php');?>
<html>
<head>
    <meta charset="utf-8">
    <title>ADD PlayList</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-12">
            <form>
              <a href="add.php" class="btn btn-primary">Add</a>
            </form>
            
            <form method="get" action="search.php">
              <input type="text" placeholder="Search...Dev ID / Dev cod / Name" name="jindex" id="jindex">
              <button class='btn btn-success btn-sm' type="submit">Search</button>
            </form>
            <table class="table table-striped table-sm" style="margin-top:20px;">
                <thead style="color:white!important">
                    <th>Plalist Name</th>
                    <th>DNS</th>
                    <th>Device ID</th>
                    <th>Device Code</th>
                    <th>UserName</th>
                    <th>Password</th>
                    <th>Expire Date</th>
                    <th>Action</th>
                </thead>
                <tbody>
                    <?php
                        //fetch data from json
                        $data = file_get_contents('api/RTXPlalist.json');
                        //decode into php array
                        $data = json_decode($data);
                        
                        
                        $max_per_page = 10;
                        $current_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
                        $total_pages = ceil(count($data->rows) / $max_per_page);
                        
                        $start_index = ($current_page - 1) * $max_per_page;
                        $end_index = $start_index + $max_per_page - 1;
                        if ($end_index >= count($data->rows)) {
                            $end_index = count($data->rows) - 1;
                        }

                        // Get the data for the current page
                        $current_page_data = array_slice($data->rows, $start_index, $max_per_page);
                        
                        
                        
                    
                        
                        /*function decr($data) {
                            $key = 'zsdfkghgujkfdsjgklsdfbjghsdfkjgb';
                            $data = base64_decode($data);
                            $iv = '0123456789abcdef';
                            $decrypted = openssl_decrypt($data, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
                            return $decrypted;
                        }*/
                        
                        function decr($encrypted_hex) {
                            $key = 'zsdfkghgujkfdsjgklsdfbjghsdfkjgb';
                            $encrypted = hex2bin($encrypted_hex);
                            $decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, '0123456789abcdef');
                            return $decrypted;
                        }


 
                        $index = 0;
                        foreach($current_page_data as $row){
                            $decryptedName = decr($row->name);
                            $decrypteddns = decr($row->dns);
                            $decrypteddevid = decr($row->devid);
                            $decrypteddevcode = decr($row->devcode);
                            $decryptedusername = decr($row->username);
                            $decryptedpassword = decr($row->password);
                            $decryptedexpire = decr($row->expire);
                            echo "
                                <tr>
                                    <td>".$decryptedName."</td>
                                    <td>".$decrypteddns."</td>
                                    <td>".$decrypteddevid."</td>
                                    <td>".$decrypteddevcode."</td>
                                    <td>".$decryptedusername."</td>
                                    <td>".$decryptedpassword."</td>
                                    <td>".$decryptedexpire."</td>
                                    <td>
                                        <a href='edit.php?index=".$index."' class='btn btn-success btn-sm'>Edit</a>
                                        <a href='delete.php?index=".$index."' class='btn btn-danger btn-sm'>Delete</a>
                                    </td>
                                </tr>
                            ";
 
                            $index++;
                        }
                        // Display the pagination links
                        // Display the pagination links
                        // Display the pagination links
                        // Display the pagination links
                        if ($total_pages > 1) {
                            echo '<div style="background-color: rgba(0, 0, 255, 0.5); color: white; padding: 5px; border: 1px solid white; border-radius: 8px;">';
                            if ($current_page > 1) {
                                echo '<a href="?page=' . ($current_page - 1) . '" style="margin-right: 10px;">Previous</a>';
                            }
                            $num_links = 10; // Number of links to show
                            $start_link = max($current_page - floor($num_links / 2), 1);
                            $end_link = min($start_link + $num_links - 1, $total_pages);
                            for ($i = $start_link; $i <= $end_link; $i++) {
                                if ($i == $current_page) {
                                    echo '<span style="margin-right: 10px;">' . $i . '</span>';
                                } else {
                                    echo '<a href="?page=' . $i . '" style="margin-right: 10px;">' . $i . '</a>';
                                }
                            }
                            if ($current_page < $total_pages) {
                                echo '<a href="?page=' . ($current_page + 1) . '" style="margin-left: 10px;">Next</a>';
                            }
                            echo '</div>';
                        }



                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php include ('includes/footer.php');?>
</body>
</html>