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

/**
 * buy_rent Class
 *
 * 用户中心求购、求租页面
 *
 * @package         esf_system
 * @subpackage      Libraries
 * @category        Libraries
 * @author          liuhu
 */
class want_zhou extends MY_Controller {

    /**
    * 用户选择的城市
    *
    * @access private
    * @var string
    */
    protected  $_city = 'nj';

    /**
    * 当前用户ID
    *
    * @access private
    * @var int
    */
    private  $_uid = 0;

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

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

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

    /**
    * 条目总数
    *
    * @access private
    * @var int
    */
    private $_total_count = 0;


    /**
    * 全站通用房源信息数组
    *
    * @access private
    * @var array
    */
    private $_house = array();


    /**
    * 解析函数
    *
    * @access public
    * @return void
    */
    public function __construct()
    {
        GLOBAL $house;

        parent::__construct();

        $this->load->helper('url');
        $this->_house = $house;

        //用户登录检测
        $this->user_id = checklogin();
        if(isset($_GET['wkj'])){
            echo  2;die;
        }
        if( $this->user_id <= 0)
        {
            jumptotk(USER_CENTER.'login',"请登录后再进行操作！",3000,'tipswaring');
            exit;
        }
        $this->_city = $this->session->userdata('esfusercity');
        $this->_uid = $this->session->userdata('esfuserid');
        //城市参数、或者用户编号参数为空，则跳转到首页
        if( empty($this->_city) || $this->_uid <= 0 )
        {
             redirect(site_url());
        }
    }


    /**
    * 初始化分页参数
    *
    * @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(uri_string());
        }
    }


    //默认页面
    public function index()
    {
        $this->buylist();
    }


    /**
    * 求购列表页
    *
    * @access public
    * @param  无
    * @return void
    */
    public function buylist()
    {
        //获取表单数据
        $page = $this->input->post('page',TRUE);

        /** 分页参数 */
        $this->_init_pagination($page);

        //加载房源操作统计类
        $this->load->model('user_model');

        //符合条件的总行数
        $this->_total_count = $this->user_model->get_buynum_by_uid($this->_uid,1);

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

        //获取数据源
        $statistic_data = $this->user_model->get_buy_by_uid($this->_uid,$this->_offset,$this->_limit);

        //表单类
        $this->load->helper('form');

        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit)
        {
            $this->load->helper('pagination');
            $pagination   = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }

        /** 页面初始化 */
        $data['title'] = '求购求租-我的求购房源';
        $data['conf_where'] = 'buylist';
        $data['want_type'] = 'buy';
        $data['page'] = $this->_current_page;
        $data['form_attributes'] = array('name' => 'search_form', 'id' => 'search_form');
        $data['js'] = "";
        $data['buy_data'] = $statistic_data;  //统计数据
        $data['pagination'] =   $pagination;
        $data['house'] = $this->_house;
		//BI统计 ----- start----->

		 $data['isa_website'] = '2,19,75';
		 $data['isa_city'] = GB_CITY;
		 $data['isa_addones'] = '';
		 $data['isa_url'] = 'isa.house365.com/js/analyze.js?v=2.1';
		//BI统计 ----- ent----->
        //加载模板
        $this->load->view('my_buy_zhou',$data);
    }




    /**
    * 求租列表页
    *
    * @access public
    * @param  无
    * @return void
    */
    public function rentlist()
    {
        //获取表单数据
        $page = $this->input->post('page',TRUE);

        /** 分页参数 */
        $this->_init_pagination($page);

        //加载房源操作统计类
        $this->load->model('user_model');

        //符合条件的总行数
        $this->_total_count = $this->user_model->get_rent_wantnum_by_uid($this->_uid,1);

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

        //获取数据源
        $statistic_data = $this->user_model->get_rentwant_by_uid($this->_uid,$this->_offset,$this->_limit);

        //表单类
        $this->load->helper('form');

        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit)
        {
            $this->load->helper('pagination');
            $pagination   = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }

        /** 页面初始化 */
        $data['title'] = '求购求租-我的求租房源';
        $data['conf_where'] = 'rentlist';
        $data['want_type'] = 'rentwant';
        $data['page'] = $this->_current_page;
        $data['form_attributes'] = array('name' => 'search_form', 'id' => 'search_form');
        $data['js'] = "";
        $data['rent_data'] = $statistic_data;  //统计数据
        $data['pagination'] =   $pagination;
        $data['house'] = $this->_house;

        //加载模板

		//BI统计 ----- start----->

		 $data['isa_website'] = '2,19,76';
		 $data['isa_city'] = GB_CITY;
		 $data['isa_addones'] = '';
		 $data['isa_url'] = 'isa.house365.com/js/analyze.js?v=2.1';
		//BI统计 ----- ent----->
        $this->load->view('my_rentwant',$data);
    }


    /**
    * 已匹配房源信息
    *
    * @access public
    * @param  string $want_type 需求类别 求购/求租
    * @param  int $want_id 需求编号
    * @return void
    */
    function want_matched_window( $want_type , $want_id )
    {
        global $house;
        //获取表单数据
        $want_id = intval($want_id);
        $want_type = strip_tags($want_type);
        $page = $this->input->post('page',TRUE);

        $data['priceunit'] = $house['priceunit'];
        if( $want_id > 0 && in_array( $want_type , array('buy','rentwant') ))
        {
            //加载房源操作统计类
            $this->load->model('want_house_model');

            $want_arr = array ('buy'=> array('house_tbl'=>'sell','match_tbl'=>'buy_match'),'rentwant'=> array('house_tbl'=>'rent','match_tbl'=>'rentwanted_match'));

            //设置房源表
            $this->want_house_model->set_tbl_house($want_arr[$want_type]['house_tbl']);

            //设置匹配表
            $this->want_house_model->set_tbl_match($want_arr[$want_type]['match_tbl']);
            if($want_type=='buy')
            {
            $fields_base = array('id','district','streetid','blockshowname','infotype','infofrom','buildarea','price','room','hall','fitment','floor','subfloor','totalfloor');
            }else{
            $fields_base = array('id','district','streetid','blockshowname','infotype','infofrom','buildarea','price','room','hall','fitment','floor','subfloor','totalfloor','priceunit');
            }
            $fields_t['buy'] = array('buildyear');
            $fields_t['rentwant'] = array('renttype');
            $fields = array_merge( $fields_base , $fields_t[$want_type]);
            $this->want_house_model->set_house_fields($fields);

            //符合条件的总行数
            $this->_total_count = $this->want_house_model->get_matched_num_by_id( $want_id );

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

            if($page>$pages)
            {
             $page=$pages;
            }
            /** 分页参数 */
            $this->_init_pagination( $page , 6 );
            $pagination = NULL;

            //获取数据源
            $statistic_data = $this->want_house_model->get_matched_by_id( $want_id , $this->_offset , $this->_limit );
        }
        else
        {
            $this->_total_count = 0;
            $pages  = 0;
            $statistic_data = array();
        }

        //表单类
        $this->load->helper('form');

        //是否显示分页
        if( $this->_total_count > $this->_limit)
        {
            $this->load->helper('pagination');
            $pagination   = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }

        /** 页面初始化 */
        $data['title'] = '求购求租-匹配房源';
        $data['conf_where'] = 'buy';
        $data['show_type'] = 'buy';
        $data['page'] = $this->_current_page;
        $data['form_attributes'] = array('name' => 'search_form', 'id' => 'search_form');
        $data['css'] = "<link href='".USER_CENTER."css/public.css' rel='stylesheet' type='text/css'>";
        $data['js'] = "<script type='text/javascript' src='".USER_CENTER."js/jquery-1.8.0.min.js'></script><script type='text/javascript' src='".USER_CENTER."js/pepcenter.js'></script>";
        $data['matched_data'] = $statistic_data;  //统计数据
        $data['pagination'] = $pagination;
        $data['house'] = $this->_house;
        $data['want_type'] = $want_type;

        //加载模板
        $this->load->view('want_matched_window',$data);
    }


    /**
    * 修改匹配信息状态
    * @author cxf
    */
    function match_isread()
    {
        //加载房源操作统计类
        $this->load->model('want_house_model');
        $match_type = $this->input->post('match_type');
        $match_id = $this->input->post('match_id');

		$ids = $this->input->post('ids');

		if(!empty($ids))
		{
		$id_array=explode(',',$ids);
		foreach($id_array as $value)
		{

		$arr = array('id'=>$value);
        $up = array('isread'=>1);
        $tbl = $match_type=='buy'?'buy_match':'rentwanted_match';
        $this->want_house_model->set_table($tbl);
        $result = $this->want_house_model->save($arr,$up,'esfhouse');
		}
		}else{

        $arr = array('id'=>$match_id);
        $up = array('isread'=>1);
        $tbl = $match_type=='buy'?'buy_match':'rentwanted_match';

        $this->want_house_model->set_table($tbl);
        $result = $this->want_house_model->save($arr,$up,'esfhouse');

		}

        //更新匹配未读的cookie
        $match_num = $this->user_model->get_match_num_byuid($this->user_id);
        setcoandsess('esfmatchnum',$match_num);
        echo $result;
    }


    /**
    * 修改求租求购的信息状态
    * @author cxf
    */
    function updatewant()
    {
       $id = $this->input->post('id');
       $type = $this->input->post('type');
       $code = $this->input->post('code');
       $tbl = $this->input->post('tbl');
       if(md5($id.'house365')==$code)
        {
         if($type =='del'){
              //更新房源状态,相当于失效状态
              $result = $this->user_model->updatewant($id,$tbl,array('esta'=>4));
              //记录再发布操作
              $this->house_statistic_model->set_uid($this->user_id);
              $this->house_statistic_model->set_city(GB_CITY);
              $result1 = $this->house_statistic_model->sub_operate_log($tbl , $id , 3 );
              if($result>0)
                {
                 //删除成功
                 echo '1';exit;
                }else{
                //删除失败
                 echo '3';exit;
                }
            }
        }else{
                  //验证不通过
                 echo '2';exit;
        }
    }


	/**
    * 修改求租求购的信息状态
    * @author cxf
    */
    function updatematch()
    {
	   //加载房源操作统计类
       $this->load->model('want_house_model');
       $match_type = $this->input->post('match_type');
       $tbl = $match_type=='buy'?'buy_match':'rentwanted_match';
	   $ask_tbl=$match_type=='buy'?'buy':'rentwanted';
	   $ids = $this->input->post('ids');
       $id = $this->input->post('id');
		if(!empty($ids))
		{
		$id_array=explode(',',$ids);

		foreach($id_array as $value)
		{

		$arr = array('id'=>$value);
        $up = array('esta'=>'4','isread'=>'1');
        $this->want_house_model->set_table($tbl);
        $result = $this->want_house_model->save($arr,$up,'esfhouse');
		//更新匹配数目
        $result = $this->user_model->updatematched_num($id,$ask_tbl);

		}
		}
		echo $result;

    }
}

/* End of file want.php */
/* Location: ./center/controllers/want.php */
