<?php

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

function view_race_leader_list_initialisation()
{
    global $wpdb;

    $table = new CASS_View_Race_Leader_List();
    ?>
    <div class="wrap">
        <h2>Liste des chefs de courses</h2>
    <?php

            // Prepare table
            $table->prepare_items();
            // Display table
            $table->display();
            echo '</div></form>';

}

class CASS_View_Race_Leader_List extends WP_List_Table
{
    private $table_data;

    // Define table columns
    function get_columns()
    {
        $columns = array(
            'cb' => '<input type="checkbox" />',
            'lastname' => __('Nom', 'cass-race-user-lastname-content'),
            'firstname' => __('Prénom', 'cass-race-user-firstname-content'),
            'memberno' => __('N° de membre', 'ccass-race-user-memberno-content'),
        );
        return $columns;
    }


    function prepare_items()
    {
        $this->table_data = $this->get_table_data();

        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);

        usort($this->table_data, array(&$this, 'usort_reorder'));

        /* pagination */
        $per_page = 20;
        $current_page = $this->get_pagenum();
        $total_items = count($this->table_data);

        $this->table_data = array_slice($this->table_data, (($current_page - 1) * $per_page), $per_page);

        $this->set_pagination_args(
            array(
                'total_items' => $total_items,
                // total number of items
                'per_page' => $per_page,
                // items to show on a page
                'total_pages' => ceil($total_items / $per_page) // use ceil to round up
            )
        );

        $this->items = $this->table_data;
    }

    // Get table data
    private function get_table_data()
    {
        $args = array(
            'role'    => 'CASS_RL',
            'orderby' => 'user_nicename',
            'order'   => 'ASC'
        );
        $users = get_users( $args );

        return $users;
    }
    function column_default($item, $column_name)
    {
        switch ($column_name) {
            case "lastname":  
                return $item->user_lastname;          
            case "firstname":
               return $item->user_firstname; 
             case "memberno":
               return get_user_meta( $item->ID, 'cassMemberNo', true );
        default:
        return $item;
        }
    }

    function column_cb($item)
    {
        return sprintf(
            '<input type="checkbox" name="element[]" value="%s" />',
            $item->id
        );
    }
    protected function get_sortable_columns()
    {
        $sortable_columns = array(
            'lastname' => array('lastname', false),
            'firstname' => array('firstname', false),
            'memberno' => array('memberno', false),
        );
        return $sortable_columns;
    }

    // Sorting function
    function usort_reorder($a, $b)
    {
        // If no sort, default to user_login
        $orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'user_login';

        // If no order, default to asc
        $order = (!empty($_GET['order'])) ? $_GET['order'] : 'asc';

        // Determine sort order
        $result = strcmp($a[$orderby], $b[$orderby]);

        // Send final sort direction to usort
        return ($order === 'asc') ? $result : -$result;
    }

    // Adding action links to column
   function column_lastname($item)
    {
                $actions = array(
                    //Race_user
                   'allCoursesMemeber' => sprintf('<a href="' . admin_url() . "admin.php?page=cass-admin-race-user-all-courses-list&element=" . $item->id . '">Liste de ses courses (membre)</a>', $_REQUEST['page'], 'allCoursesMemeber', $item->id),
                   'allCoursesLeader' => sprintf('<a href="' . admin_url() . "admin.php?page=cass-admin-race-leader-all-courses-list&element=" . $item->id . '">Liste de ses courses (Chef de course)</a>', $_REQUEST['page'], 'allCoursesLeader', $item->id),
                    
                );

                   return sprintf('%1$s %2$s', $item->user_lastname, $this->row_actions($actions));
    }
}