<?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_saveData = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}dialbunny_phone_details WHERE user = %d LIMIT 1", wp_get_current_user()->ID)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    $dialbunny_opened_details = $wpdb->get_results($wpdb->prepare("SELECT managed_number_code, managed_number FROM {$wpdb->prefix}dialbunny_phone_details WHERE user = %d LIMIT 1", wp_get_current_user()->ID)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    $dialbunny_opened_item = array();
    $dialbunny_recents_page = 1;
    $dialbunny_limit = 10;
    $dialbunny_offset = 0;
    if (isset($_GET['recentspage']) && is_numeric($_GET['recentspage']) && $_GET['recentspage'] > 1) {
        $dialbunny_recents_page = (int) $_GET['recentspage'];
        $dialbunny_offset = ($dialbunny_recents_page - 1) * $dialbunny_limit;
    }
    if (!empty($dialbunny_opened_details[0]->managed_number)) {
      // 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_recents WHERE to_number = %s OR from_number = %s ORDER BY created_at DESC LIMIT %d OFFSET %d",$dialbunny_opened_details[0]->managed_number,$dialbunny_opened_details[0]->managed_number,$dialbunny_limit,$dialbunny_offset)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    }
    ?>
    <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">Recents</h3>
          <div class="card-toolbar">
          <?php if ($dialbunny_recents_page > 1)
          {
            echo '<a href="' . esc_url(admin_url('admin.php?page=dialbunny_dashboard')) . '&dashboardpage=recents&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&recentspage=' . esc_html($dialbunny_recents_page - 1) .'" class="btn btn-light btn-sm me-3">';
						echo '<span class="menu-bullet">';
						echo '<span class="fa fa-arrow-left"></span>';
						echo '</span>';
						echo '</a>';
          }
          ?>
          <span class="btn btn-bg-primary btn-sm me-3 text-white"><?php echo esc_html($dialbunny_recents_page); ?></span>
          <?php if (isset($dialbunny_opened_item) && count($dialbunny_opened_item) == 10)
          {
						echo '<a href="' . esc_url(admin_url('admin.php?page=dialbunny_dashboard')) . '&dashboardpage=recents&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&recentspage=' . esc_html($dialbunny_recents_page + 1) .'" class="btn btn-light btn-sm me-3">';
						echo '<span class="menu-bullet">';
						echo '<span class="fa fa-arrow-right"></span>';
						echo '</span>';
						echo '</a>';
          }
          ?>
          </div>
        </div>
        <div class="card-body">
          <div class="table-responsive"> <?php
            if (isset($dialbunny_opened_item) && $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 '<tbody>';
              foreach ($dialbunny_opened_item as $dialbunny_record)
              {
                echo '<tr class="align-middle">';
                // if recent number is outbound or inbound
                if($dialbunny_record->to_countrycode.$dialbunny_record->to_number == $dialbunny_saveData[0]->managed_number_code.$dialbunny_saveData[0]->managed_number)
                {
                  // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
                  $dialbunny_opened_contact = $wpdb->get_results($wpdb->prepare("SELECT name,id FROM {$wpdb->prefix}dialbunny_contacts WHERE user = %d AND countrycode = %s AND phone = %s LIMIT 1", wp_get_current_user()->ID, $dialbunny_record->from_countrycode, $dialbunny_record->from_number)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
                  echo '<td class="col-6">' . esc_html( !empty($dialbunny_opened_contact[0]->name) ? $dialbunny_opened_contact[0]->name : $dialbunny_record->from_countrycode.' '.dialbunny_formatNumber($dialbunny_record->from_number) ) . '<br><span class="phone-inbound"></span><span class="px-3">' . esc_html(dialbunny_timeElapsed($dialbunny_record->created_at)) . '</span></td>';
                  if ($dialbunny_opened_contact == null) { echo '<td class="col-6"><div class="d-flex justify-content-end"><a href="' . esc_url(admin_url('admin.php?page=dialbunny_dashboard')) . '&dashboardpage=contacts&contact=new&newnumbercountrycode='. esc_html($dialbunny_record->from_countrycode) .'&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&newnumber='. esc_html($dialbunny_record->from_number) .'"  class="btn border btn-sm btn-active-light-primary">Create Contact</a></div></td>'; }
                  else { echo '<td class="col-6"><div class="d-flex justify-content-end"><a href="' . esc_url(admin_url('admin.php?page=dialbunny_dashboard')) . '&dashboardpage=contacts&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&contact='. esc_html($dialbunny_opened_contact[0]->id) .'"  class="btn border btn-sm btn-active-light-primary">View Contact</a></div></td>'; }
                }
                else
                {
                  // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
                  $dialbunny_opened_contact = $wpdb->get_results($wpdb->prepare("SELECT name,id FROM {$wpdb->prefix}dialbunny_contacts WHERE user = %d AND countrycode = %s AND phone = %s LIMIT 1", wp_get_current_user()->ID, $dialbunny_record->to_countrycode, $dialbunny_record->to_number)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
                  echo '<td class="col-6">' . esc_html( !empty($dialbunny_opened_contact[0]->name) ? $dialbunny_opened_contact[0]->name : $dialbunny_record->to_countrycode.' '.dialbunny_formatNumber($dialbunny_record->to_number) ) . '<br><span class="phone-outbound"></span><span class="px-3">' . esc_html(dialbunny_timeElapsed($dialbunny_record->created_at)) . '</span></td>';
                  if ($dialbunny_opened_contact == null) { echo '<td class="col-6"><div class="d-flex justify-content-end"><a href="' . esc_url(admin_url('admin.php?page=dialbunny_dashboard')) . '&dashboardpage=contacts&contact=new&newnumbercountrycode='. esc_html($dialbunny_record->from_countrycode) .'&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&newnumber='. esc_html($dialbunny_record->to_number) .'"  class="btn border btn-sm btn-active-light-primary">Create Contact</a></div></td>'; }
                  else { echo '<td class="col-6"><div class="d-flex justify-content-end"><a href="' . esc_url(admin_url('admin.php?page=dialbunny_dashboard')) . '&dashboardpage=contacts&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&contact='. esc_html($dialbunny_opened_contact[0]->id) .'"  class="btn border btn-sm btn-active-light-primary">View Contact</a></div></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 Recent Callers</span>
											<span class="text-muted mt-1 fw-semibold fs-7">All recent inbound callers will show up here.</span>
										</h3>
										<div class="card-toolbar"></div>
									</div>
								</div>';
            }
            ?>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
