<?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']= '业主姓名';
        $columns['oBroker']= '卖方经纪人';
        $columns['oCommission']= '佣金';
        $columns['cName']= '客户姓名';
        $columns['cBroker']= '买方经纪人';
        $columns['cCommission']= '佣金';
        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),
            'oBroker' => array('oBroker', false),
            'oCommission' => array('oCommission', false),
            'cName' => array('cName', false),
            'cBroker' => array('cBroker', false),
            'cCommission' => array('cCommission', 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,th.address,t.price,(t.oCommission+t.cCommission) as commission,
            tc.name as manager,ts.status_name as status,oName,oBroker,oCommission,cName,cBroker,cCommission
            from tospur_contract t
            left join tospur_consultant tc on tc.id = t.consultantId
            left join tospur_house th on th.id = t.houseId
            left join tospur_status ts on ts.status_type = 4 and t.status = ts.status_id
            where 1=1 ";
       //根据房源编号搜索
        $search_type = $_REQUEST['search_type'];
        if(isset($_REQUEST[$search_type]) && $search_type == 'search_contractId'){
           $sql .= " and t.contractId = '".$_REQUEST["search_contractId"]."'";
        }

        if(isset($_REQUEST["beginDate"]) &&isset($_REQUEST["endDate"]) &&$search_type == 'search_signedDate') {
            $sql .=  " and t.signedDate between '".$_REQUEST["beginDate"]."' and '".$_REQUEST["endDate"]."'";
        }

        if(isset($_REQUEST[$search_type]) && $search_type == 'search_OCname'){
            $sql .= " and oName like '%".$_REQUEST["search_OCname"]."%' or cName like '%".$_REQUEST["search_OCname"]."%'";
        }

        if(isset($_REQUEST[$search_type]) && $search_type == 'search_OCbroker'){
            $sql .= " and oBroker like '%".$_REQUEST["search_OCbroker"]."%' or cBroker like '%".$_REQUEST["search_OCbroker"]."%'";
        }
        //置业顾问显示自己的合同
        if(current_user_can("zygw")){
            $sql .= " and t.consultantId = ".get_current_user_id();
        }else if(current_user_can("jl")){//经理显示提交给自己的合同，和自己的合同
            $sql .= " and (t.consultantId = ".get_current_user_id()." or t.managerId = ".get_current_user_id().")";
        }
        $sql .= " order by t.signedDate desc";
        $result = DBManager::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,
                'oBroker' => $value->oBroker,
                'oCommission' => $value->oCommission,
                'cName' => $value->cName,
                'cBroker' => $value->cBroker,
                'cCommission' => $value->cCommission
            );
        }

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