<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php if ( !isset($_GET['user_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['user_nonce'])), 'DialBunny') ) {} ?>
<div class="dialbunny-admin-wrap">
<?php if (isset($_GET['error1'])) { echo wp_kses_post('<div class="dialbunny-notice dialbunny-notice-error"><p><strong>Error Adding Phone Number!</strong> Either the phone number or username that was entered was invalid.</p><button class="dialbunny-notice-dismiss">Dismiss</button></div>'); }  ?>
<?php if (isset($_GET['success'])) { echo wp_kses_post('<div class="dialbunny-notice dialbunny-notice-success"><p><strong>Phone Number Successfully Added!</strong> You have successfully added the phone number.</p><button class="dialbunny-notice-dismiss">Dismiss</button></div>'); }  ?>
 
    <div class="dialbunny-card">
        <h2 class="dialbunny-title">Add A Managed Number From Twilio</h2>
        <p class="dialbunny-description">Phone numbers can be quickly and easily obtained through the DialBunny dashboard on the Find a Number page. However if you already have a Twilio number or prefer to purchase one directly from Twilio, WordPress administrators can use this page to manually import and manage those numbers.</p>
    </div>

    <div class="dialbunny-card">
        <h2 class="dialbunny-title">Manually Adding Phone Numbers</h2>
        <p class="dialbunny-description">Follow these steps to manually add a phone number to a WordPress user's account:</p>
        <ol class="dialbunny-list">
            <li>Go to your Twilio account.</li>
            <li>Purchase the requested phone number.</li>
            <li>Under Voice Configuration, set the Configure with field to your TwiML app created during setup.</li>
            <li>Under Messaging Configuration, set the Configure with field to your TwiML app created during setup.</li>
            <li>Ensure the webhooks are set to HTTP-POST request.</li>
            <li>Enter the full phone number in the form below to complete the setup.</li>
        </ol>
    </div>

    <div class="dialbunny-card">
        <h2 class="dialbunny-title">Manually Add A Managed Number</h2>
        <p class="dialbunny-description">When added, this phone number will become the user's active phone number and replace any existing one.</p>
        <form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
            <?php wp_nonce_field('DialBunny', 'user_nonce'); ?>
            <input type="hidden" name="action" value="dialbunny_add_managed_number">
            <div class="dialbunny-form-group">
                <label for="countrycode" class="dialbunny-label">Country Code</label>
                <div class="dialbunny-input-group">
                    <?php
                    // Escaped inside the dialbunny_country_code() function
                    echo dialbunny_country_code('none', 'countrycode'); // phpcs:ignore WordPress.Security.EscapeOutput
                    ?>
                </div>
            </div>
            <div class="dialbunny-form-group">
                <label for="phone" class="dialbunny-label">Phone Number</label>
                <input required type="tel" name="phone" id="phone" inputmode="numeric" pattern="[0-9]+" placeholder="Enter phone number" class="dialbunny-input">
            </div>
            <div class="dialbunny-form-group">
                <label for="phone" class="dialbunny-label">Phone Number SID Key</label>
                <input required type="text" name="phonenumbersid" id="phonenumbersid" placeholder="Enter the Twilio phone number SID key" class="dialbunny-input">
            </div>
            <div class="dialbunny-form-group">
                <label for="user" class="dialbunny-label">WordPress User ID or Username</label>
                <input required type="text" name="user" id="user" placeholder="Enter user ID or username" class="dialbunny-input">
            </div>
            <div class="dialbunny-form-group">
                <button type="submit" class="dialbunny-button dialbunny-button-primary">Add Number</button>
                <p class="dialbunny-help-text">Refer to the "Information" tab to understand the process for assigning a managed phone number to a user.</p>
            </div>
        </form>
    </div>
</div>
