<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php
global $wpdb;
$dialbunny_records = $wpdb->get_results("SELECT id, countrycode, phone, user FROM {$wpdb->prefix}dialbunny_released_numbers LIMIT 100"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
?>

<div class="dialbunny-admin-wrap">
    <?php if (empty($dialbunny_records)) { echo wp_kses_post('<div class="dialbunny-notice dialbunny-notice-info"><p><strong>No Released Phone Numbers Were Found!</strong> WordPress users can release phone numbers from the DialBunny dashboard.</p><button class="dialbunny-notice-dismiss">Dismiss</button></div>'); }  ?>

    <div class="dialbunny-card">
        <h2 class="dialbunny-title">Released Phone Numbers</h2>
        <p class="dialbunny-description">When switching or removing a phone number from the DialBunny call center dashboard, the old number becomes released. Released numbers are not in use but still incur a monthly charge from Twilio.</p>
        <p class="dialbunny-description">This page displays all released phone numbers across all WordPress accounts and allows admins to manage them.</p>
            <div class="dialbunny-table-container">
                <table class="dialbunny-table">
                    <thead>
                        <tr>
                            <th>Phone Number</th>
                            <th>User</th>
                            <th class="actions">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($dialbunny_records as $dialbunny_record) :
                        ?>
                            <tr>
                                <td><?php echo esc_html($dialbunny_record->countrycode . ' ' . dialbunny_formatNumber($dialbunny_record->phone)); ?></td>
                                <td><?php echo esc_html(get_user_by( 'id', $dialbunny_record->user )->user_login); ?></td>
                                <td class="actions">
                                    <button onclick="window.location.href='<?php echo esc_url(admin_url('admin.php?page=dialbunny_remove&phonenumbercode='.$dialbunny_record->countrycode.'&phonenumber='.$dialbunny_record->phone))?>'" type="button" class="dialbunny-button dialbunny-primary"><span class="dashicons dashicons-trash"></span></button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
    </div>
</div>