<?php

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

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

class Wiaas_Admin_CL_Packages_List extends WP_List_Table {

	public function __construct() {

		parent::__construct( array(
			'singular'  => 'Package',
			'plural'    => 'Packages',
			'ajax'      => false,
		) );

		return true;
	}
	public function column_default( $item, $column_name ) {
		switch ( $column_name ) {
			case 'thumb':
				return '<a href="' . admin_url( 'admin.php?page=wiaas-cl-product&id=' . absint( $item->get_id() ) )  . '">' .
				       $item->get_image( 'thumbnail' ) .
				       '</a>';
			case 'name':
				return '<strong><a href="' . admin_url( 'admin.php?page=wiaas-cl-product&id=' . absint( $item->get_id() ) ) . '">' .
				       esc_attr( $item->get_name() ) .
				       '</a></strong>' .
				       '<div><span style="color: #999;">ID: '. $item->get_id() . '</span></div>';
			case 'country':
				$country = Wiaas_Countries::get_package_country($item);
				if (! empty($country)) {
					return '<span>'. $country['name'] . '</span>';
				}
				return '';
			case 'type':
				$type = Wiaas_Package_Type::get_package_type($item->get_id());
				return '<span>'. $type . '</span>';

		}
	}


	public function prepare_items() {
		$columns  = $this->get_columns();
		$sortable = $this->get_sortable_columns();
		$this->_column_headers = array( $columns, $sortable );

		$items_per_page = 10;

		$current_page = $this->get_pagenum();

		$query_args = array(
			'status' => 'publish',
			'type' => 'bundle',
			'tag' =>  array(),
			'limit' => $items_per_page,
			'page' => $current_page,
			'paginate' => true
		);

		if (!empty($_REQUEST['s'])) {
			$query_args['s'] = sanitize_text_field($_REQUEST['s']);
		}

		$results = wc_get_products($query_args);

		$this->items = $results->products;

		$this->set_pagination_args( array(
			'total_items' => $results->total,
			'per_page'    => $items_per_page,
		) );

		return true;

	}

	public function get_views() {

		return array();
	}

	public function get_columns() {
		$columns = array(
			'thumb'                   => __( '', 'wiaas' ),
			'name'            => __( 'Name', 'wiaas' ),
			'country'         => __( 'Sell in', 'wiaas' ),
			'type'            => __( 'Type', 'wiaas' ),
		);

		return $columns;
	}

	public function get_primary_column_name() {
		return 'product_name';
	}

	public function get_sortable_columns() {
		$sort = array(
			'product_name' => array( 'product_name', true ),
		);

		return $sort;
	}

	public function no_items() {
		_e( 'No packages found.', 'wiaas' );
		return true;
	}
}

function wiaas_handle_custom_query_var($query, $query_vars) {
	if ( ! empty( $query_vars['catalogue'] ) ) {

		$query['meta_query'][] = array(
			'key' => '_wiaas_catalogue_'.$query_vars['catalogue'],
			'value' => 'yes',
		);
	}

	return $query;
}

add_filter('woocommerce_product_data_store_cpt_get_products_query', 'wiaas_handle_custom_query_var', 10, 2);