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

class Msg extends CI_Controller {

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

	public function index()
	{
            $data['ids']=$this->input->get('id', TRUE);
		    $sql_string = "SELECT CS_ID,CS_Name FROM ".CS_SqlPrefix."userzu order by CS_ID asc";
		    $data['userzu']=$this->db->query($sql_string)->result();
            $this->load->view('admin/msg',$data);
	}

	public function add()
	{

                $type=$this->input->post('type', TRUE);
                $ids=$this->input->post('ids', TRUE);
                $zu=$this->input->post('zu', TRUE);

				if($type=='id'){
                     if(empty($ids))  $this->CsdjSkins->Msg_url('接收会员ID不能为空!','javascript:history.back();');
					 //获取要发送的会员账号
		             $user=$this->db->query("SELECT CS_Name FROM ".CS_SqlPrefix."user where CS_ID in (".$ids.")")->result();
					 foreach ($user as $row) {
                            $id[]=$row->CS_Name;
					 }
					 $ids=implode(',', $id);
				}else{
                     if(empty($zu))  $this->CsdjSkins->Msg_url('请选择接收会员组!','javascript:history.back();');
					 $vip=implode(',', $zu);
					 //获取要发送的会员账号
		             $user=$this->db->query("SELECT CS_Name FROM ".CS_SqlPrefix."user where CS_Vip in (".$vip.")")->result();
					 foreach ($user as $row) {
                            $id[]=$row->CS_Name;
					 }
					 $ids=implode(',', $id);
				}

                $msg['cs_name']=$this->CsdjSkins->str_checkhtml($this->input->post('name', TRUE));   //标题
                $msg['cs_neir']=$this->CsdjSkins->uhtml($this->input->post('neir'));   //消息内容

                if(empty($msg['cs_name']))  $this->CsdjSkins->Msg_url('消息标题不能为空!','javascript:history.back();'); 
                if(empty($msg['cs_neir']))  $this->CsdjSkins->Msg_url('内容不能为空!','javascript:history.back();'); 

                $Arr=explode(',',$ids);
				for($i=0;$i<count($Arr);$i++){
                        $msg['cs_usera']=$Arr[$i];
                        $msg['CS_Userb']='管理员';
                        $msg['CS_AddTime']=date('Y-m-d H:i:s');
                        $this->CsdjDB->get_insert('xiaoxi',$msg);
				}
				$this->CsdjSkins->Msg_url('恭喜您，消息发送完成!',$_SERVER['HTTP_REFERER']);  
	}
}
