<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php if ( !isset($_GET['user_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['user_nonce'])), 'Copuloo') ) {} ?>
<div class="copuloo-admin-wrap">
<?php if (isset($_GET['error1'])) { echo wp_kses_post('<div class="copuloo-notice copuloo-notice-error"><p><strong>Error Removing Phone Number!</strong> The phone number that was entered was either invalid or does not exist.</p><button class="copuloo-notice-dismiss">Dismiss</button></div>'); }  ?>
<?php if (isset($_GET['success'])) { echo wp_kses_post('<div class="copuloo-notice copuloo-notice-success"><p><strong>Phone Number Successfully Removed!</strong> You have successfully deleted the phone number.</p><button class="copuloo-notice-dismiss">Dismiss</button></div>'); }  ?>
    
    <div class="copuloo-card">
        <h2 class="copuloo-title">Remove A Managed Number</h2>
        <p class="copuloo-description">Here you can easily permanently remove a managed number from the Copuloo plugin and a user's account. This works for both released phone numbers and active phone numbers.</p>
    </div>

    <div class="copuloo-card">
        <h2 class="copuloo-title">Manually Removing Phone Numbers</h2>
        <p class="copuloo-description">Follow these steps to manually remove a phone number from a user's account:</p>
        <ol class="copuloo-list">
            <li>Go to your Twilio account.</li>
            <li>Find the number you want to remove under Active Numbers and remove it from your Twilio account to avoid paying for an unused number.</li>
            <li>Make sure the phone number in the form below is the correct number you want to remove.</li>
        </ol>
    </div>

    <div class="copuloo-card">
        <h2 class="copuloo-title">Remove Number</h2>
        <form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
            <?php wp_nonce_field('Copuloo', 'user_nonce'); ?>
            <input type="hidden" name="action" value="copuloo_remove_managed_number">
            <div class="copuloo-form-group">
                <label for="countrycode" class="copuloo-label">Country Code</label>
                <div class="copuloo-input-group">
                    <?php 
                    if (isset($_GET['phonenumbercode'])) { echo copuloo_country_code(esc_attr(sanitize_text_field(wp_unslash($_GET['phonenumbercode']))), 'countrycode'); } else { echo copuloo_country_code('none', 'countrycode'); } // phpcs:ignore WordPress.Security.EscapeOutput
                    ?>
                </div>
            </div>
            <div class="copuloo-form-group">
                <label for="phone" class="copuloo-label">Phone Number</label>
                <input required type="tel" value="<?php if (isset($_GET['phonenumber'])) { echo esc_attr(sanitize_text_field(wp_unslash($_GET['phonenumber']))); } ?>" name="phone" id="phone" inputmode="numeric" pattern="[0-9]+" placeholder="Enter phone number" class="copuloo-input">
            </div>
            <div class="copuloo-form-group">
                <button type="submit" class="copuloo-button copuloo-button-danger">Remove Number</button>
                <p class="copuloo-help-text">
                    <strong>Warning:</strong> This will <strong>permanently remove</strong> the managed phone number from the user's account.
                </p>
            </div>
        </form>
    </div>
</div>