<?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_Investment_List extends \WP_List_Table {

    function __construct() {
        parent::__construct( array(
            'singular' => 'investment',
            'plural'   => 'investment',
            '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 investment 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 'entity':
            return $item->entity;

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

            case 'investor':
            return implode(', ', $item->investor);

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

            case 'investment_date':
            return date ("d/m/Y", strtotime($item->investment_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" />',
            'entity'      => __( 'entity', 'esi' ),
            'investor'      => __( 'Investor', 'esi' ),
            'investment_type'      => __( 'Investment Type', 'esi' ),
            'amount'      => __( 'Amount', 'esi' ),
            'investment_date'      => __( 'Investment Date', 'esi' )
        );

        return $columns;
    }

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

        $actions           = array();
        $actions['edit']   = sprintf( '<a href="%s" data-id="%d" name="%s">%s</a>', admin_url( 'admin.php?page=esi-management-investment&action=edit&id=' . $item->id ), $item->id, __( 'Edit this item', 'esi' ), __( 'Edit', 'esi' ) );
        $actions['delete'] = sprintf( '<a href="%s" class="submitdelete" data-id="%d" name="%s">%s</a>', admin_url( 'admin.php?page=esi-management-investment&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-investment&action=view&id=' . $item->id ), $item->entity, $this->row_actions( $actions ) );
    }

    /**
    * Get sortable columns
    *
    * @return array
    */
    function get_sortable_columns() {
        $sortable_columns = array(
            'entity' => array( 'entity', 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="investment_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_investment( $args );

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