<?php
namespace Smackcoders\FUPESM;

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

class FUPESM_emaillog  extends \WP_List_Table{
    private $table_data;
    public function __construct()
    {
        parent::__construct([
            'singular' => 'logdata',  // Singular name for an item
            'plural'   => 'logdatas', // Plural name for items
            'ajax'     => false      // Enable AJAX?
        ]);
    }
    function column_default($item, $column_name) {
        return isset($item[$column_name]) ? $item[$column_name] : ''; // Avoid undefined index error
    }
    protected function get_sortable_columns()
        {
            $sortable_columns = array(
                    // 'id'  => array('id', false),
                    // 'status' => array('status', false),
            );
            return $sortable_columns;
        }
    function prepare_items() {
        if (((isset($_POST['email_log_nonce_field']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['email_log_nonce_field'])), 'email_log_nonce_action')) )||(isset($_REQUEST['page']) &&  sanitize_text_field(wp_unslash($_REQUEST['page'] == 'log-section')))) {            
        }
        else{
            die('Security check failed!');  
        }
        if (isset($_POST['s'])  ) {
            $search = sanitize_text_field(wp_unslash($_POST['s']));
            $this->table_data = $this->get_table_data($search);
        } else {
            $this->table_data = $this->get_table_data();
        }
        $columns = $this->get_columns();
        $hidden = ( is_array(get_user_meta( get_current_user_id(), 'managetoplevel_page_follow-up-emailscolumnshidden', true)) ) ? get_user_meta( get_current_user_id(), 'managetoplevel_page_supporthost_list_tablecolumnshidden', true) : array();
        $sortable = $this->get_sortable_columns();
        $primary  = 'id';
        // $hidden = array();
        // $sortable = array();
        
        $this->_column_headers = array($columns, $hidden, $sortable);
      //  usort($this->table_data, array(&$this, 'usort_reorder'));

           /* pagination */
           $per_page = $this->get_items_per_page('elements_per_page', 10);
           $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;    
    }
    
    function get_columns()
    {
        $columns = array(
                'id'            => __('ID', 'follow-up-emails'),
                'user_email'          => __('User Email', 'follow-up-emails'),
                'event'         => __('Event', 'follow-up-emails'),
                'status'   => __('Status', 'follow-up-emails'),
                'date'        => __('Date', 'follow-up-emails')
        );
        return $columns;
    }
    function get_table_data($search =''){
        global $wpdb;
        if ( !empty($search) ) {
            $search_new = "%".$search."%";
            $query = $wpdb->prepare("SELECT * from {$wpdb->prefix}smack_email_log WHERE event Like %s OR user_email Like %s OR status Like %s",'%failed%','%failed%','%failed%');
            $logdata = $wpdb->get_results($wpdb->prepare("SELECT * from {$wpdb->prefix}smack_email_log WHERE event Like %s OR user_email Like %s OR status Like %s",$search_new,$search_new,$search_new),ARRAY_A);
        } else {
            $logdata = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}smack_email_log",ARRAY_A);
        }
        return $logdata;

    }

}