<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php if ( !isset($_GET['user_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['user_nonce'])), 'DialBunny') ) { echo 'Sorry, we couldn’t verify your request due to a security check failure.'; return; } ?>
<div class="content d-flex flex-column flex-column-fluid" id="content">
  <div class="container-xxl" id="content_container">
    <?php
    // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
    $dialbunny_opened_item = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}dialbunny_released_numbers WHERE user = %d LIMIT 50", wp_get_current_user()->ID)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    ?>
<?php if (isset($_GET['erroraddingnumber'])) { echo wp_kses_post(dialbunny_main_danger("Phone Number Could Not Be Added", "An error has occurred and we could not process your request.")); }  ?>
<div class="card mb-8" dir="ltr">
      <div class="card-body d-flex flex-column flex-center">
        <div class="mb-2">
          <h1 class="fw-semibold text-gray-800 text-center lh-lg">What is a <span class="fw-bolder">Released Number?</span>
          </h1>
          <div class=" text-center">
            <span class="text-muted mt-1 fw-semibold fs-7">When you switch to a new DialBunny number, your old phone number will become released.</span>
            <br>
            <span class="text-muted mt-1 fw-semibold fs-7">If you've released a phone number, you can re-request it and switch back to an old phone number.</span>
          </div>
        </div>
      </div>
    </div>
    <div class="col-12">
      <div class="card h-xl-100" id="timeline_widget_2_card">
        <div class="card-header position-relative py-0 border-bottom-2">
          <h3 class="card-title">Released Phone Numbers</h3>
        </div>
        <div class="card-body">
          <div class="table-responsive"> <?php
            if ($dialbunny_opened_item != null)
            {
              echo '<table class="table table-hover table-rounded table-striped border border-gray-300 border-dashed gy-7 gs-7">';
              echo '<thead>';
              echo '<tr class="fw-semibold fs-6 text-gray-800 border-bottom-2 border-gray-200">';
                echo '<th>Number</th>';
                echo '<th>Released</th>';
                echo '<th>Address Requirements</th>';
                echo '<th></th>';
              echo '</tr>';
              echo '</thead>';
              echo '<tbody>';
              foreach ($dialbunny_opened_item as $dialbunny_record)
              {
                echo '<tr class="align-middle">';
                  echo '<td class="col-3">' . esc_html(dialbunny_formatNumber($dialbunny_record->phone)) . '</td>';
                  echo '<td class="col-2">' . esc_html(dialbunny_timeElapsed($dialbunny_record->created_at)) . '</td>';
                  echo '<td class="col-2"><span class="badge badge-success">None</span></td>';
                  echo '<td class="col-2"><button type="submit" onclick="document.getElementById(\'requestedNumber\').value = \'' . esc_html($dialbunny_record->phone) . '\'; document.getElementById(\'countrycode\').value = \'' . esc_html($dialbunny_record->countrycode) . '\'; document.getElementById(\'phonesid\').value = \'' . esc_html($dialbunny_record->sid) . '\'; document.getElementById(\'requestedNumberUI\').innerHTML = \'' . esc_html($dialbunny_record->countrycode) . ' ' . esc_html($dialbunny_record->phone) . '\';" class="btn border btn-sm btn-active-light-primary" data-bs-toggle="modal" data-bs-target="#modal_1">Re-Request</button></td>';
                echo '</tr>';
              }
              echo '</tbody>';
              echo '</table>';
            }
            else
            {
              echo '<div class="card border border-gray-300 border-dashed rounded p-6">
								<div class="card-header border-0 pt-15 pb-15">
									<h3 class="card-title align-items-start flex-column">
										<span class="card-label fw-bold text-gray-900">You Don\'t Have Any Released Numbers</span>
										<span class="text-muted mt-1 fw-semibold fs-7">All released phone numbers will show up here.</span>
									</h3>
									<div class="card-toolbar"></div>
								</div>
							</div>';
            }
            ?>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<div class="modal fade" tabindex="-1" id="modal_1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header pb-0 border-0 justify-content-end">
        <div class="btn btn-sm btn-icon btn-active-color-primary" data-bs-dismiss="modal">
          <i class="fs-1">
            <span class="fas fa-times"></span>
          </i>
        </div>
      </div>
      <div class="modal-body">
        <div class="mb-13">
          <h1 id="requestedNumberUI" class="mb-3">Getting A Number</h1>
          <div class="text-muted fw-semibold fs-5">Just click on "Get Number" to make the phone number yours again.</div>
        </div>
        <div class="stepper stepper-pills stepper-column d-flex flex-column flex-xl-row flex-row-fluid first" id="modal_create_app_stepper">
          <div class="d-flex justify-content-center justify-content-xl-start flex-row-auto w-100 w-xl-300px">
            <div class="stepper-nav ps-lg-10">
              <div class="stepper-item pending">
                <div class="stepper-wrapper">
                  <div class="stepper-icon w-40px h-40px">
                    <i class="stepper-check fs-2"></i>
                    <span class="stepper-number">1</span>
                  </div>
                  <div class="stepper-label">
                    <h3 class="stepper-title">Enjoy Your Number</h3>
                    <div class="stepper-desc">After re-requesting a number, it will be added back to your account allowing you to start making calls almost immediately.</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-light" data-bs-dismiss="modal">Close</button>
        <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" class="form">
            <?php wp_nonce_field( 'DialBunny', 'user_nonce' ); ?>
            <input type="hidden" name="action" value="dialbunny_re_request"></input>
          <input type="hidden" name="number" id="requestedNumber"></input>
          <input type="hidden" name="countrycode" id="countrycode"></input>
          <input type="hidden" name="phonesid" id="phonesid"></input>
          <button type='submit' class='btn btn-primary'>re-Get Number</button>
        </form>
      </div>
    </div>
  </div>
</div>
