<?php

/**
 * Class Wiaas_Admin_CL_Orders
 */
class Wiaas_Admin_CL_Orders {

	/**
	 * Displays list of orders from commercial lead owned shop
	 *
	 * Enables quick preview of each order
	 *
	 * This list of orders is achieved with customization of default order list for `shop_order` post
	 * by using hooks and filters to allow only data that commercial lead should be able to see
	 *
	 */

	public static function init() {

		add_filter( 'bulk_actions-edit-shop_order', array( __CLASS__, 'remove_bulk_actions_for_list_table_orders' ), 999 );

		add_filter('woocommerce_admin_order_preview_actions', array(__CLASS__, 'remove_actions_from_order_preview'));

		add_filter('woocommerce_admin_order_preview_line_items', array(__CLASS__, 'filter_order_items_for_order_preview'), 10, 2);

		add_filter('manage_shop_order_posts_columns', array(__CLASS__, 'columns_for_list_table_orders'), 999);

		add_filter( 'manage_edit-shop_order_sortable_columns', array( __CLASS__, 'define_sortable_columns_for_list_table_orders' ) );

		add_action('manage_shop_order_posts_custom_column', array(__CLASS__, 'render_columns_for_list_table_orders'), 999, 2);
	}


	/**
	 * Remove all bulk actions for commercial lead
	 *
	 * @return array
	 */
	public static function remove_bulk_actions_for_list_table_orders() {
		return array();
	}

	/**
	 * Remove actions from shop order preview modal so only data info is visible
	 *
	 * @return array
	 */
	public static function remove_actions_from_order_preview() {
		return array();
	}

	/**
	 * Show only packages on order preview
	 *
	 * @param $order_items
	 * @param $order
	 *
	 * @return array
	 */
	public static function filter_order_items_for_order_preview($order_items, $order) {

		$items =  array();

		foreach ($order_items as $order_item) {
			if (isset($order_item['wiaas_standard_package'])) {
				$items[] = $order_item;
			}
		}

		return $items;
	}

	/**
	 * Override default table columns so only commercial lead specific columns are visible
	 *
	 * @param $columns
	 *
	 * @return array
	 */
	public static function columns_for_list_table_orders($columns) {
		$show_columns                     = array();
		$show_columns['cb']               = $columns['cb'];
		$show_columns['_wiaas_order_number']     = __( 'Order', 'woocommerce' );
		$show_columns['order_date']       = __( 'Date', 'woocommerce' );
		$show_columns['order_status']     = __( 'Status', 'woocommerce' );
		$show_columns['order_total']      = __( 'Total', 'woocommerce' );

		return $show_columns;
	}


	/** Append commercial lead columns to table sortable columns
	 *
	 * @param $sortable_columns
	 *
	 * @return mixed
	 */
	public static function define_sortable_columns_for_list_table_orders($sortable_columns) {

		$sortable_columns['_wiaas_order_number'] = 'ID';

		return $sortable_columns;
	}

	/**
	 * Render commercial lead specific columns
	 *
	 * @param $column
	 * @param $order_id
	 */
	public static function render_columns_for_list_table_orders($column, $order_id) {

		if ($column === '_wiaas_order_number') {

			$order = wc_get_order($order_id);

			echo '<strong>#' . esc_attr( $order->get_order_number() ) . '</strong>';

			if ( $order->get_status() !== 'trash' ) {
				echo '<a href="#" class="order-preview" data-order-id="' . absint( $order->get_id() ) . '" title="' . esc_attr( __( 'Preview', 'wiaas' ) ) . '">' . esc_html( __( 'Preview', 'wiaas' ) ) . '</a>';
			}
		}
	}
}

Wiaas_Admin_CL_Orders::init();
