<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin 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->db->cache_off();
	        $this->CsdjAdmin->Admin_Login();
	}


	public function index()
	{
            $this->CsdjAdmin->Admin_Qx('40');
 	    $page = intval($this->input->get('page'));
            if($page==0) $page=1;


	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."admin order by CS_ID desc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/admin/index/');
	    $per_page = 20; 
            $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['admin'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

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

	public function add()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_AdminName']='';
            $data['CS_AdminPass']='';
            $data['CS_Quanx']='';
            $this->load->view('admin/admin_edit',$data);
        }

	public function edit()
	{
            $this->CsdjAdmin->Admin_Qx('40');
	    $id = intval($this->uri->segment(4));
            if($id==0) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/admin/index/'));
            $data['title']="修改";
            $sql="SELECT * FROM ".CS_SqlPrefix."admin where cs_id=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
	    if($row){
                 $data['CS_ID']=$row[0]->CS_ID;
                 $data['CS_AdminName']=$row[0]->CS_AdminName;
                 $data['CS_AdminPass']=$row[0]->CS_AdminPass;
                 $data['CS_Quanx']=$row[0]->CS_Quanx;
            }
            $this->load->view('admin/admin_edit',$data);
        }


	public function save()
	{
            $this->CsdjAdmin->Admin_Qx('40');
            $data['CS_AdminName']=$this->input->post('CS_AdminName',true);
            $data['CS_AdminPass']=$this->input->post('CS_AdminPass',true);
            $data['CS_Quanx']=$this->input->post('CS_Quanx',true);
            $ids=intval($this->input->post('CS_ID'));
            if(!empty($data['CS_Quanx'])){
                $data['CS_Quanx']=implode(',', $data['CS_Quanx']);
            }
            if(empty($data['CS_AdminName']) || empty($data['CS_AdminPass'])) $this->CsdjAdmin->Admin_Url('<font color=red>名称、密码、不能为空！</font>','javascript:history.back();');

            if($ids==0){
                 $data['CS_AdminPass']=md5($data['CS_AdminPass']);
                 $this->CsdjDB->get_insert('admin',$data);
            }else{
		 $row=$this->CsdjDB->get_select ('admin','CS_ID','*',''.$ids.'');
                 if($row[0]->CS_AdminPass!=$data['CS_AdminPass']){
                        $data['CS_AdminPass']=md5($data['CS_AdminPass']);
                 }
                 $this->CsdjDB->get_update('admin',$ids,$data);
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/admin/index/'),'ok');
        }

	public function del()
	{
	    $id = intval($this->uri->segment(4));
            if($id==0) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/admin/index/'));

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

