<?php
if (! class_exists('WP_List_Table') ) {
    include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}/**
  * List table class
  */
class Stylish_Cost_Calculator_Tabs_List extends \WP_List_Table
{
    function __construct()
    {
        parent::__construct(
            array(
                'singular' => 'List',
                'plural'   => 'Lists',
                '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()
    {
        esc_html_e('No Lists Found', 'scc');
    }
    /**
     * 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 'id':
            return $item->id;
            case 'list_name':
            return $item->list_name;
            case 'shortcode':
            return $item->shortcode;
            default:
            return isset($item->$column_name) ? $item->$column_name : '';
        }
    }
    /**
     * Get the column names
     *
     * @return array
     */
    function get_columns()
    {
        $columns = array(
            'cb'           => '<input type="checkbox" />',
            'id'      => __('List Name', 'scc'),
            // 'list_name'      => __( 'List Name', 'scc' ),
            'shortcode'      => __('Shortcode', 'scc'),
        );
        return $columns;
    }
    /**
     * Render the designation name column
     *
     * @param object $item
     *
     * @return string
     */
    function column_id( $item )
    {
        $actions           = array();
        $actions['edit']   = sprintf('<a href="%s" data-id="%d" title="%s">%s</a>', admin_url('admin.php?page=scc-tabs&action=edit&id=' . $item->id), $item->id, __('Edit this item', 'scc'), __('Edit', 'scc'));
        $actions['delete'] = sprintf('<a href="%s" class="submitdelete" data-id="%d" title="%s">%s</a>', admin_url('admin.php?page=scc-tabs&action=delete&id=' . $item->id), $item->id, __('Delete this item', 'scc'), __('Delete', 'scc'));
        return sprintf('<a href="%1$s"><strong>%2$s</strong></a> %3$s', admin_url('admin.php?page=scc-tabs&action=edit&id=' . $item->id), $item->list_name, $this->row_actions($actions));
    }
    function column_refer( $item )
    {
        return sprintf('<a href="%s" data-id="%d" title="%s">%s</a>', admin_url('admin.php?page=scc-tabs&action=readonly&id=' . $item->id), $item->id, __('#' .$item->id, 'c9s'), __('#' . $item->id, 'c9s'));
    }
    /**
     * 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(
            'delete'  => __('Delete Selected Items', 'scc'),
        );
        return $actions;
    }
    /**
     * Render the checkbox column
     *
     * @param object $item
     *
     * @return string
     */
    function column_cb( $item )
    {
        return sprintf(
            '<input type="checkbox" name="ids[]" 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';
        $search     = isset($_REQUEST['s']) ? sanitize_text_field($_REQUEST['s']) : '';
        // only ncessary because we have sample data
        $args = array(
            'offset' => $offset,
            'number' => $per_page,
            'search' => '*'.$search .'*',
        );
        if (isset($_REQUEST['orderby']) && isset($_REQUEST['order']) ) {
            $args['orderby'] = $_REQUEST['orderby'];
            $args['order']   = $_REQUEST['order'] ;
        }
        $this->items  = scc_get_all_tabs($args);
        $this->set_pagination_args(
            array(
                'total_items' => scc_get_tabs_coun(),
                'per_page'    => $per_page
            ) 
        );
    }
}?>