<?php

/**
* The admin-specific functionality of the plugin.
*
* @link       http://www.exapartners.com/
* @since      1.0.0
*
* @package    Esi_Management
* @subpackage Esi_Management/admin
*/

/**
* The admin-specific functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package    Esi_Management
* @subpackage Esi_Management/admin
* @author     Kremer Nicolas <nicolas.kremer@exapartners.com>
*/

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

/**
* List table class
*/
class Esi_Management_News_List extends \WP_List_Table {

    function __construct() {
        parent::__construct( array(
            'singular' => 'new',
            'plural'   => 'news',
            'ajax'     => false
        ) );
    }

    function get_table_classes() {
        return array( 'widefat', 'fixed', 'striped', $this->_args['plural'] );
    }

    /**
    * Message to show if no designation found
    *
    * @return void
    */
    function no_items() {
        _e( 'No news found', 'esi' );
    }

    /**
    * Default column values if no callback found
    *
    * @param  object  $item
    * @param  string  $column_name
    *
    * @return string
    */
    function column_default( $item, $column_name ) {

        switch ( $column_name ) {
            case 'title':
            return $item->title;

            case 'entity':
            return $item->entity;

            case 'media':
            return $item->media;

            case 'type':
            return $item->type;

            case 'region':
            return $item->region;

            case 'publish_date':
            return $item->publish_date;

            default:
            return isset( $item->$column_name ) ? $item->$column_name : '';
        }
    }

    /**
    * Get the column names
    *
    * @return array
    */
    function get_columns() {
        $columns = array(
            'cb'           => '<input type="checkbox" />',
            'title'      => __( 'Title', 'esi' ),
            'entity'      => __( 'Entity', 'esi' ),
            'media'      => __( 'Media', 'esi' ),
            'type'      => __( 'Type', 'esi' ),
            'region'      => __( 'Region', 'esi' ),
            'publish_date'      => __( 'Publish Date', 'esi' ),

        );

        return $columns;
    }

    /**
    * Render the designation name column
    *
    * @param  object  $item
    *
    * @return string
    */
    function column_title( $item ) {

        $actions           = array();
        $actions['edit']   = sprintf( '<a href="%s" data-id="%d" title="%s">%s</a>', admin_url( 'admin.php?page=esi-management-news&action=edit&id=' . $item->id ), $item->id, __( 'Edit this item', 'esi' ), __( 'Edit', 'esi' ) );
        $actions['view']   = sprintf( '<a href="%s" data-id="%d" title="%s">%s</a>', admin_url( 'admin.php?page=esi-management-news&action=view&id=' . $item->id ), $item->id, __( 'View this item', 'esi' ), __( 'View', 'esi' ) );
        $actions['delete'] = sprintf( '<a href="%s" class="submitdelete" data-id="%d" title="%s">%s</a>', admin_url( 'admin.php?page=esi-management-news&action=delete&id=' . $item->id ), $item->id, __( 'Delete this item', 'esi' ), __( 'Delete', 'esi' ) );

        return sprintf( '<a href="%1$s"><strong>%2$s</strong></a> %3$s', admin_url( 'admin.php?page=esi-management-news&action=view&id=' . $item->id ), $item->title, $this->row_actions( $actions ) );
    }

    /**
    * Get sortable columns
    *
    * @return array
    */
    function get_sortable_columns() {
        $sortable_columns = array(
            'name' => array( 'name', true ),
        );

        return $sortable_columns;
    }

    /**
    * Set the bulk actions
    *
    * @return array
    */
    function get_bulk_actions() {
        $actions = array(
            'trash'  => __( 'Move to Trash', 'esi' ),
        );
        return $actions;
    }

    /**
    * Render the checkbox column
    *
    * @param  object  $item
    *
    * @return string
    */
    function column_cb( $item ) {
        return sprintf(
        '<input type="checkbox" name="new_id[]" value="%d" />', $item->id
    );
}

/**
* Set the views
*
* @return array
*/
public function get_views_() {
    $status_links   = array();
    $base_link      = admin_url( 'admin.php?page=sample-page' );

    foreach ($this->counts as $key => $value) {
        $class = ( $key == $this->page_status ) ? 'current' : 'status-' . $key;
        $status_links[ $key ] = sprintf( '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>', add_query_arg( array( 'status' => $key ), $base_link ), $class, $value['label'], $value['count'] );
    }

    return $status_links;
}

/**
* Prepare the class items
*
* @return void
*/
function prepare_items() {

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

    $per_page              = 20;
    $current_page          = $this->get_pagenum();
    $offset                = ( $current_page -1 ) * $per_page;
    $this->page_status     = isset( $_GET['status'] ) ? sanitize_text_field( $_GET['status'] ) : '2';

    // only ncessary because we have sample data
    $args = array(
        'offset' => $offset,
        'number' => $per_page,
    );

    if ( isset( $_REQUEST['orderby'] ) && isset( $_REQUEST['order'] ) ) {
        $args['orderby'] = $_REQUEST['orderby'];
        $args['order']   = $_REQUEST['order'] ;
    }

    $this->items  = esi_get_all_new( $args );

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

    /**
    * Extra controls to be displayed between bulk actions and pagination
    *
    * @since 3.1.0
    * @access protected
    *
    * @param string $which
    */
    protected function extra_tablenav( $which ) {

        global $wpdb, $testiURL, $tablename, $tablet;
        if ( $which == "top" ){
            ?>
            <div class="alignleft actions bulkactions">
                <?php
                // entity
                $companies = esi_get_all_entity();
                if( $companies ){
                    ?>
                    <select name="entity-filter" class="esi-filter-entity">
                        <option value="">Filter by entity</option>
                        <?php
                        foreach( $companies as $entity ){
                            $selected = '';
                            if( $_GET['entity-filter'] == $entity->id ){
                                $selected = ' selected = "selected"';
                            }
                            ?>
                            <option value="&entity-filter=<?= $entity->id; ?>" <?php echo $selected; ?>><?php echo $entity->name; ?></option>
                            <?php

                        }
                        ?>
                    </select>
                    <?php
                }
                // MEDIA
                $medias = esi_get_all_media();
                if( $medias ){
                    ?>
                    <select name="media-filter" class="esi-filter-media">
                        <option value="">Filter by Media</option>
                        <?php
                        foreach( $medias as $media ){
                            $selected = '';
                            if( $_GET['media-filter'] == $media->id ){
                                $selected = ' selected = "selected"';
                            }
                            ?>
                            <option value="&media-filter=<?= $media->id; ?>" <?php echo $selected; ?>><?php echo $media->name; ?></option>
                            <?php

                        }
                        ?>
                    </select>
                    <?php
                }
                // TYPE
                $types = esi_get_all_type();
                if( $types ){
                    ?>
                    <select name="type-filter" class="ewc-filter-type">
                        <option value="">Filter by Type</option>
                        <?php
                        foreach( $types as $type ){
                            $selected = '';
                            if( $_GET['type-filter'] == $type->id ){
                                $selected = ' selected = "selected"';
                            }
                            ?>
                            <option value="&type-filter=<?= $type->id; ?>" <?php echo $selected; ?>><?php echo $type->name; ?></option>
                            <?php

                        }
                        ?>
                    </select>
                    <?php
                }
                // REGION
                $regions = esi_get_all_region();
                if( $regions ){
                    ?>
                    <select name="region-filter" class="ewc-filter-region">
                        <option value="">Filter by Region</option>
                        <?php
                        foreach( $regions as $region ){
                            $selected = '';
                            if( $_GET['region-filter'] == $region->id ){
                                $selected = ' selected = "selected"';
                            }
                            ?>
                            <option value="&region-filter=<?= $region->id; ?>" <?php echo $selected; ?>><?php echo $region->name; ?></option>
                            <?php

                        }
                        ?>
                    </select>
                    <?php
                }
                ?>
            </div>
            <?php
            submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
        }
        if ( $which == "bottom" ){
            //The code that goes after the table is there

        }

    }
}
