<?php

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

class admin_list_table extends WP_List_Table {
  public function __construct() {
    parent::__construct(array(
      'singular' => 'Customer',  //singular name of the listed records
      'plural'   => 'Customers', //plural name of the listed records
      'ajax'     => false,       //should this table support ajax?
      'screen'   => null
   ));
  }

  public static function get_customers($per_page = 5, $page_number = 1) {
    global $wpdb;

    $sql = "SELECT * FROM {$wpdb->prefix}customers";

    if (!empty($_REQUEST['orderby'])) {
      $sql .= ' ORDER BY ' . esc_sql($_REQUEST['orderby']);
      $sql .= ! empty($_REQUEST['order']) ? ' ' . esc_sql($_REQUEST['order']) : ' ASC';
    }

    $sql .= " LIMIT $per_page";
    $sql .= ' OFFSET ' . ($page_number - 1) * $per_page;

    $result = $wpdb->get_results($sql, 'ARRAY_A');

    return $result;
  }

  public static function delete_customer($id) {
    global $wpdb;

    $wpdb->delete(
      "{$wpdb->prefix}customers",
      array('ID' => $id),
      array('%d')
   );
  }

  public static function record_count() {
    global $wpdb;

    $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}customers";
    return $wpdb->get_var($sql);
  }

  public function column_default($item, $column_name) {
    switch ($column_name) {
    case 'address':
    case 'city':
      return $item[$column_name];
    default:
      return print_r($item, true); //Show the whole array for troubleshooting purposes
    }
  }

  function column_cb($item) {
    return sprintf(
      '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['ID']
   );
  }

  function column_name($item) {
    $delete_nonce = wp_create_nonce('delete_customer');

    $title = '<strong>' . $item['name'] . '</strong>';

    $actions = array(
      'delete' => sprintf('<a href="?page=%s&action=%s&customer=%s&_wpnonce=%s">Delete</a>',
      esc_attr($_REQUEST['page']), 'delete', absint($item['ID']), $delete_nonce)
    );

    return $title . $this->row_actions($actions);
  }

  function get_columns() {
    $columns = array(
      'cb'      => '<input type="checkbox" />',
      'name'    => 'Name',
      'address' => 'Address',
      'city'    => 'City'
   );

    return $columns;
  }

  public function get_sortable_columns() {
    $sortable_columns = array(
      'name' => array('name', true),
      'city' => array('city', true)
   );

    return $sortable_columns;
  }

  public function get_bulk_actions() {
    $actions = array('bulk-delete' => 'Delete');

    return $actions;
  }

  public function prepare_items() {
    $this->_column_headers = $this->get_column_info();

    /** Process bulk action */
    $this->process_bulk_action();

    $per_page     = $this->get_items_per_page('customers_per_page', 5);
    $current_page = $this->get_pagenum();
    $total_items  = self::record_count();

    $this->set_pagination_args(array(
      'total_items' => $total_items, //WE have to calculate the total number of items
      'per_page'    => $per_page //WE have to determine how many items to show on a page
    ));

    $this->items = self::get_customers($per_page, $current_page);
  }

  public function process_bulk_action() {
  }
}