<?php
if(is_admin())
{
    new Paulund_Wp_List_Table();
}
/**
 * Paulund_Wp_List_Table class will create the page to load the table
 */
class Paulund_Wp_List_Table
{
    /**
     * Constructor will create the menu item
     */
    public function __construct()
    {
        add_action( 'admin_menu', array($this, 'add_menu_example_list_table_page' ));
    }
    /**
     * Menu item will allow us to load the page to display the table
     */
    public function add_menu_example_list_table_page()
    {
        add_submenu_page( 'edit.php?post_type=event', 'Event Registrants', 'Event Registrants','manage_options', 'event-registrants', array($this, 'list_table_page') );
    }
    /**
     * Display the list table page
     *
     * @return Void
     */
    public function list_table_page()
    {
        $exampleListTable = new Example_List_Table();
        $exampleListTable->prepare_items();
        ?>
            <div class="wrap">
                <div id="icon-users" class="icon32"></div>
                <h2>Example List Table Page</h2>
                <?php $exampleListTable->display(); ?>
            </div>
        <?php
    }
}
// WP_List_Table is not loaded automatically so we need to load it in our application
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 Example_List_Table extends WP_List_Table
{
    /**
     * Prepare the items for the table to process
     *
     * @return Void
     */
    public function prepare_items()
    {
        $columns = $this->get_columns();
        $hidden = $this->get_hidden_columns();
        $sortable = $this->get_sortable_columns();
        $data = $this->table_data();
        //usort( $data, array( &$this, 'sort_data' ) );
        $perPage = 20;
        $currentPage = $this->get_pagenum();
        $totalItems = count($data);
        $this->set_pagination_args( array(
            'total_items' => $totalItems,
            'per_page'    => $perPage
        ) );
        $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
        $this->_column_headers = array($columns, $hidden, $sortable);
        $this->items = $data;
    }
    /**
     * Override the parent columns method. Defines the columns to use in your listing table
     *
     * @return Array
     */
    public function get_columns()
    {
        $columns = array(
            'event'						=> 'Event',
						'ticket'					=> 'Ticket No.',
						'date'						=> 'Date(Y-m-d)',
            'registrant'			=> 'Registrant',
            'email'						=> 'Email',
            'phone'        		=> 'Phone',
            'ticket1'    			=> 'Ticket 1',
            'ticket2'      		=> 'Ticket 2',
						'ticket3'      		=> 'Ticket 3',
						'tx'							=> 'Transaction',
						'delete'					=> 'Delete'
        );
        return $columns;
    }
    /**
     * Define which columns are hidden
     *
     * @return Array
     */
    public function get_hidden_columns()
    {
        return array();
    }
    /**
     * Define the sortable columns
     *
     * @return Array
     */
    public function get_sortable_columns()
    {
        return array('title' => array('title', false));
    }
    /**
     * Get the table data
     *
     * @return Array
     */
    private function table_data()
    {
        $data = array();
				$registrant_args = array('post_type'=>'adore_registrant', 'posts_per_page'=>-1, 'order'=>'DESC', 'orderby'=>'ID');
				$registrant_listings = new WP_Query($registrant_args);
				if($registrant_listings->have_posts()):while($registrant_listings->have_posts()):$registrant_listings->the_post();
				$registrant_event = get_post_meta(get_the_ID(), 'adore_registrant_event', true);
				$ticket_id = get_post_meta(get_the_ID(), 'adore_registrant_ticket_no', true);
				$event_date = get_post_meta(get_the_ID(), 'adore_event_date', true);
				$registrant_name = get_post_meta(get_the_ID(), 'adore_registrant_name', true);
				$registrant_email = get_post_meta(get_the_ID(), 'adore_registrant_email', true);
				$registrant_phone = get_post_meta(get_the_ID(), 'adore_registrant_phone', true);
				$registrant_ticket1 = get_post_meta(get_the_ID(), 'adore_registrant_ticket1', true);
				$registrant_ticket2 = get_post_meta(get_the_ID(), 'adore_registrant_ticket2', true);
				$registrant_ticket3 = get_post_meta(get_the_ID(), 'adore_registrant_ticket3', true);
				$transaction = get_post_meta(get_the_ID(), 'adore_registrant_tx', true);
        $data[] = array(
                    'event'          => get_the_title($registrant_event),
										'ticket'					=> $ticket_id,
										'date'						=> $event_date,
                    'registrant'     => $registrant_name,
                    'email' 					=> $registrant_email,
                    'phone'						=> $registrant_phone,
                    'ticket1'    		=> $registrant_ticket1,
                    'ticket2'      	=> $registrant_ticket2,
										'ticket3'				=> $registrant_ticket3,
										'tx'							=> $transaction,
										'delete' 					=> '<span class="delete-registrant" id="'.get_the_ID().'">X</span><span class="remove-registrant" id="remove-'.get_the_ID().'" style="display:none;">Delete</span>'
                    );
				endwhile; endif; wp_reset_postdata();
        return $data;
    }
    /**
     * 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 )
    {
        switch( $column_name ) {
            case 'event':
						case 'ticket':
						case 'date':
            case 'registrant':
            case 'email':
            case 'phone':
            case 'ticket1':
            case 'ticket2':
						case 'ticket3':
						case 'tx':
						case 'delete':
                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ;
        }
    }
    /**
     * Allows you to sort the data by the variables set in the $_GET
     *
     * @return Mixed
     */
    private function sort_data( $a, $b )
    {
        // Set defaults
        $orderby = 'title';
        $order = 'asc';
        // If orderby is set, use this as the sort column
        if(!empty($_GET['orderby']))
        {
            $orderby = $_GET['orderby'];
        }
        // If order is set use this as the order
        if(!empty($_GET['order']))
        {
            $order = $_GET['order'];
        }
        $result = strcmp( $a[$orderby], $b[$orderby] );
        if($order === 'asc')
        {
            return $result;
        }
        return -$result;
    }
}