<?php

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

    function column_default($item, $column_name)
    {
        $name = $item[$column_name];
        switch ($column_name) {
            case'business':
                switch($item[$column_name]){
                    case 0:
                        $name = "新房";
                        break;
                    case 1:
                        $name = "二手房";
                        break;
                    case 2:
                        $name = "租房";
                        break;
                }
                break;
        }
        return '<a href="' . admin_url() . 'admin.php?page=contract&edit=true&id='. $item['id'].'">' .$name . '</a>';
    }

    function get_columns()
    {
        $columns['contractId']= '合同编号';
        $columns['signedDate']= '签约日期';
        $columns['business']= '业务类型';
        $columns['address']='房源地址';
        $columns['price']= '成交金额';
        $columns['commission']= '佣金总额';
        $columns['manager']= '签约人员';
        $columns['status']= '状态';
        $columns['oName']= '业主姓名';
        return $columns;
    }

    function get_sortable_columns()
    {
        $sortable_columns = array(
            'contractId' => array('contractId', false),
            'signedDate' => array('signedDate', false),
            'business' => array('business', false),
            'address' => array('address', false),
            'price' => array('price', false),
            'commission' => array('commission', false),
            'status' => array('status', false),
            'oName' => array('oName', false)
        );
        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);

        $this->process_bulk_action();

        //$data = $this->example_data;

        $sql = "select t.id,t.contractId,t.signedDate,t.business,t.address,t.price,(t.oCommission+t.cCommission) as commission,
            tc.name as manager,ts.status_name as status,oName
            from tospur_contract t
            left join tospur_consultant tc on tc.id = t.managerId
            left join tospur_status ts on ts.status_type = 4 and t.status = ts.status_id;";
        $result = $wpdb->get_results($sql);
        $data = array();
        foreach ($result as $key => $value) {
            $data[$key] = array(
                'id' => $value->id,
                'contractId' => $value->contractId,
                'signedDate' => $value->signedDate,
                'business' => $value->business,
                'address' => $value->address,
                'price' => $value->price,
                'commission' => $value->commission,
                'manager' => $value->manager,
                'status' => $value->status,
                'oName' => $value->oName
            );
        }

        $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
        ));
    }


    public static function showTableView()
    {
        Timber::render("contractList.html");
    }

    public static function displayTable(){
        $contractList = new Contract_List();
        $contractList->prepare_items();
        $contractList->display();
    }
}