<?php

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

class customerTrackingList extends WP_List_Table
{
    function __construct()
    {
    }

    function column_default($item, $column_name)
    {
        switch ($column_name) {
            default:
                return $item[$column_name];
        }
    }

    function get_columns()
    {
        $columns['status_name'] = '跟进类型';
        $columns['consultant_name'] = '置业顾问';
        $columns['time'] = '时间';
        $columns['description'] = '跟进说明';
        $columns['origin'] = '客/房源';
        return $columns;
    }

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

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

        $sql = "SELECT tct.*,ts.status_name,tc.name as consultant_name,tc.subsidiaryId FROM tospur_customer_tracking tct" .
            " left join (SELECT * FROM tospur_status where status_type = 3) as ts on tct.status_type = ts.status_id" .
            " left join tospur_consultant tc on tct.consultant_id = tc.id" .
            " left join tospur_organization torg on tc.subsidiaryId = torg.id" .
            " where 1 = 1";
        if (current_user_can('zygw')) {
            $sql = $sql . " and tc.id = " . get_current_user_id();
        }
        if (isset($_REQUEST['organization']) && $_REQUEST['organization'] != -1) {
            $sql = $sql . " and tc.subsidiaryId = " . $_REQUEST['organization'];
        }
        if (isset($_REQUEST['status_type']) && $_REQUEST['status_type'] != -1) {
            $sql = $sql . " and tct.status_type = " . $_REQUEST['status_type'];
        }
        if (isset($_REQUEST['search_consultant_name']) && $_REQUEST['search_consultant_name'] != null) {
            $sql = $sql . " and tc.name like '%" . $_REQUEST['search_consultant_name'] . "%'";
        }
        if (isset($_REQUEST['search_min_time']) && $_REQUEST['search_min_time'] != null) {
            $sql = $sql . " and tct.time >= '" . $_REQUEST['search_min_time'] . "'";
        }
        if (isset($_REQUEST['search_max_time']) && $_REQUEST['search_max_time'] != null) {
            $sql = $sql . " and tct.time <= '" . $_REQUEST['search_max_time'] . "'";
        }
        $sql = $sql . " order by tct.time desc";
        $result = DBManager::get_results($sql);
        $data = array();
        foreach ($result as $key => $value) {
            $data[$key] = array(
                'id' => $value->id,
                'status_id' => $value->status_id,
                'status_name' => $value->status_name,
                'consultant_name' => $value->consultant_name,
                'time' => $value->time,
                'description' => $value->description,
                'origin' => ($value->origin == 1) ? '房源' : (($value->origin == 2) ? '客源' : '')
            );
        }

        $current_page = $this->get_pagenum();

        $total_items = count($data);
        $total_pages = ceil($total_items / $per_page);

        if ($_REQUEST['paged'] > $total_pages) {
            $current_page = $total_pages;
        }

        $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_customerTrackingList()
{
    $context = array();
    $context['status'] = SearchDao::searchStatusType(3);
    $context['req'] = $_REQUEST;

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

function addCustomerTrackingTable()
{
    $_SERVER['REQUEST_URI'] = remove_query_arg('_wp_http_referer', $_SERVER['REQUEST_URI']);
    $customerTrackingList = new customerTrackingList();
    $customerTrackingList->prepare_items();
    $customerTrackingList->display();
}

?>
