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

class Message extends CI_Controller {

	function __construct(){
		        parent::__construct();
        	    $this->load->helper('url');
        	    $this->load->helper('form');
                $this->load->library('session'); //加载session
		        $this->load->model('CsdjDB');
		        $this->load->model('CsdjSkins');
		        $this->load->model('CsdjUser');
	}

	public function index()
	{
		        $this->CsdjUser->User_Login();
                $data='';
                $op = $this->input->get('op', TRUE);   //op，默认为空为全部，xt为系统消息，wd为未读消息
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;


                $this->load->get_user_templates();
		        $template=$this->load->view('message-list.html',$data,true);
		        $template=$this->CsdjSkins->TopandBottom($template,'user');

	        	$Mark_Text=str_replace("{cscms:title}","消息 - 会员管理中心",$template);
		        $Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		        $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		        $Mark_Text=str_replace("[msg:op]",$op,$Mark_Text);
		        $Mark_Text=str_replace("[user:op]","messagem",$Mark_Text);

		        //预先除了分页
		        $pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
		        preg_match_all('/{cscms:msg(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:msg}/',$Mark_Text,$page_arr);//判断是否有分页标识
		        if(!empty($page_arr) && !empty($page_arr[2])){

                        $sqlstr="select * from ".CS_SqlPrefix."xiaoxi where CS_Usera='".$this->session->userdata('cs_name')."'";
                        if($op=='wd'){
                             $sqlstr.=" and cs_did=0";
                        }

                        $sqlstr.=" order by CS_AddTime desc";
                        $data_content='';
			            $Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/message?op='.$op,$page);//sql,每页显示条数
			            $result=$this->db->query($Arr[2]);
			            if($result){
					            $sorti=1;
					            foreach ($result->result() as $row2) {
					            	$datatmp=$this->CsdjSkins->DataMsg($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
						            $sorti=$sorti+1;
						            $data_content.=$datatmp;
					            }
			            }// end if recount
			            $Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
			            $Mark_Text=str_replace($page_arr[0][0],$data_content,$Mark_Text);
			            unset($Arr);
                }
		        unset($page_arr);
	            $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		        $Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		        $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
	}

    //消息内容
	public function show()
	{
		        $this->CsdjUser->User_Login();
                $data='';$data_content='';
                $id = intval($this->uri->segment(5));   //ID
                
                if($id==0){
                    exit($this->CsdjSkins->Msg_url('出错了，ID不能为空！','javascript:history.back();'));
                }

		        $rowm=$this->CsdjDB->get_select ('xiaoxi','CS_ID','*',''.$id.'');
		        if(!$rowm){
                	exit($this->CsdjSkins->Msg_url('抱歉，该消息记录不存在！','javascript:history.back();'));
		        }
                if($rowm[0]->CS_DID==0){ //变更为已读
                     $this->db->query("update ".CS_SqlPrefix."xiaoxi set cs_did=1 where cs_id='".$id."'");
                }
				$data['userb']=$rowm[0]->CS_Userb;
                //获取对话
                $sqlstr="select * from ".CS_SqlPrefix."xiaoxi where CS_FID='".$id."' order by CS_AddTime asc";
			    $result=$this->db->query($sqlstr);
				$data['msg']=$result->result();

                $this->load->get_user_templates();
		        $template=$this->load->view('message-show.html',$data,true);
		        $template=$this->CsdjSkins->TopandBottom($template,'user');

		        $Mark_Text=str_replace("{cscms:title}","阅读消息 - 会员管理中心",$template);
		        $Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		        $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		        $Mark_Text=str_replace("[user:op]","messagem",$Mark_Text);
		        $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
	            $rowu=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		        $Mark_Text=$this->CsdjUser->getuser($Mark_Text,$rowu);
		        $Mark_Text=$this->CsdjSkins->DataMsg($Mark_Text,$Mark_Text,$rowm[0],$this->session->userdata('cs_name')); //全部解析
		        $this->CsdjSkins->skins($Mark_Text,$data);
	}

	public function add_save()
	{
                if(!$this->CsdjUser->User_Login(1)){
		            exit("20001");//未登入
                }
                $fid=intval($this->input->post('fid', TRUE));
				if($fid==0) exit('10013');
                $row=$this->db->query("select CS_Usera,CS_Userb from ".CS_SqlPrefix."xiaoxi where cs_id='".$fid."'")->row();
		        if(!$row || $row->CS_Userb==$this->session->userdata('cs_name')){   
		            exit("10013");
				}

				//用户级别
				$level=$this->CsdjSkins->getziduan('user','cs_jinyan',$this->session->userdata('cs_id'));
				if(getlevel($level)<5){
			         exit("20002"); //级别太小
				}

                $msg['cs_usera']=$row->CS_Userb;   //接收会员
                $msg['cs_neir']=$this->CsdjSkins->str_checkhtml($this->input->post('neir', TRUE));   //消息内容

                if(empty($msg['cs_neir'])){
			         exit("10007");//内容为空
                }	
		        if(strlen($msg['cs_neir'])>1500){
			         exit("10006");//内容过长
		        }
                $msg['cs_fid']= $fid;
                $msg['CS_Userb']=$this->session->userdata('cs_name');
                $msg['CS_AddTime']=date('Y-m-d H:i:s');
                $this->CsdjDB->get_insert('xiaoxi',$msg);

				//状态改变为未读
				$this->db->query("update ".CS_SqlPrefix."xiaoxi set cs_did=0 where cs_id=".$fid."");

				//发送通知
				$msg2['cs_type']='message';
				$msg2['cs_name']='回复了&nbsp;<strong>您</strong>&nbsp;的私信';
				$msg2['cs_userb']=$this->session->userdata('cs_name');
				$msg2['cs_usera']=$row->CS_Userb;
				$msg2['cs_addtime']=date('Y-m-d H:i:s');
		        $this->CsdjDB->get_insert ('msg',$msg2);

			    exit("10000");
	}

	public function del()
	{
                if(!$this->CsdjUser->User_Login(1)){
		            exit("20001");//未登入
                }

                $op=$this->input->post('op', TRUE);
                $id=intval($this->input->post('id', TRUE));   //id 

                if($op=='all'){ //删除全部
                     $this->db->query("delete from ".CS_SqlPrefix."xiaoxi where cs_usera='".$this->session->userdata('cs_name')."'");
					 exit('10000');
                }elseif($op=='wd'){ //全部标示为已读
                     $this->db->query("update ".CS_SqlPrefix."xiaoxi set cs_did=1 where cs_usera='".$this->session->userdata('cs_name')."' and cs_did=0");
					 exit('10000');
                }elseif($op=='id'){  //按ID
					 if($id==0) exit('20002');
		             $row=$this->db->query("select CS_Usera,CS_Userb from ".CS_SqlPrefix."xiaoxi where cs_id='".$id."'")->row();
		             if($row){
						   if($row->CS_Usera!=$this->session->userdata('cs_name') && $row->CS_Userb!=$this->session->userdata('cs_name')){
                                       exit('20002');
						   }
                           $this->db->query("delete from ".CS_SqlPrefix."xiaoxi where cs_fid='$id'");
                           $this->db->query("delete from ".CS_SqlPrefix."xiaoxi where cs_id='$id'");
						   exit('10000');
					 }else{
                           exit('20002');
					 }
				}
        }
}

