<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Gbook 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();
                $this->load->helper('file');
	}

	public function index()
	{
 	    $sid = $this->input->get('sid');
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;

            if($sid==1){
	        $sql_string = "SELECT * FROM ".CS_SqlPrefix."gbook where cs_usera='cscms' order by CS_AddTime desc";
            }elseif($sid==2){
	        $sql_string = "SELECT * FROM ".CS_SqlPrefix."gbook where cs_usera<>'cscms' order by CS_AddTime desc";
            }else{
	        $sql_string = "SELECT * FROM ".CS_SqlPrefix."gbook order by CS_AddTime desc";
            }
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/gbook/index/')."?sid=".$sid;
	    $per_page = 15; 
            $totalPages = ceil($total / $per_page); // 总页数
	    $data['nums'] = $total;
            if($total<$per_page){
               $per_page=$total;
            }
            $sql_string.=' limit '. $per_page*($page-1) .','. $per_page;
	    $query = $this->db->query($sql_string);

	    $data['gbook'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

            $this->load->view('admin/gbook',$data);
        }

	public function hf()
	{
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/gbook/index/'));
            $sql="SELECT * FROM ".CS_SqlPrefix."gbook where cs_id=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
	    if($row){
                $data['CS_ID']=$row[0]->CS_ID;
                $data['CS_Neir']=$row[0]->CS_Neir;
                $data['CS_Hfneir']=$row[0]->CS_Hfneir;
            }
            $this->load->view('admin/gbook_hf',$data);
        }

	public function save()
	{
            $this->CsdjAdmin->Admin_Qx('26');
            $CS_ID=$this->input->post('CS_ID');

                $data['CS_Hfneir']=$this->input->post('CS_Neir');

                if(empty($data['CS_Hfneir'])){
                        $this->CsdjAdmin->Admin_Url('<font color=red>内容不能为空！</font>','javascript:history.back();');
                }

                $this->CsdjDB->get_update('gbook',$CS_ID,$data);
                $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/gbook/index'),'ok');
        }

	public function del()
	{
            $this->CsdjAdmin->Admin_Qx('26');
            $id = $this->security->xss_clean($this->uri->segment(4));   //ID
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');

            $this->db->query("delete from ".CS_SqlPrefix."gbook where CS_ID in (".$id.")");
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }

	public function pldel()
	{
            $this->CsdjAdmin->Admin_Qx('26');
	    $ids=$this->input->post('CS_ID');
            if(empty($ids)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');
            $id=implode(',', $ids);
            $this->db->query("delete from ".CS_SqlPrefix."gbook where CS_ID in (".$id.")");
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }
}

