<?php





if (!class_exists('WP_List_Table')) {

    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

}



class CitiesList extends WP_List_Table {



    function __construct() {

        parent::__construct(array(

            'singular' => 'City',

            'plural' => 'Cities',

            'ajax' => false

        ));

        

    }



    function get_columns() {

        $columns = array(

            'cb' => '<input type="checkbox" />', //Render a checkbox instead of text

            'id' => 'ID',

            'name' => 'City Name'
            

        );

        return $columns;

    }



     function get_bulk_actions() {

        $actions = array(

            'delete' => 'Delete'

        );

        return $actions;

    }



    function process_bulk_action() {

        //Detect when a bulk action is being triggered... 

        if ('delete' === $this->current_action()) {

            $records_to_delete = array_map( 'sanitize_text_field', $_GET['user'] );

            if (empty($records_to_delete)) {

                echo '<div id="message" class="updated fade"><p>Error! You need to select multiple records to perform a bulk action!</p></div>';

                return;

            }

            foreach ($records_to_delete as $record_id) {

                if( !is_numeric( $record_id )){

                    wp_die('Error! ID must be numeric.');

                }                

                global $wpdb;

                $users = $wpdb->prefix . "findus_city";

                $query_string = "DELETE FROM $users WHERE id='$record_id'";

                $results = $wpdb->query($query_string);

            }

            echo '<div id="message" class="updated fade"><p>Selected records deleted successfully!</p></div>';

        }

    }



    function get_sortable_columns() {

        return array();

    }



    function column_default($item, $column_name) {

        return stripslashes($item[$column_name]);

    }



    function column_term_id($item) {

        return $item->term_id;

    }



    function delete_record($record_id) {

        global $wpdb;

        $city_table_name = $wpdb->prefix . "findus_city";

        $delete_command = "DELETE FROM " . $city_table_name . " WHERE id = '$record_id'";

        $result = $wpdb->query($delete_command);

    }



    function column_id($item) {



        //Build row actions

        $actions = array(

            'View' => sprintf('<a href="admin.php?page=quotation_form_accident&tab=view_user&id=%s" >View</a>', $item['id']),

            'delete' => sprintf('<a href="admin.php?page=quotation_form_accident&action=delete_txn&id=%s" onclick="return confirm(\'Are you sure you want to delete this record?\')">Delete</a>', $item['id']),

        );



        //Return the refid column contents

        return $item['id'] . $this->row_actions($actions);

    }

    



    function column_cb($item) {

        return sprintf(

                '<input type="checkbox" name="%1$s[]" value="%2$s" />',

                /* $1%s */ $this->_args['singular'], //Let's reuse singular label (affiliate)

                /* $2%s */ $item['id'] //The value of the checkbox should be the record's key/id

        );

    }



   

    function prepare_items() {

        global $wpdb;



        $users_table_name = $wpdb->prefix ."motor_users_table";



        $users_meta_table_name = $wpdb->prefix . "motor_users_meta";



        $type_id = 3;



        $columns = $this->get_columns();



        $hidden = array();



        $per_page = 10;



        $sortable = $this->get_sortable_columns();



        $this->_column_headers = array($columns, $hidden, $sortable);



        $this->process_bulk_action();



        //This checks for sorting input. Read and sanitize the inputs

        $orderby_column = isset($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) : '';

        $sort_order = isset($_GET['order']) ? sanitize_text_field($_GET['order']) : '';

        if (empty($orderby_column)) {

            $orderby_column = "id";

            $sort_order = "DESC";

        }



        $query = "SELECT COUNT(*) FROM $users_table_name WHERE insurance_type = $type_id ";

        $total_items = $wpdb->get_var($query);



        // //pagination requirement

        $current_page = $this->get_pagenum();

        

        // $query = "SELECT * FROM $users_table_name ORDER BY $orderby_column $sort_order";



        $query = "SELECT * FROM $users_table_name  WHERE  insurance_type = $type_id ";



        $offset = ($current_page - 1) * $per_page;

        

        $query.=' LIMIT ' . (int) $offset . ',' . (int) $per_page;



        $data = $wpdb->get_results($query, ARRAY_A);

        //echo '<pre>';print_r($data);echo '</pre>';



        $this->items = $data;



        $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

            'total_pages' => ceil($total_items / $per_page)   //WE have to calculate the total number of pages

        ));



    }

    



    function no_items() {

        echo "no items found";

    }



}

