<?php
/**
 * This file describes class for render view abandoned cart content in WordPress admin panel.
 *
 * @package xts
 */

namespace XTS\Modules\Abandoned_Cart\List_Table;

use WP_List_Table;

if ( ! defined( 'ABSPATH' ) ) {
	exit( 'No direct script access allowed' );
}

if ( ! class_exists( 'WP_List_Table' ) ) {
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}

/**
 * Create a new table class that will extend the WP_List_Table.
 */
class Cart_Content_Table extends WP_List_Table {
	/**
	 * Abandoned cart.
	 *
	 * @var WC_Cart
	 */
	public $cart;

	/**
	 * Constructor.
	 *
	 * @param WC_Cart $cart Abandoned cart.
	 */
	public function __construct( $cart ) {
		if ( ! xts_get_opt( 'cart_recovery_enabled' ) || ! xts_is_woocommerce_installed() ) {
			return;
		}

		$this->cart = $cart;

		parent::__construct(
			array(
				'singular' => 'product',
				'plural'   => 'products',
				'ajax'     => false,
			)
		);
	}

	/**
	 * Define what data to show on each column of the table.
	 *
	 * @param array  $item        Data.
	 * @param string $column_name - Current column name.
	 *
	 * @return mixed
	 */
	public function column_default( $item, $column_name ) {
		return array_key_exists( $column_name, $item ) ? esc_html( $item[ $column_name ] ) : '';
	}

	/**
	 * Prints column for image.
	 *
	 * @param array $item Item to use to print record.
	 */
	public function column_thumb( $item ) {
		if ( ! $item['product_permalink'] ) {
			echo wp_kses_post( $item['thumbnail'] );
		} else {
			printf( '<a href="%s">%s</a>', esc_url( $item['product_permalink'] ), $item['thumbnail'] ); // phpcs:ignore.
		}
	}

	/**
	 * Prints column for name.
	 *
	 * @param array $item Item to use to print record.
	 */
	public function column_name( $item ) {
		if ( ! $item['product_permalink'] ) {
			echo wp_kses_post( $item['product_name'] . '&nbsp;' );
		} else {
			echo wp_kses_post( sprintf( '<a href="%s">%s</a>', esc_url( $item['product_permalink'] ), $item['product_name'] ) );
		}
	}

	/**
	 * Prints column for price.
	 *
	 * @param array $item Item to use to print record.
	 */
	public function column_price( $item ) {
		echo wp_kses_post( $item['price'] );
	}

	/**
	 * Prints column for quantity.
	 *
	 * @param array $item Item to use to print record.
	 */
	public function column_quantity( $item ) {
		echo esc_html( $item['quantity'] );
	}

	/**
	 * Prints column for subtotal.
	 *
	 * @param array $item Item to use to print record.
	 */
	public function column_subtotal( $item ) {
		echo wp_kses_post( $item['subtotal'] );
	}

	/**
	 * Get columns.
	 *
	 * @return array
	 */
	public function get_columns() {
		return array(
			'thumb'    => '<span class="wc-image tips" data-tip="' . esc_attr__( 'Image', 'xts-theme' ) . '">' . __( 'Image', 'xts-theme' ) . '</span>',
			'name'     => esc_html__( 'Name', 'xts-theme' ),
			'price'    => esc_html__( 'Price', 'xts-theme' ),
			'quantity' => esc_html__( 'Quantity', 'xts-theme' ),
			'subtotal' => esc_html__( 'Subtotal', 'xts-theme' ),
		);
	}

	/**
	 * Prepare items.
	 *
	 * @return void
	 */
	public function prepare_items() {
		$columns               = $this->get_columns();
		$hidden                = array();
		$sortable              = array();
		$this->_column_headers = array( $columns, $hidden, $sortable );

		$data        = $this->table_data();
		$this->items = $data;
	}

	/**
	 * Get table data.
	 *
	 * @return array
	 */
	public function table_data() {
		$data        = array();
		$origin_cart = WC()->cart;

		foreach ( $this->cart->get_cart_contents() as $cart_item_key => $cart_item ) {
			$_product = $cart_item['data'];
			$quantity = $cart_item['quantity'];

			WC()->cart = $this->cart;

			if ( ! $_product || ! $_product->exists() || $quantity <= 0 ) {
				continue;
			}

			$data[] = array(
				'product_permalink' => $_product->is_visible() ? $_product->get_permalink() : '',
				'thumbnail'         => $_product->get_image(),
				'product_name'      => $_product->get_name(),
				'price'             => $this->cart->get_product_price( $_product ),
				'quantity'          => $quantity,
				'subtotal'          => $this->cart->get_product_subtotal( $_product, $quantity ),
			);
		}

		WC()->cart = $origin_cart;

		return $data;
	}
}
