
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

if ( ! class_exists('WP_List_Table')) {
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}

use GigaAI\Storage\Eloquent\Lead as Lead;

/**
 * Create a new table class that will extend the WP_List_Table
 */
class Lead_List_Table extends \WP_List_Table
{
    public $leads = [];
    
    public $per_page = 20;
    
    public $total = 0;
    
    public $instances = [];
    
    /**
     * Prepare the items for the table to process
     *
     * @return Void
     */
    public function prepare_items()
    {
        $columns = $this->get_columns();
        $hidden = $this->get_hidden_columns();
        $sortable = $this->get_sortable_columns();
        
        $currentPage = $this->get_pagenum();
        
        
        $s = isset($_REQUEST['s']) ? $_REQUEST['s'] : '';
        $source = isset($_REQUEST['instance_id']) ? $_REQUEST['instance_id'] : null;
        
        $this->total = Lead::ofSource($source)->search($s)->count();
        $data = Lead::ofSource($source)->search($s)->take($this->per_page)->skip(($currentPage - 1) * $this->per_page)
            ->get()->toArray();
        
        $this->instances = \GigaAI\Storage\Eloquent\Instance::lists('name', 'id');
        
        $this->set_pagination_args([
            'total_items' => $this->total,
            'per_page'    => $this->per_page,
        ]);
        
        $this->_column_headers = [$columns, $hidden, $sortable];
        
        $this->items = $data;
    }
    
    /**
     * Override the parent columns method. Defines the columns to use in your listing table
     *
     * @return array
     */
    public function get_columns()
    {
        return [
            'name'       => __('Name', 'giga-messenger-bots'),
            'email'      => __('Email', 'giga-messenger-bots'),
            'phone'      => __('Phone', 'giga-messenger-bots'),
            'source'     => __('FB Page', 'giga-messenger-bots'),
            'subscribe'  => __('Subscribe', 'giga-messenger-bots'),
            'auto_stop'  => __('Status', 'giga-messenger-bots'),
            'created_at' => __('Created At', 'giga-messenger-bots'),
        ];
    }
    
    /**
     * Displays the search box.
     *
     * @since  3.1.0
     * @access public
     *
     * @param string $text     The 'submit' button label.
     * @param string $input_id ID attribute value for the search input field.
     */
    public function search_box($text, $input_id)
    {
        if (empty($_REQUEST['s']) && ! $this->has_items()) {
            return;
        }
        
        $input_id = $input_id . '-search-input';
        ?>
        <p class="search-box">
            <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo $text; ?>:</label>
            <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
                   value="<?php _admin_search_query(); ?>"/>
            <?php submit_button($text, 'button', '', false, ['id' => 'search-submit']); ?>
        </p>
        <?php
    }
    
    protected function extra_tablenav($which)
    {
        if ('top' !== $which) {
            return;
        }
        ?>
        <div class="alignleft actions">
            <?php
            $this->instances_dropdown();
            
            submit_button(__('Filter'), 'button', 'filter_action', false, ['id' => 'post-query-submit']);
            ?>
        </div>
        <?php
    }
    
    private function instances_dropdown()
    {
        echo '<label class="screen-reader-text" for="instances">' . __('Filter by instance', 'giga-messenger-bots') . '</label>';
        $instances = giga_get_instances();
        $instance_id = isset($_REQUEST['instance_id']) ? trim($_REQUEST['instance_id']) : 0;
        ?>
        <select name="instance_id" id="instances">
            <option value="0"><?php _e('Select page', 'giga-messenger-bots'); ?></option>
            <?php foreach ($instances as $instance) : ?>
                <option<?php selected($instance_id, $instance['id']); ?>
                        value="<?php echo $instance['id'] ?>"><?php echo $instance['name'] ?></option>
            <?php endforeach; ?>
        </select>
        <?php
    }
    
    public function display_tablenav($which)
    {
        ?>
        <div class="tablenav <?php echo esc_attr($which); ?>">
            
            <?php
            $this->bulk_actions();
            $this->extra_tablenav($which);
            $this->pagination($which);
            ?>
            <br class="clear"/>
        </div>
        <?php
    }
    
    /**
     * Define which columns are hidden
     *
     * @return array
     */
    public function get_hidden_columns()
    {
        return [];
    }
    
    /**
     * Define the sortable columns
     *
     * @return array
     */
    public function get_sortable_columns()
    {
        return [
            'name' => ['name', false],
        ];
    }
    
    /**
     * Define what data to show on each column of the table
     *
     * @param  array  $item        Data
     * @param  String $column_name - Current column name
     *
     * @return Mixed
     */
    public function column_default($item, $column_name)
    {
        if ($column_name === 'name') {
            $name = $item['first_name'] . ' ' . $item['last_name'];
            
            $redirect_to = add_query_arg(['view' => 'lead', 'lead_id' => $item['user_id']]);
            
            return "<img src='{$item['profile_pic']}' alt='Profile Pic' width='32' height='32' class='avatar avatar-32 photo'> 
                    <strong><a href='{$redirect_to}'>{$name}</a></strong>
            ";
        }
        if ($column_name === 'subscribe') {
            $item[$column_name] = $item[$column_name] == false ?
                '<span class="light light-error" title="' . __('No', 'giga-messenger-bots') . '"></span>' :
                '<span class="light light-active" title="' . __('Yes', 'giga-messenger-bots') . '"></span>';
        }
        
        if ($column_name === 'source') {
            $item[$column_name] = isset($this->instances[$item['source']]) ? $this->instances[$item['source']] : 'N/A';
        }
        
        if ($column_name === 'auto_stop') {
            $item[$column_name] = ! empty($item[$column_name]) ?
                '<span class="light light-error" title="' . __('Waiting Answers from Page Administrators', 'giga-messenger-bots') . '"></span>' :
                '<span class="light light-active" title="' . __('Active', 'giga-messenger-bots') . '"></span>';
        }
        
        return $item[$column_name];
    }
}