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

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

    <div class="copuloo-card">
        <h2 class="copuloo-title">Released Phone Numbers</h2>
        <p class="copuloo-description">When switching or removing a phone number from the Copuloo 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="copuloo-description">This page displays all released phone numbers across all WordPress accounts and allows admins to manage them.</p>
            <div class="copuloo-table-container">
                <table class="copuloo-table">
                    <thead>
                        <tr>
                            <th>Phone Number</th>
                            <th>User</th>
                            <th class="actions">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($copuloo_records as $copuloo_record) :
                        ?>
                            <tr>
                                <td><?php echo esc_html($copuloo_record->countrycode . ' ' . copuloo_formatNumber($copuloo_record->phone)); ?></td>
                                <td><?php echo esc_html(get_user_by( 'id', $copuloo_record->user )->user_login); ?></td>
                                <td class="actions">
                                    <button onclick="window.location.href='<?php echo esc_url(admin_url('admin.php?page=copuloo_remove&phonenumbercode='.$copuloo_record->countrycode.'&phonenumber='.$copuloo_record->phone))?>'" type="button" class="copuloo-button copuloo-primary"><span class="dashicons dashicons-trash"></span></button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
    </div>
</div>