<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Sell Class
 *
 * 租售宝求购、求租管理模块
 *
 * @package     zsb
 * @subpackage      Controllers
 * @category        Controllers
 * @author      liuhu
 */
class House_demands extends MY_Controller {
    
    /**
     * 城市参数
     *
     * @access private
     * @var string
     */
    protected  $_city = NULL;

    /**
     * 当前页码
     *
     * @access private
     * @var string
     */
    private $_current_page = 1;

    /**
     * 每页条目数
     *
     * @access private
     * @var int
     */
    private $_limit = 5;

    /**
     * 偏移
     *
     * @access private
     * @var int
     */
    private $_offset = 0;

    /**
     * 条目总数
     *
     * @access private
     * @var int
     */
    private $_total_count = 0;
    
    
    /**
     * 解析函数
     *
     * @access public
     * @return void
     */
    public function __construct()
    {   
        parent::__construct();
        
        //加载共用字符串处理函数
        $this->load->helper('common_string_helper');
    }
    
    
    /**
     * 初始化分页参数
     *
     * @access public
     * @param  int  $current_page
     * @param  int  $page_size
     * @return void
     */
    private function _init_pagination($current_page = 1 , $page_size = 0)
    {
        /** 当前页 */
        $this->_current_page = ( $current_page && is_numeric( $current_page ) ) ? intval( $current_page ) : 1;

        /** 每页多少项 */
        $this->_limit = ( $page_size && is_numeric( $page_size ) ) ? intval( $page_size ) : $this->_limit;

        /** 偏移量 */
        $this->_offset = ( $this->_current_page - 1 ) * $this->_limit;

        if($this->_offset < 0)
        {
            redirect(base_url());
        }
    }
    
    //默认控制器
    public function index()
    {   
        $this->buy();
    }
    
    
    /**
     * 求购管理列表页
     * 
     * @access pubic
     * @return void
     */
    public function buy()
    {
        $data_arr = array ();

        //获取表单数据
        $page = $this->input->post('page' , TRUE);

        /** 分页参数 */
        $this->_init_pagination($page);
        
        $this->load->model('buy_demand_model');
        
        //经纪人账号uid
        $broker_uid = $this->user_arr['uid'];
        
        //设置查询字段
        $field_arr = array('id','uid','infotype','address'
                        ,'buildyear1','buildyear2','buildarea1','buildarea2'
                        ,'price1','price2','contactor','remark','creattime'
                        ,'expiretime','room'
                        );
        $this->buy_demand_model->set_search_fields($field_arr);

        //查询符合房源版块条件的求购条数
        $this->_total_count = $this->buy_demand_model->get_demand_num_by_uid( $broker_uid );

	//计算总页数
        $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;

        //查询符合条件的求购信息列表
        $demands_info = $this->buy_demand_model->get_demand_list_by_uid( $broker_uid , $this->_offset , $this->_limit );
        
        //经纪人已匹配求购信息
        $matched_arr = array();
        
        //加载匹配房源类
        $this->load->model('buy_match_model');
        
        //设置匹配人的账号
        $this->buy_match_model->set_match_uid($broker_uid);
        
        //已匹配个数
        $matched_num = $this->buy_match_model->get_count_by_matchuid( 5 );
        if( $matched_num > 0)
        {   
            $this->buy_match_model->set_search_fields(array('want_id'));
            $matched_arr_all = $this->buy_match_model->get_list_by_matchuid( 5 , 0 , $matched_num);
            
            if(is_array($matched_arr_all) && !empty($matched_arr_all))
            {
                foreach ($matched_arr_all as $key => $value)
                {
                    $matched_arr[] = $value['want_id'];
                }
            }
        }

        //当前时间点
        $now_time = intval (date( 'His' , time())); 
        
        //加载房源核心数组
        $house = $this->config->item('house');
        
        $demands_num = count( $demands_info );
        for( $i = 0 ; $i < $demands_num ; $i++ )
        {
            //每个求购信息发布的时间点
            $publish_time = intval (date( 'His' , $demands_info[$i]['creattime'] ) );

            //每个求购信息的下期匹配时间点和本期匹配开始节点
            if( $now_time < $publish_time )
            {
                $next_time = date( 'Y-m-d' , time() ).' '.date('H:i:s' ,$demands_info[$i]['creattime']);
            }
            else
            {
                $next_time = date( 'Y-m-d' , time() + 86400 ).' '.date('H:i:s' ,$demands_info[$i]['creattime']);
            }

            // date formate july 30, 2014 10:20:30
            $demands_info[$i]['next_match_time'] =  date('F j, Y H:i:s',strtotime($next_time));

            //本轮开始时间节点
            $start_time = strtotime( $next_time ) - 86400;

            //本轮已匹配个数
            $infofrom = '';
            $today_matched_num = $this->buy_match_model->get_count_by_wantid( 5 , $start_time );
            $demands_info[$i]['totday_matched_num'] = intval($today_matched_num);
            
            //需要特殊处理的参数
            $demands_info[$i]['remark'] = csubstr( $demands_info[$i]['remark'] , 80 , 1 );
            $demands_info[$i]['infotype'] = $house['infotype'][$demands_info[$i]['infotype']] != '' 
                                    ? $house['infotype'][$demands_info[$i]['infotype']] : '';
        }
        
        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit)
        {
            $pagination = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }

        $data_arr['page_title'] = '求购匹配-求购需求';
        $data_arr['template_name'] = 'buy_demands_list';
        $data['page'] = $this->_current_page;
        $data_arr['demands_info'] = $demands_info; //求购信息
        $data_arr['matched_ids_arr'] = $matched_arr;//经纪人已匹配信息
        $data_arr['js'] = load_js('common/js/jquery.jcountdown.js,common/js/ajax_page.js');//需要加载的JS
        $data_arr['css'] = load_css('zsb/css/v1.0/style1.css');//需要加载的css
        $data_arr['perday_matche_limit'] = 20;
        $data_arr['pagination'] =   $pagination;
        
        //加载模板
        $this->view( 'house_manage' , 'buy_match_manage' , $data_arr );
    }
    
    
    /**
     * 求购已匹配管理列表页
     * 
     * @access pubic
     * @return void
     */
    public function buy_matched()
    {
        $data_arr = array ();

        //获取表单数据
        $page = $this->input->post('page' , TRUE);

        /** 分页参数 */
        $this->_init_pagination($page);
        
        //设置uid
        $broker_uid = $this->user_arr['uid'];
        
        //加载匹配MODEL
        $this->load->model('buy_match_model');
        
        //加载需求MODEL
        $this->load->model('buy_demand_model');
        
        //设置查询字段
        $field_arr = array('id','infotype','address','buildyear1'
                    ,'buildyear2','buildarea1','buildarea2'
                     ,'price1','price2','telno','remark'
                    );
        $this->buy_match_model->set_search_fields($field_arr);

        //查询经纪人已匹配的求购个数
        $this->buy_match_model->set_match_uid( $broker_uid );
        $this->_total_count = $this->buy_match_model->get_count_by_matchuid( 5 );

	//计算总页数
        $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;
        
        //经纪人已匹配求购信息
        $demand_tbl = $this->buy_demand_model->get_tbl();
        $matched_arr = array();
        $matched_arr = $this->buy_match_model->get_demandlist_by_matchuid( $demand_tbl , 5 , $this->_offset , $this->_limit );
        
        //加载房源核心数组
        $house = $this->config->item('house');
        $demands_num = count($matched_arr);
        for( $i = 0 ; $i < $demands_num ; $i++ )
        {
            //需要特殊处理的参数
            $matched_arr[$i]['remark'] = csubstr( $matched_arr[$i]['remark'] , 80 , 1 );
            $matched_arr[$i]['infotype'] = $house['infotype'][$matched_arr[$i]['infotype']] != '' 
                                    ? $house['infotype'][$matched_arr[$i]['infotype']] : '';
        }
        
        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit)
        {
            $pagination   = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }
        
        $data_arr['page_title'] = '求购匹配-已匹配需求';
        $data_arr['template_name'] = 'buy_demands_matched_list';
        $data['page'] = $this->_current_page;
        $data_arr['js'] = load_js('common/js/ajax_page.js,zsb/js/v1.0/js2.js');    //需要加载的JS
        $data_arr['css'] = load_css('zsb/css/v1.0/style1.css');//需要加载的css
        $data_arr['matched_info'] = $matched_arr;   //求购信息
        $data_arr['pagination'] =   $pagination;
        
        //加载模板
        $this->view( 'house_manage' , 'buy_match_manage' , $data_arr );
    }
    
    
    /**
     * 求租管理列表页
     * 
     * @access pubic
     * @return void
     */
    public function rent()
    {
        $data_arr = array ();

        //获取表单数据
        $page = $this->input->post('page' , TRUE);

        /** 分页参数 */
        $this->_init_pagination($page);
        
        $this->load->model('rent_demand_model');
        
        //经纪人账号uid
        $broker_uid = $this->user_arr['uid'];
        
        //设置查询字段
        $field_arr = array('id','uid','infotype','address','buildarea1'
            ,'buildarea2','price1','price2','contactor','remark','creattime'
            ,'expiretime','renttype');
        $this->rent_demand_model->set_search_fields($field_arr);

        //查询符合房源版块条件的求租条数
        $this->_total_count = $this->rent_demand_model->get_demand_num_by_uid( $broker_uid );

	//计算总页数
        $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;

        //查询符合条件的求租信息列表
        $demands_info = $this->rent_demand_model->get_demand_list_by_uid( $broker_uid , $this->_offset , $this->_limit );
        
        //经纪人已匹配求租信息
        $matched_arr = array();
        
        //加载匹配房源类
        $this->load->model('rent_match_model');
        
        //设置匹配人的账号
        $this->rent_match_model->set_match_uid($broker_uid);
        
        //已匹配个数
        $matched_num = $this->rent_match_model->get_count_by_matchuid( 5 );
        if( $matched_num > 0)
        {   
            $this->rent_match_model->set_search_fields(array('want_id'));
            $matched_arr_all = $this->rent_match_model->get_list_by_matchuid( 5 , 0 , $matched_num);
            
            if(is_array($matched_arr_all) && !empty($matched_arr_all))
            {
                foreach ($matched_arr_all as $key => $value)
                {
                    $matched_arr[] = $value['want_id'];
                }
            }
        }

        //当前时间点
        $now_time = intval (date( 'His' , time())); 
        
        //加载房源核心数组
        $house = $this->config->item('house');
        
        $demands_num = count( $demands_info );
        for( $i = 0 ; $i < $demands_num ; $i++ )
        {
            //每个求租信息发布的时间点
            $publish_time = intval (date( 'His' , $demands_info[$i]['creattime'] ) );

            //每个求租信息的下期匹配时间点和本期匹配开始节点
            if( $now_time < $publish_time )
            {
                $next_time = date( 'Y-m-d' , time() ).' '.date('H:i:s' ,$demands_info[$i]['creattime']);
            }
            else
            {
                $next_time = date( 'Y-m-d' , time() + 86400 ).' '.date('H:i:s' ,$demands_info[$i]['creattime']);
            }

            // date formate july 30, 2014 10:20:30
            $demands_info[$i]['next_match_time'] =  date('F j, Y H:i:s',strtotime($next_time));

            //本轮开始时间节点
            $start_time = strtotime( $next_time ) - 86400;

            //本轮已匹配个数
            $infofrom = '';
            $today_matched_num = $this->rent_match_model->get_count_by_wantid( 5 , $start_time );
            $demands_info[$i]['totday_matched_num'] = intval($today_matched_num);
            
            //需要特殊处理的参数
            $demands_info[$i]['remark'] = csubstr( $demands_info[$i]['remark'] , 80 , 1 );
            $demands_info[$i]['infotype'] = $house['infotype'][$demands_info[$i]['infotype']] != '' 
                                    ? $house['infotype'][$demands_info[$i]['infotype']] : '';
        }
        
        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit)
        {
            $pagination = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }

        $data_arr['page_title'] = '求租匹配-求租需求';
        $data_arr['template_name'] = 'rent_demands_list';
        $data['page'] = $this->_current_page;
        $data_arr['demands_info'] = $demands_info; //求租信息
        $data_arr['matched_ids_arr'] = $matched_arr;//经纪人已匹配信息
        $data_arr['js'] = load_js('common/js/jquery.jcountdown.js,common/js/ajax_page.js');//需要加载的JS
        $data_arr['css'] = load_css('zsb/css/v1.0/style1.css');//需要加载的css
        $data_arr['perday_matche_limit'] = 20;
        $data_arr['pagination'] =   $pagination;
        
        //加载模板
        $this->view( 'house_manage' , 'rent_match_manage' , $data_arr );
    }
    
    
    /**
     * 求租已匹配管理列表页
     * 
     * @access pubic
     * @return void
     */
    public function rent_matched()
    {
        $data_arr = array ();

        //获取表单数据
        $page = $this->input->post('page' , TRUE);

        /** 分页参数 */
        $this->_init_pagination($page);
        
        //设置uid
        $broker_uid = $this->user_arr['uid'];
        
        //加载匹配MODEL
        $this->load->model('rent_match_model');
        
        //加载需求MODEL
        $this->load->model('rent_demand_model');
        
        //设置查询字段
        $field_arr = array('id','infotype','address','renttype',
                    'buildarea1','buildarea2','price1','price2','telno','remark');
        $this->rent_match_model->set_search_fields($field_arr);

        //查询经纪人已匹配的求租个数
        $this->rent_match_model->set_match_uid( $broker_uid );
        $this->_total_count = $this->rent_match_model->get_count_by_matchuid( 5 );

	//计算总页数
        $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;
        
        //经纪人已匹配求租信息
        $demand_tbl = $this->rent_demand_model->get_tbl();
        $matched_arr = array();
        $matched_arr = $this->rent_match_model->get_demandlist_by_matchuid( $demand_tbl , 5 , $this->_offset , $this->_limit );
        
        //加载房源核心数组
        $house = $this->config->item('house');
        $demands_num = count($matched_arr);
        for( $i = 0 ; $i < $demands_num ; $i++ )
        {
            //需要特殊处理的参数
            $matched_arr[$i]['remark'] = csubstr( $matched_arr[$i]['remark'] , 80 , 1 );
            $matched_arr[$i]['infotype'] = $house['infotype'][$matched_arr[$i]['infotype']] != '' 
                                    ? $house['infotype'][$matched_arr[$i]['infotype']] : '';
        }
        
        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit)
        {
            $pagination = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }
        
        $data_arr['page_title'] = '求租匹配-已匹配需求';
        $data_arr['template_name'] = 'rent_demands_matched_list';
        $data['page'] = $this->_current_page;
        $data_arr['js'] = load_js('common/js/ajax_page.js,zsb/js/v1.0/js2.js');    //需要加载的JS
        $data_arr['css'] = load_css('zsb/css/v1.0/style1.css');//需要加载的css
        $data_arr['matched_info'] = $matched_arr;   //求租信息
        $data_arr['pagination'] =   $pagination;
        
        //加载模板
        $this->view( 'house_manage' , 'rent_match_manage' , $data_arr );
    }
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */