<?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()
    {
    }

    function column_default($item, $column_name)
    {
        switch ($column_name) {
            case 'name':
                return '<a href="' . admin_url('admin.php?page=customer&edit=true&id=' . $item['id']) . '">' . $item[$column_name] . '</a>';
            default:
                return $item[$column_name];
        }
    }

    function get_columns()
    {
        $columns['name'] = '姓名';
        $columns['demand_type'] = '需求类型';
        $columns['customer_remark'] = '客户备注';
        $columns['consultant_name'] = '置业顾问';
        $columns['status_name'] = '状态';
        return $columns;
    }

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

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

        $sql = "SELECT tcs.*,tc.name as consultant_name,ts.status_name FROM tospur_customer tcs" .
            " left join (SELECT * FROM tospur_status where status_type = 5) as ts on tcs.status = ts.status_id" .
            " left join tospur_consultant tc on tcs.consultant_id = tc.id where 1 = 1";
        if (isset($_POST['status']) && $_POST['status'] != -1) {
            $sql = $sql . " and tcs.status = " . $_POST['status'];
        }
        if (isset($_POST['customer_type']) && $_POST['customer_type'] != -1) {
            $sql = $sql . " and tcs.customer_type = " . $_POST['customer_type'];
        }
        if (isset($_POST['demand_type']) && $_POST['demand_type'] != -1) {
            $demand_type = null;
            switch ($_POST['demand_type']) {
                case 0:
                    $demand_type = 'tcs.new_house';
                    break;
                case 1:
                    $demand_type = 'tcs.secondHand_house';
                    break;
                case 2:
                    $demand_type = 'tcs.rent_house';
                    break;
            }
            if ($demand_type) {
                $sql = $sql . " and " . $demand_type . " = 1";
            }
        }
        if (isset($_POST['search_name'])) {
            $sql = $sql . " and tcs.name like '%" . $_POST['search_name'] . "%'";
        }
        if (isset($_POST['search_consultant_name'])) {
            $sql = $sql . " and tc.name like '%" . $_POST['search_consultant_name'] . "%'";
        }
        if (isset($_POST['search_phone'])) {
            $sql = $sql . " and tcs.phone like '%" . $_POST['search_phone'] . "%'";
        }
        if (isset($_POST['search_min_time'])) {
            $sql = $sql . " and tcs.time >= '" . $_POST['search_min_time'] . "'";
        }
        if (isset($_POST['search_max_time'])) {
            $sql = $sql . " and tcs.time <= '" . $_POST['search_max_time'] . "'";
        }
        $result = $wpdb->get_results($sql);
        $data = array();
        foreach ($result as $key => $value) {
            $demand_type_array = array(
                ($value->new_house == 1) ? '新房' : null,
                ($value->secondHand_house == 1) ? '二手房' : null,
                ($value->rent_house == 1) ? '租房' : null
            );
            $demand_type_array = array_filter($demand_type_array, function ($var) {
                return !is_null($var);
            });
            $data[$key] = array(
                'id' => $value->id,
                'name' => $value->name,
                'demand_type' => implode("、", $demand_type_array),
                'customer_remark' => $value->customer_remark,
                'consultant_name' => $value->consultant_name,
                'status_name' => $value->status_name
            );
        }

        $current_page = $this->get_pagenum();
        if (isset($_POST['paged'])) {
            $current_page = $_POST['paged'];
        }

        $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();
    $context['status'] = SearchDao::searchStatusType(5);
    $context['customer_type'] = SearchDao::searchStatusType(6);

    $context['status_id'] = $_POST['status'];
    $context['demand_type'] = isset($_POST['demand_type']) ? $_POST['demand_type'] : -1;
    $context['customer_type_id'] = isset($_POST['customer_type']) ? $_POST['customer_type'] : -1;
    $context['search_name'] = $_POST['search_name'];
    $context['search_consultant_name'] = $_POST['search_consultant_name'];
    $context['search_phone'] = $_POST['search_phone'];
    $context['search_min_time'] = $_POST['search_min_time'];
    $context['search_max_time'] = $_POST['search_max_time'];

    Timber::render("customerList.html", $context);
}

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

?>
