<?php

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

class customerList extends WP_List_Table
{
    function __construct()
    {
        global $status, $page;
        //Set parent defaults
        parent::__construct(array(
            'singular' => 'customerList',     //singular name of the listed records
            'plural' => 'customerLists',    //plural name of the listed records
            'ajax' => false        //does this table support ajax?
        ));
    }

    function column_default($item, $column_name)
    {
        switch ($column_name) {
            case'id':
            case 'name':
            case 'address':
            case 'average_price':
            case 'developer':
            case 'check_in_time':
            case 'room_id':
            case 'property_age':
            case 'decoration':
            case 'covered_area':
            case 'status':
            case 'volume_rate':
            case 'greening_rate':
            case 'households':
            case 'parking_spaces':
            case 'property_management':
            case 'property_money':
            default:
                return '<a href="' . admin_url() . 'admin.php?page=newHouse&edit=true&id=' . $item['id'] . '">' . $item[$column_name] . '</a>';
        }
    }

    function column_cb($item)
    {
        return sprintf(
            '<input type="checkbox" name="%1$s[]" value="%2$s" />',
            /*$1%s*/
            $this->_args['singular'],  //Let's simply repurpose the table's singular label ("score")
            /*$2%s*/
            $item['id']                //The value of the checkbox should be the record's id
        );
    }

    function get_columns()
    {
        if (current_user_can('administrator')) {
            $columns = array(
                'cb' => '<input type="checkbox" />'
            );
        }
        $columns['name'] = '姓名';
        $columns['demand_type'] = '需求类型';
        $columns['customer_remark'] = '客户备注';
        $columns['consultant'] = '置业顾问';
        $columns['check_in_time'] = '状态';
        return $columns;
    }

    function get_sortable_columns()
    {
        $sortable_columns = array();
        return $sortable_columns;
    }

    function prepare_items()
    {
        global $wpdb;
        $per_page = 10;
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();

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

        $sql = "select * from tospur_customer tc";
        $result = $wpdb->get_results($sql);
        $data = array();
        foreach ($result as $key => $value) {
        }

        $current_page = $this->get_pagenum();

        $total_items = count($data);

        $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);

        $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 function_customerList()
{
    $context = array();
    Timber::render("customerList.html", $context);
}

function addCustomerTable()
{
    $customerList = new customerList();
    $customerList->prepare_items();
    $customerList->display();
}

?>
