<?php
/**
 * Copyright (C) 2019-2025 Paladin Business Solutions
 *
 */

global $print_again, $wpdb;  

if( ! class_exists( 'WP_List_Table' ) ) {
    // in case the class is ever removed from WP Core use this copy
    require_once( ABSPATH . 'wp-content/plugins/ringcentral/includes/wp_list_class/class-wp-list-table.php' );
}

class RC_CallMe_List_Table extends WP_List_Table {
    /* ===== constructor ==== */
    function __construct() {
        global $status, $page;
        parent::__construct( array(
            'singular'  => 'CallMe',   //singular name of the listed records
            'plural'    => 'CallMes'  //plural name of the listed records 	           
        ) );
    }
    
    function column_default($item, $column_name) {
        return $item[$column_name];
    }    
    function column_mobile($item) {              
        return '<a href="tel:'.$item['mobile'].'">'.$item['mobile'].'</a>' ;
    }    
    function column_call_reason($item) {
        return '<em>' . stripslashes($item['call_reason']) . '</em>';
    }
    
    function column_full_name($item) {
        // $_REQUEST['page'] used so action will be done on curren page in delete a href string
        
        $actions = array(            
            'delete' => sprintf('<a href="?page=%s&action=delete&id=%s">%s</a>', $_REQUEST['page'], $item['ringcentral_call_requests_id'], 'Delete')
        );
        return sprintf('%s %s',
            $item['full_name'],
            $this->row_actions($actions)
            );
    }    
       
    function column_cb($item) {
        return sprintf(
            '<input type="checkbox" name="id[]" value="%s" />',
            $item['ringcentral_call_requests_id']
            );
    }
    
    function get_columns() {
        $columns = array(
            'cb'                    => '<input type="checkbox" />', //Render a checkbox instead of text
            'full_name'             => 'Requested Caller Name',
            'mobile'                => 'Mobile #',
            'requested_date_time'   => 'Call Date / Time',
            'call_reason'           => 'Call Reason'
        );
        return $columns;
    }
    
    function get_sortable_columns(){
        $sortable_columns = array(
            'full_name'         => array('full_name',true), //true means it's already sorted
            'requested_date_time'         => array('requested_date',false) //true means it's already sorted
        );
        return $sortable_columns;
    }
    
    function get_bulk_actions() {
        $actions = array(
            'delete' => 'Delete'           
        );
        return $actions;
    }
    
    function process_bulk_action() {
        global $wpdb;
        $table_name = 'ringcentral_call_requests'; 
        if ('delete' === $this->current_action()) {
            $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
            if (is_array($ids)) $ids = implode(',', $ids);
            if (!empty($ids)) {
                $wpdb->query("DELETE FROM $table_name WHERE ringcentral_call_requests_id IN($ids)");
            }
        }
    }
    
    function prepare_items($search = NULL) {
        global $wpdb;
        $table_name = 'ringcentral_call_requests'; 
        $per_page = 10; // constant, how much records will be shown per page
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        // here we configure table headers, defined in our methods
        $this->_column_headers = array($columns, $hidden, $sortable);
        // [OPTIONAL] process bulk action if any
        $this->process_bulk_action();
        // will be used in pagination settings
        $total_items = $wpdb->get_var("SELECT COUNT(`ringcentral_call_requests_id`) FROM `$table_name`");
        // prepare query params, as usual current page, order by and order direction
        $paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged']) - 1) : 0;
        $orderby = (isset($_REQUEST['orderby']) && in_array($_REQUEST['orderby'], array_keys($this->get_sortable_columns()))) ? $_REQUEST['orderby'] : 'full_name';
        $order = (isset($_REQUEST['order']) && in_array($_REQUEST['order'], array('asc', 'desc'))) ? $_REQUEST['order'] : 'asc';

        /* If the value is not NULL, do a search for it. */
        if( $search != NULL ){
            // Trim and sanitize Search Term
            $search = trim($search) ;
            
            /* Notice how you can search multiple columns for your search term easily, and return one data set */
            $sql = $wpdb->prepare("SELECT * FROM $table_name WHERE `full_name` LIKE '%%%s%%' OR `mobile` LIKE '%%%s%%'
                ORDER BY $orderby $order LIMIT %d OFFSET %d", $search, $search, $per_page, $paged);
           // echo $sql ;
            $this->items = $wpdb->get_results($sql, ARRAY_A);            
        } else {
            $this->items = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name
                ORDER BY $orderby $order LIMIT %d OFFSET %d", $per_page, $paged), ARRAY_A);
        }
        
        // [REQUIRED] configure pagination
        $this->set_pagination_args(array(
            'total_items' => $total_items, // total items defined above
            'per_page' => $per_page, // per page constant defined at top of method
            'total_pages' => ceil($total_items / $per_page) // calculate pages count
        ));
    }

} //class

$myListTable = new RC_CallMe_List_Table();

if( isset($_REQUEST['s']) ){
    $myListTable->prepare_items($_REQUEST['s']);
} else {
    $myListTable->prepare_items();
}

$current_action = $myListTable->current_action() ;

if ($current_action !== 'delete' && $current_action !== 'edit') {
    ?>
    <div style="background:#ECECEC;border:1px solid #CCC;padding:0 10px;margin-top:5px;border-radius:5px;-moz-border-radius:5px;-webkit-border-radius:5px;">
    	<p>Manage your call me requests from this page.</p>
    </div>   
    <?php 
}

if ($current_action === 'delete') {
    $message = "<div class='updated below-h2' id='message'><p>Item(s) successfully deleted.</p></div>";
}
?>
    <h2>Call Me Requests</h2>   
    <?php echo $message; ?>             
<!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
<form method="get">
    <!-- For plugins, we also need to ensure that the form posts back to our current page -->
    <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
    <?php $myListTable->search_box('Search', 'search_id'); ?>
    <!-- Now we can render the completed list table -->
    <?php $myListTable->display() ?>
</form>

<?php ringcentral_plugin_version() ; ?>