<?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 amazilonListTable 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 $item[ $column_name ] . '<br /><a href="' . admin_url( 'admin.php?page=amazilon-xchange&asin=' . urlencode( $item[ $column_name ] ) . '&findSimilar#xchangenew' ) . ' ">' . __( 'Replace',
						'amazilon' ) . '</a>';
			case 'tstamp':
				return date_i18n( get_option( 'date_format' ),
						strtotime( $item[ $column_name ] ) ) . "\n" . date_i18n( get_option( 'time_format' ),
						strtotime( $item[ $column_name ] ) );
			case 'id':
			case 'msg':
			case 'shortcode':
				return $item[ $column_name ];
			case 'url':
				if ( substr( $item[ $column_name ], 0, 4 ) == 'http' ) {
					$iId = url_to_postid( $item[ $column_name ] );

					return ( ( (int) $iId > 0 && current_user_can( 'edit_posts' ) ) ? '<a href="' . get_edit_post_link( $iId ) . '">' . __( 'Edit' ) . '</a> | ' : '' ) . '<a href="' . esc_url( $item[ $column_name ] ) . '" target="_blank">' . $item[ $column_name ] . '</a><br />' . $item['title'];
				} else {
					return $item[ $column_name ] . '<br />' . $item['title'];
				}
			default:
				return 'N/A';
		}
	}

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

		return $sortable_columns;
	}

	function get_columns() {
		$page = ( isset( $_GET['page'] ) ) ? esc_attr( $_GET['page'] ) : false;
		switch ( $page ) {
			case 'amazilon-statistics':
				$columns = array(
					'cb'        => '<input type="checkbox" />',
					'tstamp'    => __( 'Occured on', 'amazilon' ),
					'shortcode' => __( 'Shortcode', 'amazilon' ),
					'asin'      => __( 'ASIN', 'amazilon' ),
					'msg'       => __( 'Message', 'amazilon' ),
					'url'       => __( 'URL / Product', 'amazilon' )
				);
				break;
		}

		return $columns;
	}

	function get_bulk_actions() {
		$actions = array(
			'e_delete'      => __( 'Delete', 'amazilon' ),
			'e_deleteAsin'  => __( 'Delete by ASIN', 'amazilon' ),
			'e_deleteUrl'   => __( 'Delete by URL', 'amazilon' ),
			'e_deleteGroup' => __( 'Delete by ASIN, URL and Message', 'amazilon' )
		);

		return $actions;
	}

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

	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'] ) ) ? $_REQUEST['orderby'] : 'tstamp';
		$order   = ( ! empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'desc';

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

		switch ( $page ) {
			case 'amazilon-statistics':
				$search      = ( ! empty( $_REQUEST['s'] ) ) ? ' AND (shortcode LIKE "%' . esc_sql( $_REQUEST['s'] ) . '%" OR asin LIKE "%' . esc_sql( $_REQUEST['s'] ) . '%" OR msg LIKE "%' . esc_sql( $_REQUEST['s'] ) . '%" OR url LIKE "%' . esc_sql( $_REQUEST['s'] ) . '%" OR titel LIKE "%' . esc_sql( $_REQUEST['s'] ) . '%")' : '';
				$this->items = $wpdb->get_results( 'SELECT id, asin, msg, url, tstamp, titel AS title, shortcode FROM ' . $wpdb->prefix . 'wpapn_errors WHERE 1' . $search . ' GROUP BY c32, asin, msg, url ORDER BY ' . esc_sql( $orderby ) . ' ' . esc_sql( $order ) . ' LIMIT ' . intval( ( $current_page - 1 ) * $per_page ) . ', ' . intval( $per_page ),
					ARRAY_A );
				$total_items = $wpdb->query( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'wpapn_errors WHERE 1' . $search . ' GROUP BY c32, asin, msg, url' );
				break;
		}

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

}

$amazilonListTable = new amazilonListTable();

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

    .wp-list-table .column-tstamp {
        width: 12em;
        white-space: pre;
    }

    .wp-list-table .column-asin {
        width: 9em;
    }

    .wp-list-table .column-msg {
        width: 20em;
    }

    .wp-list-table .column-shortcode {
        width: 8em;
    }

    .wp-list-table .column-urlproduct {
        width: 15em;
        overflow: hidden;
        white-space: nowrap;
    }
</style>
<p><b><?php _e( 'Errors-Dashboard – Non-Available Products', 'amazilon' ); ?></b></p>
<p><?php _e( 'This is a list of products, which are currently not available on Amazon but promoted on your website.',
		'amazilon' ); ?></p>
<?php
if ( isset( $_GET['m'] ) ) {
	switch ( $_GET['m'] ) {
		case '1':
			echo '<div class="notice notice-success"><p>' . __( 'The selected rows where successfully deleted.',
					'amazilon' ) . '</p></div>';
			break;
	}
}
?>

<?php $amazilonListTable->prepare_items(); ?>
<form method="post">
	<?php
	$amazilonListTable->search_box( 'Suchen', 'search' );
	$amazilonListTable->display();
	?>
</form>