<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
 * Sales Report Table class
 *
 * @author  YITH
 * @package YITH\COG\Classes
 * @version 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

if ( ! class_exists( 'YITH_COG_Sales_Report_Admin_Table' ) ) {
	/**
	 * COG Sales Report table
	 *
	 * @since 2.0.0
	 */
	class YITH_COG_Sales_Report_Admin_Table extends YITH_COG_Abstract_Admin_Table {

		/**
		 * Order IDS used for the totals calculations
		 *
		 * @var array $order_ids
		 */
		protected $order_ids;

		/**
		 * Class constructor method
		 *
		 * @param array $args Arguments for the parent constructor.
		 * @since 2.0.0
		 */
		public function __construct( $args = array() ) {

			parent::__construct(
				array_merge(
					$args,
					array(
						'singular' => 'sale',
						'plural'   => 'sales',
						'ajax'     => false,
					)
				)
			);
		}

		/**
		 * Prints Export CSV button
		 *
		 * @return void
		 */
		protected function print_export_csv_button() {
			echo sprintf(
				'<a href="%s" class="button action primary sales-report-export-csv">%s</a>',
				esc_url(
					add_query_arg(
						array(
							'action' => 'export_sales_report',
							'nonce'  => wp_create_nonce( 'export-sales-report' ),
							'args'   => isset( $_POST ) ? $_POST : array(), // phpcs:ignore
						),
						admin_url( 'edit.php' )
					)
				),
				esc_html_x( 'Export CSV', '[ADMIN] Export CSV button on admin screens', 'yith-cost-of-goods-for-woocommerce' )
			);
		}

		/* === COLUMNS METHODS === */

		/**
		 * Returns columns available in table
		 *
		 * @return array Array of columns of the table
		 * @since 2.0.0
		 */
		public function get_columns() {
			$columns = array(
				'product'            => _x( 'Product', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
				'quantity'           => _x( 'Qty sold', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
				'avg_product_price'  => _x( 'AVG Price', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
				'revenue'            => _x( 'Revenue', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
				'avg_product_cost'   => _x( 'AVG Cost', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
				'cost'               => _x( 'Cost', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
				'avg_product_margin' => _x( 'AVG Gross Profit', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
				'margin'             => _x( 'Gross Profit', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
				'margin_percentage'  => _x( 'Margin %', '[ADMIN] Sales report heading', 'yith-cost-of-goods-for-woocommerce' ),
			);

			$custom_columns_options = get_option( 'yith_cog_columns_custom_fields_toggle', array() );

			if ( ! empty( $custom_columns_options ) ) {
				foreach ( $custom_columns_options as $column ) {
					$columns[ $column['cog_custom_column_meta'] ] = $column['cog_custom_column_name'];
				}
			}

			return $columns;
		}

		/**
		 * Column Default
		 *
		 * @param object $item        Product.
		 * @param string $column_name Column name.
		 */
		public function column_default( $item, $column_name ) {
			switch ( $column_name ) {
				case $column_name:
					$meta = get_post_meta( $item['product_id'], $column_name, true );

					if ( ! ! $meta ) {
						echo wp_kses_post( $meta );
					} else {
						echo wp_kses_post( get_post_meta( $item['product_id'], '_' . $column_name, true ) );
					}

					break;
			}
		}

		/**
		 * Print column with product details
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_product( $item ) {

			$product = wc_get_product( $item['product_id'] );

			if ( ! $product ) {
				return $item['product_name'];
			}

			$product_id = $product->get_parent_id() > 0 ? $product->get_parent_id() : $product->get_id();

			$column = sprintf( '%s<a href="' . get_edit_post_link( $product_id ) . '">%s</a>', $product->get_image( array( 50, 50 ) ), $product->get_name() );

			return $column;
		}

		/**
		 * Print column with the data of how many this item has being sold
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_quantity( $item ) {
			$quantity = 0;
			if ( ! ! $item['items_sold'] ) {
				$quantity = $item['items_sold'];
			}

			return (int) $quantity;
		}

		/**
		 * Print column with the avergare product price
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_avg_product_price( $item ) {
			$avg_product_price = 0;
			if ( ! ! $item['net_revenue'] && $item['items_sold'] ) {
				$avg_product_price = yith_cog_price( round( $item['net_revenue'] / $item['items_sold'], 2 ) );
			}

			return apply_filters( 'yith_cog_sales_report_avg_product_price', $avg_product_price, $item );
		}

		/**
		 * Print column with the net sales of each item
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_revenue( $item ) {
			$revenue = 0;
			if ( ! ! $item['net_revenue'] ) {
				$revenue = $item['net_revenue'];
			}
			return yith_cog_price( $revenue );
		}

		/**
		 * Print column with the average item cost
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_avg_product_cost( $item ) {

			if ( ! ! $item['total_cost'] && !! $item['items_sold'] ) {
				$avg_cost = round( $item['total_cost'] / $item['items_sold'], 2 );
			}
			return yith_cog_price( $avg_cost );
		}

		/**
		 * Print column with the cost of each product
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_cost( $item ) {
			if ( ! ! $item['total_cost'] ) {

				$cost = $item['total_cost'];

				$include_taxes = get_option( 'yith_cog_include_taxes_as_cost', false );

				if ( 'yes' === $include_taxes && isset( $item['tax_amount'] ) ) {
					$cost += $item['tax_amount'];
				}
			}

			return yith_cog_price( round( $cost, 2 ) );
		}

		/**
		 * Print column with the average margin
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_avg_product_margin( $item ) {
			$avg_margin = 0;
			if ( ! ! $item['total_cost'] && ! ! $item['net_revenue'] ) {
				$avg_cost  = $item['total_cost'];
				$avg_price = $item['net_revenue'];

				$avg_margin = round( ( $avg_price - $avg_cost ) / $item['items_sold'], 2 );
			}
			return yith_cog_price( $avg_margin );
		}

		/**
		 * Print column with margin of each item
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_margin( $item ) {
			$margin = 0;

			if ( isset( $item['margin'] ) ) {
				$cost = $item['total_cost'];

				$include_taxes = get_option( 'yith_cog_include_taxes_as_cost', false );

				if ( 'yes' === $include_taxes && isset( $item['tax_amount'] ) ) {
					$cost += $item['tax_amount'];
				}

				$margin = $item['net_revenue'] - $cost;

			}
			return yith_cog_price( $margin );
		}

		/**
		 * Print column with the percentage of margin of each item
		 *
		 * @param object $item Current item row.
		 *
		 * @return string Column content
		 * @since 2.0.0
		 */
		public function column_margin_percentage( $item ) {
			$percentage = 0;
			if ( ! ! $item['net_revenue'] && ! ! $item['margin'] ) {

				$cost = $item['total_cost'];

				$include_taxes = get_option( 'yith_cog_include_taxes_as_cost', false );

				if ( 'yes' === $include_taxes && isset( $item['tax_amount'] ) ) {
					$cost += $item['tax_amount'];
				}

				$margin = $item['net_revenue'] - $cost;

				$percentage = round( ( $margin / $item['net_revenue'] ) * 100, 2 );
			}
			return $percentage . '%';
		}

		/**
		 * Print filters for current table
		 *
		 * @param string $which Top / Bottom.
		 *
		 * @return void
		 * @since 2.0.0
		 */
		protected function extra_tablenav( $which ) {
			if ( 'top' !== $which ) {
				return;
			}

			$quantity = 0;
			$revenue  = 0;
			$cost     = 0;
			$margin   = 0;
			$shipping = 0;

			// For the totals, we use the total items without pagination to make the correct summary
			$items = $this->total_items;

			foreach ( $items as $item ) {
				$quantity     += $item['items_sold'];
				$revenue      += round( $item['net_revenue'], 2 );
				$cost         += $item['total_cost'];
				$shipping     += $item['shipping_amount'];
			}

			$margin = $revenue - $cost;

			// adding shipping as a margin to the total section.
			if ( 'yes' === get_option( 'yith_cog_include_shipping_as_margin', false ) ) {
				$margin += $shipping;
			}

			// adding the order fees as a cost to the total section.
			if ( 'yes' === get_option( 'yith_cog_include_fees_as_cost', false ) ) {
				$total_fees = 0;
				$order_ids  = $this->order_ids;

				if ( is_array( $order_ids ) ) {
					foreach ( $order_ids as $order_id ) {

						$order       = wc_get_order( $order_id );
						$order_fees  = $order->get_total_fees();
						$total_fees += (int) $order_fees;
					}
					$cost += $total_fees;
				}
			}

			// adding shipping as a cost to the total section.
			if ( 'yes' === get_option( 'yith_cog_include_shipping_as_cost', false ) ) {
				$cost += $shipping;
			}

			$args = array(
				'items_sold' => array(
					'label' => _x( 'Items sold', '[ADMIN] Header total labels sales report table', 'yith-cost-of-goods-for-woocommerce' ),
					'value' => $quantity,
				),
				'revenue'    => array(
					'label' => _x( 'Revenue', '[ADMIN] Header total labels sales report table', 'yith-cost-of-goods-for-woocommerce' ),
					'value' => yith_cog_price( $revenue ),
				),
				'cost'       => array(
					'label' => _x( 'Cost', '[ADMIN] Header total labels sales report table', 'yith-cost-of-goods-for-woocommerce' ),
					'value' => yith_cog_price( $cost ),
				),
				'margin'     => array(
					'label' => _x( 'Gross Profit', '[ADMIN] Header total labels sales report table', 'yith-cost-of-goods-for-woocommerce' ),
					'value' => apply_filters( 'yith_cog_totals_section_gross_profit_value', yith_cog_price( $margin ), $margin ),
				),
			);

			// Options for the select in the report table to sort by them.
			$options = array(
				'product'  => _x( 'Name', '[ADMIN] Label of the order by select input', 'yith-cost-of-goods-for-woocommerce' ),
				'quantity' => _x( 'Quantity', '[ADMIN] Label of the order by select input', 'yith-cost-of-goods-for-woocommerce' ),
				'revenue'  => _x( 'Revenue', '[ADMIN] Label of the order by select input', 'yith-cost-of-goods-for-woocommerce' ),
				'cost'     => _x( 'Cost', '[ADMIN] Label of the order by select input', 'yith-cost-of-goods-for-woocommerce' ),
				'margin'   => _x( 'Gross Profit', '[ADMIN] Label of the order by select input', 'yith-cost-of-goods-for-woocommerce' ),
			);

			$this->print_totals_section( $args );

			?>
			<div class="yith-cog-reports-tablenav">
				<?php
				$this->print_product_filter();
				$this->print_category_filter();
				$this->print_select_orderby( $options );
				$this->print_from_datepicker();
				$this->print_to_datepicker();
				$this->print_filter_button();
				$this->print_reset_button();
				$this->print_export_csv_button();
				?>
			</div>
			<?php
		}

		/**
		 * Get items from Query.
		 *
		 * @param int $current_page Current page.
		 * @param int $per_page Items per page.
		 */
		public function get_items( $current_page, $per_page ) { 

			global $wpdb;

			// Cant' verify nonces here because they are fired within the topbar when filtering, if there are no filters, we can't verify it but we need the execution to continue.
			$from = isset( $_REQUEST['yith-cog-from-datepicker'] ) && ! empty( $_REQUEST['yith-cog-from-datepicker'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['yith-cog-from-datepicker'] ) ) : apply_filters( 'yith_cog_default_from_datepicker', date ( 'F d, Y', strtotime("first day of this month") ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$to   = isset( $_REQUEST['yith-cog-to-datepicker'] ) && ! empty( $_REQUEST['yith-cog-to-datepicker'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['yith-cog-to-datepicker'] ) ) : current_time( 'Y-m-d' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

			$from = $_GET['yith-cog-from-datepicker'] ?? $from;
			$to   = $_GET['yith-cog-to-datepicker'] ?? $to;

			$from = gmdate( 'Y-m-d', strtotime( $from ) ) . ' 00:00:00';
			$to   = current_time( 'Y-m-d' ) !== $to ? gmdate( 'Y-m-d', strtotime( $to ) ) . ' 23:59:59' : current_time( 'Y-m-d' ) . ' 23:59:59';

			$product_lookup_table = $wpdb->prefix . 'yith_cog_sales_report_data';

			if ( apply_filters( 'yith_cog_use_light_query', false ) ) {
				$per_page     = $this->get_items_per_page( 'yith_cog_products_per_page' );
				$current_page = $this->get_pagenum();

				$limit_from = ( $current_page * $per_page ) - $per_page;
				$limit_to   = $current_page * $per_page;

				$query_string = "
					select *
					from {$product_lookup_table}
					where date_created >= '{$from}' 
					and date_created <= '{$to}'
					group by order_item_id
					limit {$limit_from},{$limit_to}
				";
			} else {
				$query_string = "
					select *
					from {$product_lookup_table}
					where date_created >= '{$from}' 
					and date_created <= '{$to}'
					group by order_item_id
				";
			}

			$products_data         = $wpdb->get_results( $query_string ); // phpcs:ignore
			$formatted_report_data = array();

			foreach ( $products_data as $item => $product_data ) {

				$order   = wc_get_order( $product_data->order_id );
				$product = wc_get_product( $product_data->product_id );

				if ( ! $order || ! $order instanceof WC_Order ) {
					continue;
				}

				// Integration with YITH Bundle: if the product is inside a bundle, dont add it to the report.
				$is_bundle = wc_get_order_item_meta( $product_data->order_item_id, '_bundled_by' );

				if ( 'refunded' === $order->get_status() || $is_bundle ) {
					continue;
				}

				/**
				 * APPLY_FILTERS: yith_cog_order_status_sales_report_table
				 *
				 * Filter the default order status to check if the order would be printed in the report sales table
				 *
				 * @param array Default order statuses ('on-hold', 'processing', 'completed', 'refunded')
				 *
				 * @return array
				 */
				if ( ! is_object( $order ) || ( !! $product && $product instanceof WC_Product_Gift_Card ) || ! in_array( $order->get_status(), apply_filters( 'yith_cog_order_status_sales_report_table', array( 'on-hold', 'processing', 'completed', 'refunded' ) ), true ) ) {
					unset( $products_data[ $item ] );
					continue;
				}

				$refunded_item_id = wc_get_order_item_meta( $product_data->order_item_id, '_refunded_item_id' );

				if ( $refunded_item_id || $product_data->product_qty < 0 ) {
					$item_cost = wc_get_order_item_meta( $refunded_item_id, '_yith_cog_item_cost' );

					$product_data->item_cost = floatval( $item_cost ) * (int) $product_data->product_qty;
				}

				if ( ! isset( $product_data->item_cost ) || $product_data->item_cost <= 0 ) {
					continue;
				}

				$product_id   = $product_data->variation_id > 0 ? $product_data->variation_id : $product_data->product_id;
				$product_item = $order->get_item( $product_data->order_item_id );
				
				$formatted_report_data[ $product_id ] = $formatted_report_data[ $product_id ] ?? array();

				$formatted_report_data[ $product_id ]['product_id']   = $product_id;
				$formatted_report_data[ $product_id ]['product_name'] = is_object( $product_item) ?? $product_item->get_name();

				// total sold quantity.
				if ( isset( $formatted_report_data[ $product_id ]['items_sold'] ) ) {
					$formatted_report_data[ $product_id ]['items_sold'] += $product_data->product_qty;
				} else {
					$formatted_report_data[ $product_id ]['items_sold'] = $product_data->product_qty;
				}

				// total revenue.
				if ( isset( $formatted_report_data[ $product_id ]['net_revenue'] ) ) {
					$formatted_report_data[ $product_id ]['net_revenue'] += apply_filters( 'yith_cog_convert_amounts', $product_data->product_net_revenue, $product_data->order_item_id );
				} else {
					$formatted_report_data[ $product_id ]['net_revenue'] = apply_filters( 'yith_cog_convert_amounts', $product_data->product_net_revenue, $product_data->order_item_id );
				}

				// total cost.
				if ( isset( $formatted_report_data[ $product_id ]['total_cost'] ) ) {
					$formatted_report_data[ $product_id ]['total_cost'] += $product_data->item_cost * $product_data->product_qty;
				} else {
					$formatted_report_data[ $product_id ]['total_cost'] = $product_data->item_cost * $product_data->product_qty;
				}

				// shipping.
				if ( isset( $formatted_report_data[ $product_id ]['shipping_amount'] ) ) {
					$formatted_report_data[ $product_id ]['shipping_amount'] += apply_filters( 'yith_cog_convert_amounts', $product_data->shipping_amount, $product_data->order_item_id );
				} else {
					$formatted_report_data[ $product_id ]['shipping_amount'] = apply_filters( 'yith_cog_convert_amounts', $product_data->shipping_amount, $product_data->order_item_id );
				}

				// tax.
				if ( isset( $formatted_report_data[ $product_id ]['tax_amount'] ) ) {
					$formatted_report_data[ $product_id ]['tax_amount'] += apply_filters( 'yith_cog_convert_amounts', $product_data->tax_amount, $product_data->order_item_id );
				} else {
					$formatted_report_data[ $product_id ]['tax_amount'] = apply_filters( 'yith_cog_convert_amounts', $product_data->tax_amount, $product_data->order_item_id );
				}

				// shipping tax.
				if ( isset( $formatted_report_data[ $product_id ]['shipping_tax_amount'] ) ) {
					$formatted_report_data[ $product_id ]['shipping_tax_amount'] += apply_filters( 'yith_cog_convert_amounts', $product_data->shipping_tax_amount, $product_data->order_item_id );
				} else {
					$formatted_report_data[ $product_id ]['shipping_tax_amount'] =  apply_filters( 'yith_cog_convert_amounts', $product_data->shipping_tax_amount, $product_data->order_item_id );
				}
				
				// margin.
				if ( ! ! $product_data->item_cost && ! ! $product_data->product_net_revenue ) {
					$margin = $formatted_report_data[ $product_id ]['net_revenue'] - $formatted_report_data[ $product_id ]['total_cost'];
					$formatted_report_data[ $product_id ]['margin'] = $margin;
				}

				// filters.
				if ( ! isset( $_REQUEST['filter_action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
					continue;
				}

				// product filter.
				if ( isset( $_REQUEST['_product_id'] ) && ! empty( $_REQUEST['_product_id'] ) && $product_id !== $_REQUEST['_product_id'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended

					unset( $formatted_report_data[ $product_id ] );
				}

				// category filter.
				if ( isset( $_REQUEST['_category_id'] ) && ! empty( $_REQUEST['_category_id'] ) && !! $product && is_object( $product ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended

					$product_categories = $product->get_category_ids();
					foreach ( $_REQUEST['_category_id'] as $category_filter ) { // phpcs:ignore

						if ( ! in_array( $category_filter, $product_categories, false ) ) { // phpcs:ignore
							unset( $formatted_report_data[ $product_id ] );
						}
					}
				}
			}

			// Store the order IDs to be able to calculate the fees total.
			if ( isset( $formatted_report_data['orders'] ) ) {
				$this->order_ids = array_unique( $formatted_report_data['orders'] );
				unset( $formatted_report_data['orders'] );
			}

			$this->total_items = $formatted_report_data;

			// do the pagination.
			$formatted_report_data_with_pagination = array_slice( $formatted_report_data, ( $current_page - 1 ) * $per_page, $per_page );

			// retrieve data for table.
			$this->items     = $formatted_report_data_with_pagination;
			$this->max_items = count( $formatted_report_data );

			// Order by:
			if ( isset( $_REQUEST['cog_orderby'] ) && 'product' === $_REQUEST['cog_orderby'] ) {
				usort( $this->items, function($a, $b){return strnatcasecmp($a['product_name'], $b['product_name']); }); 
			}

			if ( isset( $_REQUEST['cog_orderby'] ) && 'quantity' === $_REQUEST['cog_orderby'] ) {
				usort($this->items, fn($a, $b) => $b['items_sold'] <=> $a['items_sold']);
			}

			if ( isset( $_REQUEST['cog_orderby'] ) && 'revenue' === $_REQUEST['cog_orderby'] ) {
				usort($this->items, fn($a, $b) => $b['net_revenue'] <=> $a['net_revenue']);
			}

			if ( isset( $_REQUEST['cog_orderby'] ) && 'cost' === $_REQUEST['cog_orderby'] ) {
				usort($this->items, fn($a, $b) => $b['total_cost'] <=> $a['total_cost']);
			}

			if ( isset( $_REQUEST['cog_orderby'] ) && 'margin' === $_REQUEST['cog_orderby'] ) {
				usort($this->items, fn($a, $b) => $b['margin'] <=> $a['margin']);
			}

			// Add the filters and the blank state when filtering a no search result are retrieved.
			if ( empty( $this->items ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended

				$args = array(
					'items_sold' => array(
						'label' => _x( 'Items sold', '[ADMIN] Header total labels sales report table', 'yith-cost-of-goods-for-woocommerce' ),
						'value' => '-',
					),
					'revenue'    => array(
						'label' => _x( 'Revenue', '[ADMIN] Header total labels sales report table', 'yith-cost-of-goods-for-woocommerce' ),
						'value' => '-',
					),
					'cost'       => array(
						'label' => _x( 'Cost', '[ADMIN] Header total labels sales report table', 'yith-cost-of-goods-for-woocommerce' ),
						'value' => '-',
					),
					'margin'     => array(
						'label' => _x( 'Gross Profit', '[ADMIN] Header total labels sales report table', 'yith-cost-of-goods-for-woocommerce' ),
						'value' => '-',
					),
				);

				$this->print_totals_section( $args );
				// Print filters again to allow to continue filtering when no items were found.
				?>
				<div class="yith-cog-reports-tablenav">
					<?php
					$this->print_product_filter();
					$this->print_category_filter();
					$this->print_from_datepicker();
					$this->print_to_datepicker();
					$this->print_filter_button();
					$this->print_reset_button();
					?>
				</div>
				<?php

			}
		}

		/**
		 * Prepare items for table
		 *
		 * @return void
		 * @since 2.0.0
		 */
		public function prepare_items() {
			
			// sets pagination arguments.
			$per_page     = $this->get_items_per_page( 'yith_cog_products_per_page' );
			$current_page = $this->get_pagenum();

			// sets columns headers.
			$columns  = $this->get_columns();
			$hidden   = $this->get_hidden_columns();
			$sortable = $this->get_sortable_columns();

			$this->_column_headers = array( $columns, $hidden, $sortable );

			$this->get_items( $current_page, $per_page );

			$total_pages = ceil( $this->max_items / $per_page );

			$this->set_pagination_args(
				array(
					'total_items' => $this->max_items,
					'per_page'    => $per_page,
					'total_pages' => $total_pages,
				)
			);

		}
	}
}
