<?php
if ( !defined( 'ABSPATH' ) ) {
    exit;
}

echo '<h1>' . esc_html__( 'WP CV Builder', 'cv-builder' ) . '</h1>';
echo '<div class="bwdcv-dashboard-page">';
$image_url = plugin_dir_url(__FILE__) . '../../assets/admin/welcome-screen/icon/';
?>
<div class="bwdcv-dashboard-template">
  <div class="bwdcv-revenue-area mb-4">
    <div class="row">
      <div class="col-lg-5">
        <div class="bwdcv-revenue-left d-flex flex-column gap-4">
          <div class="bwdcv-revenue-left-wrap">
            <div class="row g-4">
                <?php
                function get_completed_orders_count_with_cv_subscription() {
                  $args = array(
                      'status' =>  ['completed', 'processing'],
                      'return' => 'ids',
                      'limit' => -1,
                  );
                  $completed_orders = wc_get_orders( $args );
                  $count = 0;
                  $total_price = 0;
                  $returned_count = 0;
                  $best_price_order = 0;
                  $low_price_order = 1;
                  foreach ( $completed_orders as $order_id ) {
                      $order = wc_get_order( $order_id );
                      $has_cv_subscription = false;
                      foreach ( $order->get_items() as $item_id => $item ) {
                          $product = $item->get_product();
                          if ( $product && has_term( 'cv-subscription', 'product_cat', $product->get_id() ) ) {
                              $has_cv_subscription = true;
                              break;
                          }
                      }
                      if ( $has_cv_subscription ) {
                          $count++;
                          $order_total = $order->get_total();
                          $total_price += $order_total;
                          if ( $order->get_status() == 'returned' ) {
                              $returned_count++;
                          }
                          if ( $order_total > $best_price_order ) {
                              $best_price_order = $order_total;
                          }
                          if ( $order_total < $low_price_order ) {
                              $low_price_order = $order_total;
                          }
                      }
                  }
                  return array(
                      'count' => $count,
                      'total_price' => $total_price,
                      'returned_count' => $returned_count,
                      'best_price_order' => $best_price_order,
                      'low_price_order' => $low_price_order
                  );
                }
                $order_info = get_completed_orders_count_with_cv_subscription();
                $totalPrice = $order_info['total_price'];
                $totalBestPrice = $order_info['best_price_order'];
                $totalLowPrice = $order_info['low_price_order'];
                $totalCurPrice = wc_price( bwdcv_format_number($order_info['total_price']) );
                ?>
              <div class="col-lg-6">
                <div class="bwdcv-revenue-left-single-box">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>resume-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Lifetime Orders', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_format_number($order_info['count']); ?></div>
                </div>
              </div>
              <div class="col-lg-6">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>dollar-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Lifetime Revenue', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo wc_price( bwdcv_format_number($order_info['total_price']) ); ?></div>
                </div>
              </div>
            </div>
          </div>
          <div class="bwdcv-revenue-left-wrap">
            <div class="row g-4">
                <?php
                function get_cv_subscription_orders_today_count() {
                    $args = array(
                        'status' =>  ['completed', 'processing'],
                        'date_created' => '>' . strtotime('today'),
                        'return' => 'ids',
                        'limit' => -1,
                    );
                    $orders_today = wc_get_orders( $args );
                    $count = 0;
                    $total_price = 0;
                    foreach ( $orders_today as $order_id ) {
                        $order = wc_get_order( $order_id );
                        $has_cv_subscription = false;
                        foreach ( $order->get_items() as $item_id => $item ) {
                            $product = $item->get_product();
                            if ( $product && has_term( 'cv-subscription', 'product_cat', $product->get_id() ) ) {
                                $has_cv_subscription = true;
                                break;
                            }
                        }
                        if ( $has_cv_subscription ) {
                            $count++;
                            $total_price += $order->get_total();
                        }
                    }
                    return array( 'count' => $count, 'total_price' => $total_price );
                }
                $order_info = get_cv_subscription_orders_today_count();
                ?>
              <div class="col-lg-6">
                <div class="bwdcv-revenue-left-single-box box3">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>subscription-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Today\'s Orders', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_format_number($order_info['count']); ?></div>
                </div>
              </div>
              <div class="col-lg-6">
                <div class="bwdcv-revenue-left-single-box box4">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>dollar-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Today\'s Revenue', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo wc_price( bwdcv_format_number($order_info['total_price']) ); ?></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="col-lg-7">
        <div class="bwdcv-revenue-right-area">
          <div class="d-flex justify-content-between align-items-center mb-4">
              <h4 class="bwdcv-revenue-title"><?php esc_html_e( 'Monthly Revenue', 'cv-builder' ); ?></h4>
              <button class="bwdcv-revenue-button" type="button"><?php echo date( 'Y' ); ?></button>
          </div>
          <?php
          function get_completed_orders_count_with_cv_subscription_all_months( $year ) {
            $order_info_by_month = array();

            for ( $month = 1; $month <= 12; $month++ ) {
                $first_day_of_month = "$year-$month-01";
                $last_day_of_month  = date( 'Y-m-t', strtotime( $first_day_of_month ) );

                $args = array(
                    'status'    =>  ['completed', 'processing'],
                    'return'    => 'ids',
                    'limit'     => -1,
                    'date_created' => "$first_day_of_month...$last_day_of_month",
                );
                $completed_orders = wc_get_orders( $args );

                $total_price = 0;
                foreach ( $completed_orders as $order_id ) {
                    $order = wc_get_order( $order_id );
                    $has_cv_subscription = false;
                    foreach ( $order->get_items() as $item_id => $item ) {
                        $product = $item->get_product();
                        if ( $product && has_term( 'cv-subscription', 'product_cat', $product->get_id() ) ) {
                            $has_cv_subscription = true;
                            break;
                        }
                    }
                    if ( $has_cv_subscription ) {
                        $total_price += $order->get_total();
                    }
                }

                $order_info_by_month[ $month ] = array(
                    'month_name'  => date( 'M', mktime( 0, 0, 0, $month, 1 ) ),
                    'total_price' => $total_price,
                );
            }

            return $order_info_by_month;
          }

          $current_year = date( 'Y' );
          $order_info_by_month = get_completed_orders_count_with_cv_subscription_all_months( $current_year );

          // Encode data into JSON
          $order_data_json = htmlspecialchars( wp_json_encode( $order_info_by_month ), ENT_QUOTES, 'UTF-8' );
          ?>
          <div class="bwdcv-revenue-chart">
            <canvas id="myChart" style="height: 300px"></canvas>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="bwdcv-revenue-area bwdcv-price-graph-area mb-4">
    <div class="row">
      <div class="col-lg-5">
        <div class="bwdcv-revenue-right-area bwdcv-price-graph-right-area">
          <h1><?php esc_html_e( 'Compare Price', 'cv-builder' ); ?></h1>
          <div class="row g-0 align-items-center">
            <div class="col-lg-6">
              <div class="bwdcv-price-graph-chart">
                <div id="bwdcv-chart-container">
                  <canvas id="priceChart"></canvas>
                  <div class="bwdcv-inside-price">
                    <h4><?php echo $totalCurPrice; ?></h4>
                    <span><?php esc_html_e( 'Total Price', 'cv-builder' ); ?></span>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-lg-6">
              <div class="bwdcv-price-info-wrap">
                <div class="row">
                  <div class="col-lg-6 mb-4">
                    <div class="bwdcv-price-info">
                      <div class="bwdcv-legend-item">
                          <span>
                              <span class="bwdcv-legend-color" style="background-color: #1D0BCE"></span>
                              <?php esc_html_e( 'Total', 'cv-builder' ); ?>
                          </span>
                          <h5><?php echo $totalCurPrice; ?></h5>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-6 mb-4">
                    <div class="bwdcv-price-info">
                      <div class="bwdcv-legend-item">
                          <span>
                              <span class="bwdcv-legend-color" style="background-color: #403CEF"></span>
                              <?php esc_html_e( 'Today', 'cv-builder' ); ?>
                          </span>
                          <h5><?php $orderToday = get_cv_subscription_orders_today_count(); echo wc_price(bwdcv_format_number($orderToday['count'])); ?></h5>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-6 mb-4">
                    <div class="bwdcv-price-info">
                      <div class="bwdcv-legend-item">
                          <span>
                              <span class="bwdcv-legend-color" style="background-color: #080566"></span>
                              <?php esc_html_e( 'Best', 'cv-builder' ); ?>
                          </span>
                          <h5><?php echo wc_price(bwdcv_format_number($totalBestPrice)); ?></h5>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-6 mb-4">
                    <div class="bwdcv-price-info">
                      <div class="bwdcv-legend-item">
                          <span>
                              <span class="bwdcv-legend-color" style="background-color: #010019"></span>
                              <?php esc_html_e( 'Low', 'cv-builder' ); ?>
                          </span>
                          <h5><?php echo wc_price(bwdcv_format_number($totalLowPrice)); ?></h5>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="col-lg-7">
        <div class="bwdcv-revenue-left d-flex flex-column gap-4">
          <div class="bwdcv-revenue-left-wrap">
            <div class="row g-4">
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>visitors-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'CV Created', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <?php
                    function get_editor_page_count() {
                        $args = array(
                            'post_type'      => 'cv',
                            'post_status'    => 'publish',
                            'author__in'     => get_users(array(
                                'role'    => 'cv_editor',
                                'fields'  => 'ID',
                            )),
                            'posts_per_page' => -1,
                            'fields'         => 'ids',
                        );
                        $query = new WP_Query($args);
                        return count($query->posts);
                    }
                    echo '<div class="bwdcv-cv-create-num">'.bwdcv_format_number(get_editor_page_count()).'</div>';
                  ?>
                </div>
              </div>
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>user-role-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Live Visitors', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_get_live_visitor_count(); ?></div>
                </div>
              </div>
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img src="<?php echo esc_url( $image_url );?>user.png" alt="" />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Total Visitors', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_get_total_visitor_count(); ?></div>
                </div>
              </div>
              <?php
                function lvc_get_editor_user_count() {
                    $editor_users = get_users( array(
                        'role'    => 'cv_editor',
                        'fields'  => 'ID',
                    ) );
                    return count( $editor_users );
                }
                $count = lvc_get_editor_user_count();
                ?>
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>resume-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Total Users', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_format_number($count); ?></div>
                </div>
              </div>
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>resume-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Register Visitors', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_format_number(bwdcv_get_logged_in_user_count()); ?></div>
                </div>
              </div>
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>active-subs-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Total Refund', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num">
                    <?php
                    $total_refunded_orders = 0;
                    $total_refunded_amount = 0;
                    $args = array(
                      'status' => 'refunded',
                      'limit'  => -1,
                    );
                    $orders = wc_get_orders( $args );
                    foreach ( $orders as $order ) {
                      $total_refunded_orders++;
                      $total_refunded_amount += $order->get_total_refunded();
                    }
                    echo bwdcv_format_number($total_refunded_orders)."(" .get_woocommerce_currency_symbol(). bwdcv_format_number($total_refunded_amount).")";
                    ?>
                  </div>
                </div>
              </div>
              <?php
              $orders = wc_get_orders([
                'limit' => -1,
                'status' =>  ['completed', 'processing'],
              ]);
              $activeSub = 0;
              $deactiveSub = 0;
              $total_downloads = 0;
              if (!empty($orders)) {
                  foreach ($orders as $order) {
                      $purchase_date = $order->get_date_created();
                      $formatted_purchase_date = $purchase_date ?
                          $purchase_date->setTimezone(new \DateTimeZone(wp_timezone_string()))->format('Y-m-d') : esc_html__('N/A', 'cv-builder');

                      $items = $order->get_items();
                      foreach ($items as $item) {
                          $product_id = $item->get_product_id();
                            $cv_download = $order->get_meta( '_subscription_per_download', true );
                            if ( $cv_download === '' || $cv_download === null || $cv_download === false ) {
                                // শুধু প্রথমবার order এ meta না থাকলে product থেকে নেবে
                                $cv_download = get_post_meta( $product_id, '_subscription_per_download', true );
                                $cv_download = is_numeric( $cv_download ) ? (int) $cv_download : 0;
                                $order->update_meta_data( '_subscription_per_download', $cv_download );
                                $order->save();
                            } else {
                                $cv_download = is_numeric( $cv_download ) ? (int) $cv_download : 0;
                            }

                          $order_total_downloads = (int) $order->get_meta('_total_downloads', true);
                          $total_downloads += $order_total_downloads;

                          $subscription_billing_period = (int) $order->get_meta('_subscription_billing_period', true);
                          if (!$subscription_billing_period) {
                              $subscription_billing_period = get_post_meta($product_id, '_subscription_billing_period', true);
                              $order->update_meta_data('_subscription_billing_period', $subscription_billing_period);
                              $order->save();
                          }

                          $expire_date = '';
                          if($subscription_billing_period){
                              $billing_days = 0;
                              if ($subscription_billing_period == '1') {
                                  $billing_days = 1;
                              } elseif ($subscription_billing_period == '30') {
                                  $billing_days = 30;
                              } elseif ($subscription_billing_period == '90') {
                                  $billing_days = 90;
                              } elseif ($subscription_billing_period == '183') {
                                  $billing_days = 183;
                              } elseif ($subscription_billing_period == '365') {
                                  $billing_days = 365;
                              }
                              if ($purchase_date && $billing_days) {
                                  $purchase_date_obj = new \DateTime($formatted_purchase_date);
                                  $purchase_date_obj->modify("+$billing_days days");
                                  $newDate = $purchase_date_obj->format('Y-m-d');
                                  $expire_date = $subscription_billing_period?$newDate:'';
                              }
                          }
                          $expireCheck = '';
                          if(\DateTime::createFromFormat('Y-m-d', $expire_date) !== false){
                              $expireCheck = $expire_date;
                          }
                          $current_date = date("Y-m-d");

                          if ($cv_download >= 1 || $current_date <= $expireCheck) {
                              $activeSub++;
                          } else{
                              $deactiveSub++;
                          }
                      }
                  }

              }
              ?>
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>active-subs-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Total Downloaded', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_format_number($total_downloads); ?></div>
                </div>
              </div>
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>deactive-subs-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Total Active subscriptions', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_format_number($activeSub); ?></div>
                </div>
              </div>
              <div class="col-lg-4 bwdcv-child">
                <div class="bwdcv-revenue-left-single-box box2">
                  <div
                    class="bwdcv-revenue-left-icon-box d-flex align-items-center gap-2"
                  >
                    <div
                      class="bwdcv-revenue-left-icon d-flex align-items-center justify-content-center rounded-circle"
                    >
                      <img
                        src="<?php echo esc_url( $image_url );?>deactive-subs-icon.png"
                        alt=""
                      />
                    </div>
                    <div class="bwdcv-revenue-left-icon-title">
                      <h5><?php esc_html_e( 'Total Deactive subscriptions', 'cv-builder' ); ?></h5>
                    </div>
                  </div>
                  <div class="bwdcv-cv-create-num"><?php echo bwdcv_format_number($deactiveSub); ?></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="bwdcv-subscriptions-area">
    <div class="row">
      <?php
      function get_monthly_mrr_with_cv_subscription() {
        $args = array(
            'status' =>  ['completed', 'processing'],
            'return' => 'ids',
            'limit' => -1,
        );
        $completed_orders = wc_get_orders( $args );
        $monthly_data = array();
        foreach ( $completed_orders as $order_id ) {
            $order = wc_get_order( $order_id );
            $has_cv_subscription = false;
            foreach ( $order->get_items() as $item_id => $item ) {
                $product = $item->get_product();
                if ( $product && has_term( 'cv-subscription', 'product_cat', $product->get_id() ) ) {
                    $has_cv_subscription = true;
                    break;
                }
            }
            if ( $has_cv_subscription ) {
                $order_date = $order->get_date_created();
                $month = $order_date->format( 'Y-m' );
                if ( ! isset( $monthly_data[ $month ] ) ) {
                    $monthly_data[ $month ] = array(
                        'count' => 0,
                        'total_price' => 0,
                    );
                }
                $monthly_data[ $month ]['count']++;
                $monthly_data[ $month ]['total_price'] += $order->get_total();
            }
        }
        return $monthly_data;
      }
      $monthly_mrr = get_monthly_mrr_with_cv_subscription();
      ?>
      <div class="col-lg-6">
        <div class="bwdcv-subscription-table">
          <div
            class="bwdcv-status-wrap d-flex justify-content-between align-items-center"
          >
            <div class="bwdcv-status-title">
              <h4><?php esc_html_e( 'Monthly Recurring Revenue', 'cv-builder' ); ?></h4>
            </div>

            <div class="bwdcv-status-button d-flex gap-3 align-items-center">
              <div id="bwdcv-popup" class="bwdcv-popup">
                  <div class="bwdcv-popup-content">
                    <span class="bwdcv-close-btn" onclick="closePopup()">&times;</span>
                    <div class="bwdcv-filter-container">
                      <select id="bwdcv-month-filter" class="bwdcv-nice-select" onchange="bwdcvFilterOrders()">
                          <option value=""><?php esc_html_e( 'All Months', 'cv-builder' ); ?></option>
                          <option value="january"><?php esc_html_e( 'January', 'cv-builder' ); ?></option>
                          <option value="february"><?php esc_html_e( 'February', 'cv-builder' ); ?></option>
                          <option value="march"><?php esc_html_e( 'March', 'cv-builder' ); ?></option>
                          <option value="april"><?php esc_html_e( 'April', 'cv-builder' ); ?></option>
                          <option value="may"><?php esc_html_e( 'May', 'cv-builder' ); ?></option>
                          <option value="june"><?php esc_html_e( 'June', 'cv-builder' ); ?></option>
                          <option value="july"><?php esc_html_e( 'July', 'cv-builder' ); ?></option>
                          <option value="august"><?php esc_html_e( 'August', 'cv-builder' ); ?></option>
                          <option value="september"><?php esc_html_e( 'September', 'cv-builder' ); ?></option>
                          <option value="october"><?php esc_html_e( 'October', 'cv-builder' ); ?></option>
                          <option value="november"><?php esc_html_e( 'November', 'cv-builder' ); ?></option>
                          <option value="december"><?php esc_html_e( 'December', 'cv-builder' ); ?></option>
                      </select>

                      <select id="bwdcv-year-filter" class="bwdcv-nice-select" onchange="bwdcvFilterOrders()">
                          <option value=""><?php esc_html_e( 'All Years', 'cv-builder' ); ?></option>
                          <?php
                          $activation_date = get_option('dynamic_year_dropdown_activation_date');
                          $activation_year = date('Y', strtotime($activation_date));
                          $current_year = date('Y');
                          for ($year = $activation_year; $year <= $current_year; $year++) {
                              echo '<option value="' . $year . '">' . $year . '</option>';
                          }
                          ?>
                      </select>
                    </div>
                    <button id="bwdcv-download-button" class="bwdcv-cta-btn"><?php esc_html_e( 'Download Data', 'cv-builder' ); ?></button>
                    <div class="bwdcv-scroll-table">
                      <table id="bwdcv-order-table">
                          <thead>
                              <tr>
                                <th class="bwdcv-th"><?php esc_html_e( 'Order No', 'cv-builder' ); ?></th>
                                <th class="bwdcv-th"><?php esc_html_e( 'Name', 'cv-builder' ); ?></th>
                                <th class="bwdcv-th"><?php esc_html_e( 'Email', 'cv-builder' ); ?></th>
                                <th class="bwdcv-th"><?php esc_html_e( 'Date', 'cv-builder' ); ?></th>
                                <th class="bwdcv-th"><?php esc_html_e( 'Revenue', 'cv-builder' ); ?></th>
                              </tr>
                          </thead>
                          <tbody>
                            <?php
                              $args = array(
                                'status'    =>  ['completed', 'processing'],
                                'return'    => 'ids',
                                'limit'     => -1,
                              );
                              $completed_orders = wc_get_orders( $args );

                              $total_orders = 0;
                              $total_price = 0;

                              foreach ( $completed_orders as $order_id ) {
                                $order = wc_get_order( $order_id );
                                $has_cv_subscription = false;

                                foreach ( $order->get_items() as $item_id => $item ) {
                                  $product = $item->get_product();
                                  if ( $product && has_term( 'cv-subscription', 'product_cat', $product->get_id() ) ) {
                                    $has_cv_subscription = true;
                                    break;
                                  }
                                }

                                if ( $has_cv_subscription ) {
                                  $total_orders++;
                                  $user_id = $order->get_user_id();
                                  $user_info = get_userdata( $user_id );
                                  $username = $user_info ? $user_info->user_login : 'Guest';
                                  echo '<tr>';
                                    echo '<td class="bwdcv-td">'. $order_id .'</td>';
                                    echo '<td class="bwdcv-td">'. $username .'</td>';
                                    echo '<td class="bwdcv-td">'. $order->get_billing_email() .'</td>';
                                    echo '<td class="bwdcv-td">'. $order->get_date_created()->format( 'M j Y' ) .'</td>';
                                    echo '<td class="bwdcv-td">'. wc_price($order->get_total()) .'</td>';
                                  echo '</tr>';
                                }
                              }
                          ?>
                          </tbody>
                      </table>
                      <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js"></script>
                    </div>
                  </div>
              </div>
              <button id="bwdcv-popupBtn" class="bwdcv-cta-btn"><?php esc_html_e( 'Filter Revenue', 'cv-builder' ); ?></button>
            </div>

          </div>
          <div class="bwdcv-scroll-table">
          <table class="table table-bordered">
            <thead>
              <tr>
                <th><?php esc_html_e( 'Month', 'cv-builder' ); ?></th>
                <th><?php esc_html_e( 'Order Count', 'cv-builder' ); ?></th>
                <th><?php esc_html_e( 'Total Revenue', 'cv-builder' ); ?></th>
              </tr>
            </thead>
            <tbody>
              <?php
              foreach ( $monthly_mrr as $month => $data ) {
                echo '<tr>';
                echo '<td><div class="bwd-subscription-info"><h5>' . $month . '</h5></div></td>';
                echo '<td>' . bwdcv_format_number($data['count']) . '</td>';
                echo '<td>' . wc_price( bwdcv_format_number($data['total_price']) ) . '</td>';
                echo '</tr>';
              }
              ?>
            </tbody>
          </table>
        </div>
        </div>
      </div>
      <?php
      $args = array(
          'role'    => 'cv_editor',
          'fields'  => array( 'ID', 'user_login', 'user_email' )
      );
      $editors = get_users( $args );
      if ( ! empty( $editors ) ) {
        ?>
      <div class="col-lg-6">
        <div class="bwdcv-subscription-table">
          <div
            class="bwdcv-status-wrap d-flex justify-content-between align-items-center"
          >
            <div class="bwdcv-status-title">
              <h4><?php esc_html_e( 'Users information', 'cv-builder' ); ?></h4>
            </div>
            <div
              class="bwdcv-status-button d-flex gap-3 align-items-center"
            >
              <div class="bwdcv-status-select">
                <select id="subStatus" onchange="filterSubStatus()">
                  <option value="all"><?php esc_html_e( 'All', 'cv-builder' ); ?></option>
                  <option value="pro"><?php esc_html_e( 'Pro', 'cv-builder' ); ?></option>
                  <option value="free"><?php esc_html_e( 'Free', 'cv-builder' ); ?></option>
                </select>
              </div>
              <button onclick="downloadExcel()"><?php esc_html_e( 'Export', 'cv-builder' ); ?></button>
            </div>
          </div>
          <div class="bwdcv-scroll-table">
            <table class="table table-bordered" id="editors-table">
              <thead>
                <tr>
                  <th><?php esc_html_e( 'Username', 'cv-builder' ); ?></th>
                  <th><?php esc_html_e( 'Email', 'cv-builder' ); ?></th>
                  <th><?php esc_html_e( 'Status', 'cv-builder' ); ?></th>
                </tr>
              </thead>
              <tbody>
                <?php
                foreach ( $editors as $editor ) {
                  $args = array(
                    'customer' => $editor->ID,
                    'status'   =>  ['completed', 'processing'],
                  );
                  $orders = wc_get_orders($args);
                  if (!empty($orders)) {
                    $order_status = 'pro';
                  } else {
                    $order_status = 'free';
                  }
                  echo '<tr data-editors-table="'.$order_status.'">';
                  echo '<td>' . $editor->user_login . '</td>';
                  echo '<td>' . $editor->user_email . '</td>';
                  echo '<td><button class="'.$order_status.'" type="button">'.$order_status.'</button></td>';
                  echo '</tr>';
                }
                ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>
      <?php
      } else {
        echo esc_html__( 'No datas found yet.', 'cv-builder' );
      }
      ?>
    </div>
  </div>
</div>
<?php
echo '<div id="order-data" data-year="'.esc_attr( $current_year ).'" data-months="'.$order_data_json.'" data-total-price="'.bwdcv_format_number($totalPrice).'" data-today-price="'.bwdcv_format_number($orderToday['count']).'" data-total-low-price="'.bwdcv_format_number($totalLowPrice).'" data-total-best-price="'.bwdcv_format_number($totalBestPrice).'"></div>';
echo '</div>';
