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

/**
 * 个人中心——消息管理
 */

class ucenter_message extends MY_Controller{
    /**
    * 当前用户uid
    *
    * @access private
    * @var int
    */
    private  $_uid = 0;

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

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

    /**
    * 偏移
    *
    * @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->config_cityflag = 'nj';
        //经纪人账号uid
        $this->_uid = $this->user_arr['uid']; 
        //echo $this->_uid;
        $this->load->model('message_model');
    }    
    
    /**
     * 消息管理
     */
    public function index(){
        $type = $this->input->get('type',TRUE);
        $keyword = $this->input->get('keyword',TRUE);
        $method = $this->input->get('method',TRUE);
        $post_param_page = $this->input->get('page',TRUE);
        $current_page = $post_param_page !='' ? intval($post_param_page) : 1;
         
       
        $arrModule = array('announce', 'system', 'shop', 'guest', 'house');
		$type = in_array($type, $arrModule) ? $type : $arrModule[0];
        $data = array();
        $where = "";
        $intNoRead ="";
        $tab = "";
        $url = '?type=' . $type;
        if($type == 'announce' || $type == 'system')
		{
            $tab = $type == 'system' ? "系统消息" : "通知公告";
            $tbl = 'agent_announceitem';
            $this->message_model->set_tbl($tbl);
            $strInfofrom = $type == 'system' ? 'agent_announce.issystem = 1 ' : 'agent_announce.issystem = 0 ';
			
            if($method == 'search'&& $keyword != '请输入消息标题')
			{
                $keyword = addslashes($keyword);
                $where = " and subject like '%$keyword%'";
            }
			elseif ($method=='del') 
			{
                $rowid = $this->input->get('rowid',TRUE);
                
				$del_where = "uid =  " . $this->_uid . "  AND agentcode = 'B000'";
                $this->message_model->delete_message($del_where,'announceid',$rowid);
                $this->jump($url, "删除成功！");//跳转
                return;
            }
            
            $join_arr = array('agent_announce'=>'agent_announceitem.announceid=agent_announce.id');
            $this->message_model->set_join_table($join_arr,'left');
            $cond_where ="agent_announceitem.uid = $this->_uid and agent_announceitem.agentcode = 'B000' and $strInfofrom $where ";   
            $cond_where_no = $cond_where." and agent_announceitem.new = 1";
            $intNoRead = $this->message_model->find_count_by($cond_where_no);
            
        }elseif ($type == 'shop') {
            $tab = "店铺留言";
            $tbl = "broker_guestbook";
            $this->message_model->set_tbl($tbl);
            if($method == 'search'&& $keyword != '请输入留言内容'){
                $keyword = addslashes($keyword);
                $where = " and content like '%$keyword%'";
            }elseif ($method=='del') {
                $rowid = $this->input->get('rowid',TRUE);
                $del_where = "uid = $this->_uid ";
                $this->message_model->delete_message($del_where,'id',$rowid);
                $this->jump($url, "删除成功！");//跳转
                return;
            }            
            $cond_where = "uid = $this->_uid".$where;
            $cond_where_no = $cond_where." and new = 1";
            $intNoRead = $this->message_model->find_count_by($cond_where_no);
        }

          
        //符合条件的总行数
        $this->_total_count = $this->message_model->find_count_by($cond_where);
        
        //计算总页数
        $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;
        
        //如果页数大于实际页数的页数为1
        if($current_page>$pages) $current_page=1;
        
        /** 分页参数 */
        $this->_init_pagination($current_page);   
        
        //获取数据源	
		if($type == 'announce' || $type == 'system'){
		     $data_info = $this->message_model->find_all_by( $cond_where , $this->_offset , $this->_limit);
		}else{
			 $data_info = $this->message_model->find_all_by( $cond_where , $this->_offset , $this->_limit);		
		}
       

        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit){
           $pagination = helper_pagination( $current_page , $pages , $this->_total_count );
        }
        if($type == 'house'){
            $data_info = $this->see_house_info($data_info);
              
        }
         /** 页面初始化 */
        $data['page_title'] = '个人中心-消息管理-'.$tab;
        $data['css'] = load_css('zsb/css/v1.0/style1.css'); //需要加载的css      
        $data['method'] = $method;
        $data['keyword'] = $keyword;
        $data['intNoRead'] = $intNoRead;
        $data['total_count'] = $this->_total_count;
        $data['data_info'] = $data_info;
        $data['type'] = $type;
        $data['pagination'] = $pagination;
        $data['page']  = $current_page;
        $data['google_analytics'] = get_ga_js_code('/个人中心/消息管理/'.$tab.'/消息列表页/'.$current_page.'/');
        $this->view('ucenter','ucenter/my_message_list',$data);
    }
    
    
    
    /**
     * 消息详情
     */
    public function detail(){
        $type = $this->input->get('type',TRUE);
        $action = $this->input->get('action',TRUE);
        $id = $this->input->get('id',TRUE);
        $data_info = array();
        $data = array();
        if($type == 'announce' || $type == 'system'){
            $tbl = 'agent_announceitem';
            $tab = $type == 'system' ? "系统消息" : "通知公告";
            $this->message_model->set_tbl($tbl);
            $fields_arr = array('agent_announce.subject', 'agent_announce.message', 'agent_announce.operator','agent_announce.creattime');
            $this->message_model->set_select_fields($fields_arr);
            $join_arr = array('agent_announce'=>'agent_announceitem.announceid=agent_announce.id');
            $this->message_model->set_join_table($join_arr,'left');
            $cond_where ="agent_announceitem.announceid = $id and  agent_announceitem.agentcode='B000' and agent_announceitem.uid='".$this->_uid."'  ";   
            $data_info = $this->message_model->find_one_by($cond_where);
            //修改查看状态
            $where_cond = array('announceid'=>$id,'agentcode'=>'B000','uid'=>$this->_uid);
            $this->message_model->update_message_status($where_cond,'new','0');
            
        }elseif ($type == 'shop') {
            $reply = $this->input->get('reply',TRUE);
            $tbl = 'broker_guestbook';
            $tab = "店铺留言";
            $this->message_model->set_tbl($tbl);
            if($action == 'reply'){
                //添加回复内容
                $reply_phone = $this->input->get('phone',TRUE);//需要回复的手机号
                $where_cond = array('id'=>$id,'uid'=>$this->_uid);
                $this->message_model->update_message_status($where_cond,'reply',$reply); 
               // //加短息通道回复留言者
               // if($this->is_phone($reply_phone)){
                    //send_sms($telno, $msg);
               // }
                $this->jump("?type=shop&id=$id", '回复成功！');
                return ;
                      
            }else{
                $cond_where = "id = $id AND uid = $this->_uid";
                $data_info = $this->message_model->find_one_by($cond_where);
                //修改查看内容
                $where_cond = array('id'=>$id,'uid'=>$this->_uid);
                $this->message_model->update_message_status($where_cond,'new','0'); 
            }
            
        }
        
        
         /** 页面初始化 */
        $data['page_title'] = '个人中心-消息管理-'.$tab;
        $data['css'] = load_css('zsb/css/v1.0/style1.css'); //需要加载的css
        
        $data['id'] = $id;
        $data['data_info'] = $data_info;
        $data['type'] = $type;
	    $data['google_analytics'] = get_ga_js_code('/个人中心/消息详情页/');
        $this->view('ucenter','ucenter/my_message_detail',$data);
    }

    /**
    * 初始化分页参数
    *
    * @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)
        {
            $this->jump('/ucenter_message/index/');
        }
    }
    
    /**
     * 列表页看房信息处理
     * see_house_info
     * @param array $data_info
     * @return array
     */
    private function see_house_info($data_info){      
        if(is_array($data_info)&&!empty($data_info)){
            foreach ($data_info as $key=>$value){
                $data_info[$key]['title'] = $this->arrange_house_info($value['tbl'],$value['rowid']);
                    
         }
     }
     return $data_info;
    }
    
    
    //整理看房房源信息
    private function arrange_house_info($tbl,$id){
          if($tbl=='sell'){
                    $arrRowInfo = $this->get_house_info($tbl,$id);
                    $blockname = $this->get_blockname($arrRowInfo['blockid']);  
                    $title = $blockname . '&nbsp;' . $arrRowInfo['room'] . '&nbsp;室&nbsp;' 
                    . $arrRowInfo['hall'] . '&nbsp;厅&nbsp;' . $arrRowInfo['buildarea'] 
                    . '㎡&nbsp;&nbsp;' . $arrRowInfo['price'] . '万&nbsp;&nbsp;房源ID：' . $arrRowInfo['id'];   
                }elseif($tbl=='rent'){
                    $arrRowInfo = $this->get_house_info($tbl,$id);                   
                    $blockname = $this->get_blockname($arrRowInfo['blockid']);  
                    $value['priceunit'] = $arrRowInfo['priceunit'] ? $arrRowInfo['priceunit'] : 1;
                    $house = $house = $this->config->item('house');
		    $unit = $house['priceunit'][$arrRowInfo['priceunit']];
		    $price = $arrRowInfo['price'] . $unit;
								
	            $title = $blockname . '&nbsp;' . $arrRowInfo['room'] . '&nbsp;室&nbsp;' 
                    . $arrRowInfo['hall'] . '&nbsp;厅&nbsp;' . $arrRowInfo['buildarea'] 
                    . '㎡&nbsp;&nbsp;' . $price . '&nbsp;&nbsp;房源ID：' . $arrRowInfo['id'];
                 
                    
                }
                return $title;
    }
    
    //根据房源id和房源类型获取房源信息
    private function get_house_info($tbl,$id){
        $key = $this->config_cityflag."_".$tbl."_sell_house_by_".$id;
        $type_model = $tbl == 'sell' ? 'sell_model' : 'rent_model'; 
        $data = $this->mc->get($key);
        if($data['ok']!=1){
             $this->load->model($type_model);
             if($tbl=='sell'){
                 $fields = array('id','blockid','room','hall','buildarea','price');
             }elseif($tbl=='rent'){
                 $fields = array('id','blockid','room','hall','buildarea','price','priceunit');
             }
             
             $this->$type_model->set_select_fields($fields);
             $this->$type_model->set_tbl($tbl);
             $arrRowInfo = $this->$type_model->find_by_id($id);
             $this->mc->set($key,array('ok'=>1,'data'=>$arrRowInfo),600);
        }else{
            $arrRowInfo = $data['data'];
        }
        return $arrRowInfo;
    }
    
    //根据小区id获取小区名
    private function get_blockname($id){
        $key = $this->config_cityflag."_sell_house_blockname_by_".$id;
        $data = $this->mc->get($key);
        if($data['ok']!=1){
            $this->load->model('block_model');
            $this->block_model->set_select_fields(array('blockname'));
            $blockinfo = $this->block_model->base_info_by_id($id);
            $blockname = $blockinfo['blockname'];
            $this->mc->set($key,array('ok'=>1,'data'=>$blockname),600);
            
        }else{
            $blockname = $data['data'];
        }
        return $blockname;
        
    }
    
    //判断是否为手机号
    private function is_phone($phone){
        if(preg_match("/1[34578]{1}\d{9}$/",$phone)){  
            $return = TRUE;
        }else{
            $return = FALSE;            
        }
        return $return;
    }
    
}
