<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php if ( !isset($_GET['user_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['user_nonce'])), 'Copuloo') ) { 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.
    $copuloo_saveData = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}copuloo_phone_details WHERE user = %d LIMIT 1", wp_get_current_user()->ID)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    $copuloo_opened_details = $wpdb->get_results($wpdb->prepare("SELECT managed_number_code, managed_number FROM {$wpdb->prefix}copuloo_phone_details WHERE user = %d LIMIT 1", wp_get_current_user()->ID)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    $copuloo_opened_item = array();
    $copuloo_recents_page = 1;
    $copuloo_limit = 10;
    $copuloo_offset = 0;
    if (isset($_GET['recentspage']) && is_numeric($_GET['recentspage']) && $_GET['recentspage'] > 1) {
        $copuloo_recents_page = (int) $_GET['recentspage'];
        $copuloo_offset = ($copuloo_recents_page - 1) * $copuloo_limit;
    }
    if (!empty($copuloo_opened_details[0]->managed_number)) {
      // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
      $copuloo_opened_item = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}copuloo_recents WHERE to_number = %s OR from_number = %s ORDER BY created_at DESC LIMIT %d OFFSET %d",$copuloo_opened_details[0]->managed_number,$copuloo_opened_details[0]->managed_number,$copuloo_limit,$copuloo_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 ($copuloo_recents_page > 1)
          {
            echo '<a href="' . esc_url(admin_url('admin.php?page=copuloo_dashboard')) . '&dashboardpage=recents&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&recentspage=' . esc_html($copuloo_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($copuloo_recents_page); ?></span>
          <?php if (isset($copuloo_opened_item) && count($copuloo_opened_item) == 10)
          {
						echo '<a href="' . esc_url(admin_url('admin.php?page=copuloo_dashboard')) . '&dashboardpage=recents&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&recentspage=' . esc_html($copuloo_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($copuloo_opened_item) && $copuloo_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 ($copuloo_opened_item as $copuloo_record)
              {
                echo '<tr class="align-middle">';
                // if recent number is outbound or inbound
                if($copuloo_record->to_countrycode.$copuloo_record->to_number == $copuloo_saveData[0]->managed_number_code.$copuloo_saveData[0]->managed_number)
                {
                  // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
                  $copuloo_opened_contact = $wpdb->get_results($wpdb->prepare("SELECT name,id FROM {$wpdb->prefix}copuloo_contacts WHERE user = %d AND countrycode = %s AND phone = %s LIMIT 1", wp_get_current_user()->ID, $copuloo_record->from_countrycode, $copuloo_record->from_number)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
                  echo '<td class="col-6">' . esc_html( !empty($copuloo_opened_contact[0]->name) ? $copuloo_opened_contact[0]->name : $copuloo_record->from_countrycode.' '.copuloo_formatNumber($copuloo_record->from_number) ) . '<br><span class="phone-inbound"></span><span class="px-3">' . esc_html(copuloo_timeElapsed($copuloo_record->created_at)) . '</span></td>';
                  if ($copuloo_opened_contact == null) { echo '<td class="col-6"><div class="d-flex justify-content-end"><a href="' . esc_url(admin_url('admin.php?page=copuloo_dashboard')) . '&dashboardpage=contacts&contact=new&newnumbercountrycode='. esc_html($copuloo_record->from_countrycode) .'&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&newnumber='. esc_html($copuloo_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=copuloo_dashboard')) . '&dashboardpage=contacts&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&contact='. esc_html($copuloo_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.
                  $copuloo_opened_contact = $wpdb->get_results($wpdb->prepare("SELECT name,id FROM {$wpdb->prefix}copuloo_contacts WHERE user = %d AND countrycode = %s AND phone = %s LIMIT 1", wp_get_current_user()->ID, $copuloo_record->to_countrycode, $copuloo_record->to_number)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
                  echo '<td class="col-6">' . esc_html( !empty($copuloo_opened_contact[0]->name) ? $copuloo_opened_contact[0]->name : $copuloo_record->to_countrycode.' '.copuloo_formatNumber($copuloo_record->to_number) ) . '<br><span class="phone-outbound"></span><span class="px-3">' . esc_html(copuloo_timeElapsed($copuloo_record->created_at)) . '</span></td>';
                  if ($copuloo_opened_contact == null) { echo '<td class="col-6"><div class="d-flex justify-content-end"><a href="' . esc_url(admin_url('admin.php?page=copuloo_dashboard')) . '&dashboardpage=contacts&contact=new&newnumbercountrycode='. esc_html($copuloo_record->from_countrycode) .'&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&newnumber='. esc_html($copuloo_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=copuloo_dashboard')) . '&dashboardpage=contacts&user_nonce='.esc_attr(sanitize_text_field(wp_unslash($_GET['user_nonce']))).'&contact='. esc_html($copuloo_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>
