<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="content d-flex flex-column flex-column-fluid" id="content">
  <div class="container-xxl" id="content_container">
    <?php
    $dialbunny_table = $wpdb->prefix . 'dialbunny_phone_details';
    // 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_recents = $wpdb->prefix . 'dialbunny_recents';
    $dialbunny_recent_day = 0;
    $dialbunny_recent_7day = 0;
    $dialbunny_recent_30day = 0;
    if (isset($dialbunny_saveData[0]->managed_number)) {
      // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
      $dialbunny_recent_day = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}dialbunny_recents WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 DAY) AND (to_number = %s OR from_number = %s) AND (to_number IS NOT NULL AND from_number IS NOT NULL AND to_number != '' AND from_number != '')",$dialbunny_saveData[0]->managed_number,$dialbunny_saveData[0]->managed_number)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
      $dialbunny_recent_7day = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}dialbunny_recents WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) AND (to_number = %s OR from_number = %s) AND (to_number IS NOT NULL AND from_number IS NOT NULL AND to_number != '' AND from_number != '')",$dialbunny_saveData[0]->managed_number,$dialbunny_saveData[0]->managed_number)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
      $dialbunny_recent_30day = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}dialbunny_recents WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) AND (to_number = %s OR from_number = %s) AND (to_number IS NOT NULL AND from_number IS NOT NULL AND to_number != '' AND from_number != '')",$dialbunny_saveData[0]->managed_number,$dialbunny_saveData[0]->managed_number)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    }
    $dialbunny_json_data = null;
    if (isset($dialbunny_saveData[0]->business_hours)) {
        $dialbunny_json_data = json_decode($dialbunny_saveData[0]->business_hours);
    }
?>
<?php
  if ( isset($_GET['user_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['user_nonce'])), 'DialBunny') )
  {
    if (isset($_GET['phonenumberadded'])) { echo wp_kses_post(dialbunny_main_success("Phone Number Added", "You have successfully added a DialBunny phone number to your account.")); }
    if (isset($_GET['phonenumberreleased'])) { echo wp_kses_post(dialbunny_main_success("Phone Number Released", "You have successfully released your DialBunny phone number.")); }
    if (isset($_GET['phonenumberreleasederror'])) { echo wp_kses_post(dialbunny_main_danger("Number Could Not Be Released", "There was an error removing your phone number.")); }
  }
?>
<div class="row g-5 g-xl-10 mb-6 mb-xl-10">
      <div class="col-xl-8">
        <div class="card h-xl-100">
          <div class="card-header position-relative py-0 border-bottom-2">
            <h3 class="card-title">Your Stats</h3>
          </div>
          <div class="card-body">
            <div class="mb-3 tns-item tns-slide-active" id="tns1-item0">
              <div class="d-flex flex-stack">
                <div class="m-0">
                  <span class="text-gray-800 fw-bold d-block fs-2hx lh-1 ls-n2"> <?php echo number_format($dialbunny_recent_day); ?> </span>
                  <span class="text-gray-500 fw-semibold fs-6">Calls <br>Last 24 Hours </span>
                </div>
              </div>
              <div class="separator separator-dashed my-5"></div>
              <div class="d-flex flex-stack">
                <div class="m-0">
                  <span class="text-gray-800 fw-bold d-block fs-2hx lh-1 ls-n2"> <?php echo number_format($dialbunny_recent_7day); ?> </span>
                  <span class="text-gray-500 fw-semibold fs-6">Calls <br>Last 7 Days </span>
                </div>
              </div>
              <div class="separator separator-dashed my-5"></div>
              <div class="d-flex flex-stack">
                <div class="m-0">
                  <span class="text-gray-800 fw-bold d-block fs-2hx lh-1 ls-n2"> <?php echo number_format($dialbunny_recent_30day); ?> </span>
                  <span class="text-gray-500 fw-semibold fs-6">Calls <br>Last 30 Days </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="col-xl-4">
        <div class="card bg-primary h-xl-100">
          <div class="card-body d-flex flex-column pt-13 pb-14">
            <div class="m-0">
              <h1 class="fw-semibold text-white text-center lh-lg mb-9">Need to forward calls? <br>
                <span class="fw-bolder">It's easy to setup</span>
              </h1>
              <div class="d-flex justify-content-center align-items-center p-10 mb-9">
                  <span class="menu-bullet">
                      <span class="fa fa-arrow-right text-white display-1"></span>
                  </span>
              </div>
            </div>
            <div class="text-center">
              <a class="btn btn-sm bg-white btn-color-gray-800" href="<?php echo esc_url(wp_nonce_url($dialbunnyDashboardUrl.'&dashboardpage=inbound', 'DialBunny', 'user_nonce')); ?>">Manage Calls</a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="row g-5 g-xl-10 mb-5 mb-xl-10">
      <div class="col-xl-<?php if (get_option('dialbunny_user_can_request_number') == true || current_user_can('administrator')) { echo '8'; } else { echo '12'; } ?>">
        <div class="card h-xl-100">
          <div class="card-header position-relative py-0 border-bottom-2">
            <h3 class="card-title">Your Number</h3>
            <div class="card-toolbar">
              <?php if (isset($dialbunny_saveData[0]->managed_number) && isset($dialbunny_saveData[0]->managed_number_code) && strlen((string)$dialbunny_saveData[0]->managed_number) > 3) { echo '<button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#release_managed_number">Release</button>'; } ?>
            </div>
          </div>
          <div class="card-body">
            <div class="d-flex flex-column flex-grow-1 py-2 py-lg-13 me-2">
              <p class="fw-bold text-gray-900 fs-1 mb-2">
                <?php if (isset($dialbunny_saveData[0]->managed_number) && isset($dialbunny_saveData[0]->managed_number_code) && strlen((string)$dialbunny_saveData[0]->managed_number) > 3)
                {
                  echo esc_html($dialbunny_saveData[0]->managed_number_code) . ' ' . esc_html(dialbunny_formatNumber($dialbunny_saveData[0]->managed_number));
                }
                else { echo 'You don\'t have a number setup yet'; }
                ?>
              </p>
              <span class="fw-semibold text-muted fs-5">Your DialBunny managed phone number</span>
            </div>
            <div class="row g-5 g-xl-8">
              <div class="col-xl-4">
                <a href="<?php echo esc_url(wp_nonce_url($dialbunnyDashboardUrl.'&dashboardpage=inbound', 'DialBunny', 'user_nonce')) ?>" class="card bg-danger hoverable card-xl-stretch mb-xl-8">
                  <div class="card-body">
                    <span class="text-white fa fa-level-down fa-2x"></span>
                    <div class="text-white fw-bold fs-2 mb-2 mt-5">Inbound Calls</div>
                    <div class="fw-semibold text-white">Setup call routing from your managed number</div>
                  </div>
                </a>
              </div>
              <div class="col-xl-4">
                <a href="<?php echo esc_url(wp_nonce_url($dialbunnyDashboardUrl.'&dashboardpage=calendar', 'DialBunny', 'user_nonce')); ?>" class="card bg-primary hoverable card-xl-stretch mb-xl-8">
                  <div class="card-body">
                    <span class="text-white fa fa-clock fa-2x"></span>
                    <div class="text-white fw-bold fs-2 mb-2 mt-5">Timetable</div>
                    <div class="fw-semibold text-white">Setup a custom work schedule</div>
                  </div>
                </a>
              </div>
              <div class="col-xl-4">
                <a href="<?php echo esc_url(wp_nonce_url($dialbunnyDashboardUrl.'&dashboardpage=inbound', 'DialBunny', 'user_nonce')); ?>" class="card bg-success hoverable card-xl-stretch mb-5 mb-xl-8">
                  <div class="card-body">
                    <span class="text-white fa fa-reply-all fa-2x"></span>
                    <div class="text-white fw-bold fs-2 mb-2 mt-5">Automated Responses</div>
                    <div class="fw-semibold text-white">Setup custom responses</div>
                  </div>
                </a>
              </div>
            </div>
          </div>
        </div>
      </div>
      <?php if (get_option('dialbunny_user_can_request_number') == true || current_user_can('administrator')) : ?>
      <div class="col-xl-4">
        <div class="card h-xl-100">
          <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">Need a number? <br>
                <span class="fw-bolder">Look through the repository</span>
              </h1>
              <div class="d-flex justify-content-center align-items-center p-10 mb-9">
                  <span class="menu-bullet">
                      <span class="fa fa-search text-dark display-1"></span>
                  </span>
              </div>
            </div>
            <div class="text-center mb-1">
              <a class="btn btn-sm btn-primary me-2" href="<?php echo esc_url(wp_nonce_url($dialbunnyDashboardUrl.'&dashboardpage=choose', 'DialBunny', 'user_nonce')); ?>">Get a number </a>
            </div>
          </div>
        </div>
      </div>
      <?php endif; ?>
    </div>
  </div>
</div>

<div class="modal fade" tabindex="-1" id="release_managed_number">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header pb-0 border-0 d-flex justify-content-end">
        <div class="btn btn-icon btn-sm btn-active-light-primary ms-2" data-bs-dismiss="modal" aria-label="Close">
          <i class="fs-1">
            <span class="fas fa-times"></span>
          </i>
        </div>
      </div>
      <div class="modal-body">
        <div class="text-center mb-5">
          <h1 class="d-flex justify-content-center align-items-center mb-3">Release Number?</h1>
          <div class="text-muted fw-semibold fs-5">After removing your number you can re-request it inside the released numbers page.</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_release_managed_number_user"></input>
          <?php
          if (isset($dialbunny_saveData[0]->managed_number) && isset($dialbunny_saveData[0]->managed_number_code))
          {
            echo '<input type="hidden" name="countrycode" value="'. esc_html($dialbunny_saveData[0]->managed_number_code) .'"></input>';
            echo '<input type="hidden" name="phone" value="'. esc_html($dialbunny_saveData[0]->managed_number) .'"></input>';
            echo '<input type="hidden" name="phonesid" value="'. esc_html($dialbunny_saveData[0]->sid) .'"></input>';
          }
          ?>
          <button type="submit" class="btn btn-danger">Release</button>
        </form>
      </div>
    </div>
  </div>
</div>
