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


class rentHouseList extends WP_List_Table
{
    function __construct()
    {
        global $status, $page;
        //Set parent defaults
        parent::__construct(array(
            'singular' => 'rentHouseList',     //singular name of the listed records
            'plural' => 'rentHouseList',    //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 'community_name':
            case 'rent':
            case 'buildproperty_id':
            case 'covered_area':
            case 'floor':
            case 'faceto':
            case 'decoration':
            case 'age':
            case 'matching_facilities':
            case 'user_id':
            case 'status':
            default:
                return '<a href="' . admin_url() . 'admin.php?page=rentHouse&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('moderate_comments') ) {
            $columns = array(
                'cb' => '<input type="checkbox" />'
            );
        }
        $columns['id']= 'ID';
        $columns['name']= '租房标题';
        $columns['community_name']= '小区名称';
        $columns['rent']= '元/月';
        $columns['buildproperty_id']= '户型';
        $columns['covered_area']= '面积';
        $columns['floor']= '楼层';
        $columns['faceto']= '朝向';
        $columns['decoration']= '装修状况';
        $columns['age']= '年代';
        $columns['matching_facilities']= '配套设施';
        $columns['user_id']= '置业顾问';
        $columns['status']= '状态';
        return $columns;
    }

    function get_sortable_columns()
    {
        $sortable_columns = array(
            'id' => array('id', false),
            'name' => array('name', false),
            'community_name' => array('community_name', false),
            'rent' => array('rent', false),
            'buildproperty_id' => array('buildproperty_id', false),
            'covered_area' => array('covered_area', false),
            'floor' => array('floor', false),
            'faceto' => array('faceto', false),
            'decoration' => array('decoration', false),
            'age' => array('age', false),
            'matching_facilities' => array('matching_facilities', false),
            'user_id' => array('user_id', false),
            'status' => array('status', false)
        );
        return $sortable_columns;
    }

    function get_bulk_actions()
    {
        $actions = array(
            'noCheck' => '未审核',
            'check' => '审核',
            'selfSale'=>'自售',
            'otherSale'=>'他售',
            'invalid'=>'无效',
            'reactivation'=>'重激活'
        );
        return $actions;
    }

    function process_bulk_action()
    {
        $action = $this->current_action();
        if ($action) {
            $string = null;
            $status = null;
            switch ($action) {
                case 'noCheck':
                    $id = $_POST['renthouselist'];
                    if ($id) {
                        $string = '(' . implode(',', array_map('intval', $id)) . ')';
                        $status = 0;
                    }
                    break;
                case 'check':
                    $id = $_POST['renthouselist'];
                    if ($id) {
                        $string = '(' . implode(',', array_map('intval', $id)) . ')';
                        $status = 1;
                    }
                    break;
                case 'selfSale':
                    $id = $_POST['renthouselist'];
                    if ($id) {
                        $string = '(' . implode(',', array_map('intval', $id)) . ')';
                        $status = 2;
                    }
                    break;
                case 'otherSale':
                    $id = $_POST['renthouselist'];
                    if ($id) {
                        $string = '(' . implode(',', array_map('intval', $id)) . ')';
                        $status = 3;
                    }
                    break;
                case 'invalid':
                    $id = $_POST['renthouselist'];
                    if ($id) {
                        $string = '(' . implode(',', array_map('intval', $id)) . ')';
                        $status = 4;
                    }
                    break;
                case 'reactivation':
                    $id = $_POST['renthouselist'];
                    if ($id) {
                        $string = '(' . implode(',', array_map('intval', $id)) . ')';
                        $status = 5;
                    }
                    break;

            }
            global $wpdb;
            $result = $wpdb->query('update tospur_house SET status='.$status .' where id in ' . $string);
        }
    }



    function prepare_items()
    {
        global $wpdb;
        $params = array();
        $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 * from tospur_house th
                 left join(select user_id as aus_id,house_id,user_type from a_house_user)  ahu on th.id = ahu.house_id and ahu.user_type=1
                 left join(select id as consul_id,name as consul_name from tospur_consultant) tc on ahu.aus_id = tc.consul_id
                 left join(select value,literal from dic_buildproperty) db on th.buildproperty_id = db.value
                 where 1=1 and house_type=2 ";
        if($_POST["listCity"]!=0  ){
            $params[] = $_POST["listCity"];
            $sql = $sql." and city_id=%d";
        }
        if($_POST["listDistrict"] != 0 ){
            $params[] = $_POST["listDistrict"];
            $sql = $sql." and district_id=%d";
        }
        if($_POST["listPlate"] != 0){
            $params[] = $_POST["listPlate"];
            $sql = $sql." and plate_id=%d";
        }
        if($_POST["buildProperty"]!=0){
            $params[] = $_POST["buildProperty"];
            $sql = $sql." and buildproperty_id=%d";
        }
        if($_POST["room"]!=0){
            $params[] = $_POST["room"];
            $sql = $sql." and room_id=%d";
        }
        if(isset($_POST["status"]) && $_POST["status"]!=-1){
            $params[] = $_POST["status"];
            $sql = $sql." and status=%d";
        }
        if($_POST["rentalPrice"]!=NULL){
            $priceArray = explode("-", $_POST['rentalPrice']);
            $params[] = $priceArray[0];
            $params[] = $priceArray[1];
            $sql = $sql . " and rent between %d and %d";
        }
        if($_POST["acreage"]!= NULL){
            $areaArray = explode("-", $_POST['acreage']);
            $params[] = $areaArray[0];
            $params[] = $areaArray[1];
            $sql = $sql . " and covered_area between %d and %d";
        }
        if($_POST["searchText"]!=NULL){
            $params[] = '%'.$_POST['searchText'].'%';
            $sql = $sql . " and name like %s";
        }
        if(isset($_GET["orderby"])){
            $orderby = $_GET["orderby"];
            $order = $_GET["order"];
            $sql = $sql ." order by ".$orderby." ".$order;
        }
        $result = $wpdb->get_results($wpdb->prepare($sql, $params));
        $data = array();
        foreach ($result as $key => $value) {
            $data[$key] = array(
                'id' => $value->id,
                'name' =>  $value->name,
                'community_name' =>  $value->community_name,
                'rent' =>  $value->rent,
                'buildproperty_id' =>  $value->literal,
                'covered_area' => $value->covered_area,
                'floor' =>  $value->floor,
                'faceto' =>  $value->faceto,
                'decoration' =>  $value->decoration,
                'age' => $value->age,
                'matching_facilities'=> $value->matching_facilities,
                'user_id' =>  $value->consul_name,
            );

            if($value->status == 0){
                $data[$key]['status'] ="未审核";
            }else if($value->status == 1){
                $data[$key]['status'] ="审核";
            }else if($value->status == 2){
                $data[$key]['status'] ="自售";
            }else if($value->status == 3){
                $data[$key]['status'] ="他售";
            }else if($value->status == 4){
                $data[$key]['status'] ="无效";
            }else if($value->status == 5){
                $data[$key]['status'] ="重激活";
            }
        }


        $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
        ));
    }
}
add_action( 'wp_ajax_updateList', 'newHouseList::prepare_items');
add_action( 'wp_ajax_nopriv_updateList', 'newHouseList::prepare_items');
add_action( 'wp_ajax_searchListCity', 'SearchDao::ajax_searchListCity');
add_action( 'wp_ajax_nopriv_searchListCity', 'SearchDao::ajax_searchListCity');

function function_rentHouseList()
{
    $contest = array();
    $contest['page'] = $_REQUEST['page'];
    $contest['city'] = SearchDao::searchCity();
    $contest['status'] = SearchDao::searchStatusType(2);
    $contest['buildProperty'] = SearchDao::searchBuildProperty();
    if(isset($_POST['hasSearch'])){
        $contest['district'] = SearchDao::searchCity($_POST['listCity']);
        $contest['plate'] = SearchDao::searchCity($_POST['listCity'],$_POST['listDistrict']);
        $contest['dicRentalPrice'] = searchDao::searchRentalPrice($_POST['listCity']);
        $contest['dicArea'] = searchDao::searchArea($_POST['listCity']);
        $contest['cityId'] = $_POST['listCity'];
        $contest['districtId'] = $_POST['listDistrict'];
        $contest['plateId' ]= $_POST['listPlate'];
        $contest['buildPropertyId']= $_POST['buildProperty'];
        $contest['rentalPrice'] = $_POST['rentalPrice'];
        $contest['acreage'] = $_POST['acreage'];
        $contest['statusId']  =  $_POST['status'];
    }
    $contest["house_type"] = 2;
    Timber::render("houseList.html",$contest);
}

function addRentTable(){
    $rentHouseList = new rentHouseList();
    $rentHouseList->prepare_items();
    $rentHouseList->display();
}
?>




