<?php

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

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

class amazilonListTableXchange extends WP_List_Table {
	function __construct() {
		parent::__construct( array(
			'singular' => __( 'name' ),     //singular name of the listed records
			'plural'   => __( 'names' ),   //plural name of the listed records
			'ajax'     => false        //does this table support ajax?
		) );
	}

	function no_items() {
		_e( 'No entries found.' );
	}

	function column_default( $item, $column_name ) {
		switch ( $column_name ) {
			case 'asin':
				return '<a href="https://amazon.de/dp/' . urlencode( $item[ $column_name ] ) . '/" target="_blank">' . $item[ $column_name ] . '</a><br /><small><i>' . __( 'was',
						'amazilon' ) . ':</i> ' . esc_html( $item['title'] ) . '</small>';
			case 'asinNew':
				return '<a href="https://amazon.de/dp/' . urlencode( $item[ $column_name ] ) . '/" target="_blank">' . $item[ $column_name ] . '</a><br /><small><i>' . __( 'was',
						'amazilon' ) . ':</i> ' . esc_html( $item['titleNew'] ) . '</small>';
			case 'cb':
				return '<input type="checkbox" />';
			case 'id':
				return $item[ $column_name ];
			case 'actions':
				return '<a href="#" onclick="jQuery(\'input[type=checkbox]\').prop(\'checked\', false);jQuery(\'select#bulk-action-selector-top\').val(\'e_delete\');jQuery(\'input[type=checkbox][value=' . (int) $item['id'] . ']\').prop(\'checked\', true);jQuery(\'input[type=submit]#doaction\').click();return false;">' . __( 'Delete',
						'amazilon' ) . '</a>';
			default:
				return 'N/A';
		}
	}

	function column_cb( $item ) {
		return sprintf( '<input type="checkbox" name="id[]" value="%s" />', $item['id'] );
	}

	function get_sortable_columns() {
		$sortable_columns = array(
			'asin'    => array( 'asin', true ),
			'asinNew' => array( 'asinNew', true )
		);

		return $sortable_columns;
	}

	function get_columns() {
		$columns = array(
			'cb'      => '<input type="checkbox" />',
			'asin'    => __( 'ASIN', 'amazilon' ),
			'asinNew' => __( 'ASIN to use', 'amazilon' ),
			'actions' => __( 'Actions', 'amazilon' )
		);

		return $columns;
	}

	function get_bulk_actions() {
		$actions = array(
			'e_delete' => __( 'Delete', 'amazilon' )
		);

		return $actions;
	}

	function read_data() {
	}

	function prepare_items() {
		global $wpdb;
		$page                  = ( isset( $_GET['page'] ) ) ? esc_attr( $_GET['page'] ) : false;
		$columns               = $this->get_columns();
		$hidden                = array();
		$sortable              = $this->get_sortable_columns();
		$this->_column_headers = array( $columns, $hidden, $sortable );

		$orderby = ( ! empty( $_REQUEST['orderby'] ) && isset( $sortable[ $_REQUEST['orderby'] ] ) ) ? $_REQUEST['orderby'] : 'id';
		$order   = ( ! empty( $_REQUEST['order'] ) && in_array( $_REQUEST['order'],
				array( 'asc', 'desc' ) ) ) ? $_REQUEST['order'] : 'desc';

		$per_page     = ( ! empty( $_REQUEST['per_page'] ) ) ? intval( $_REQUEST['per_page'] ) : 10;
		$current_page = $this->get_pagenum();

		$search .= ( ! empty( $_REQUEST['s'] ) ) ? ' AND ( asin = "' . esc_sql( $_REQUEST['s'] ) . '" OR asinNew = "' . esc_sql( $_REQUEST['s'] ) . '" )' : '';

		$this->items = $wpdb->get_results( 'SELECT id, asin, title, asinNew, titleNew FROM ' . $wpdb->prefix . 'amazilon_xchange WHERE 1' . $search . ' ORDER BY ' . esc_sql( $orderby ) . ' ' . esc_sql( $order ) . ' LIMIT ' . intval( ( $current_page - 1 ) * $per_page ) . ', ' . intval( $per_page ),
			ARRAY_A );
		$total_items = $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'amazilon_xchange WHERE 1' . $search );

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

}

$amazilonListTableXchange = new amazilonListTableXchange();

?>
<style type="text/css">
    .wp-list-table .column-cb {
        width: 3em;
    }

    .wp-list-table .column-asin, .wp-list-table .column-asinNew {
        width: 20em;
    }

    .wp-list-table .column-actions {
        width: 9em;
    }
</style>
<?php $amazilonListTableXchange->prepare_items(); ?>
<form method="post">
	<?php
	$amazilonListTableXchange->search_box( 'Suchen', 'amazilon-search' );
	$amazilonListTableXchange->display();
	?>
</form>
<br style="clear: both;">