<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login 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('CsdjAdmin');
	}

	public function index()
	{
           if(Admin_Url && $_SERVER['HTTP_HOST']!=Admin_Url){
                 $this->CsdjAdmin->Admin_Url('您的登入地址错误,系统禁止该地址登入！',Web_Path);
           }
           $this->load->view('admin/login');
        }

	public function log()
	{
		header('Content-Type: text/html; charset=gb2312');
		$name = $this->security->xss_clean($this->input->post('name', TRUE));
		$pass = $this->security->xss_clean($this->input->post('pass', TRUE));
		$code = $this->security->xss_clean($this->input->post('codes', TRUE));

		if (empty($name)||empty($pass)||empty($code)){
			$this->CsdjAdmin->Admin_Url('请填写用户名、密码、认证码！',site_url('admin/login/'));
		}

		if($code!=Admin_Code){
			$this->CsdjAdmin->Admin_Url('抱歉，认证码不正确！',site_url('admin/login/'));
		}

		$log=$this->CsdjDB->get_select ('admin','CS_AdminName','*',''.$name.'');
		if($log){
                     if($log[0]->CS_AdminPass!=md5($pass)){
			  $this->CsdjAdmin->Admin_Url('抱歉，密码不正确！',site_url('admin/login/'));
                     }else{

	                    $updata['CS_LoginNum']  = $log[0]->CS_LoginNum+1;
	                    $updata['CS_LoginIP']   = $_SERVER["REMOTE_ADDR"];
	                    $updata['CS_LoginTime'] = date('Y-m-d H:i:s');
                            $this->CsdjDB->get_update ('admin',$log[0]->CS_ID,$updata);

		            $admindata = array(
                                        'admin_name'  => $log[0]->CS_AdminName,
                                        'admin_id'    => $log[0]->CS_ID,
                                        'admin_pass'  => md5($log[0]->CS_AdminPass),
                                        'admin_qx'  => $log[0]->CS_Quanx,
                                   );
                            $this->session->set_userdata($admindata); //写入缓存
			    exit("<script>window.location='".site_url('admin/')."';</script>");
                     }
                }else{
			$this->CsdjAdmin->Admin_Url('抱歉，用户不存在！',site_url('admin/login/'));
                }
        }

	public function logout()
	{
		$admindata = array(
                                'admin_name'  => '',
                                'admin_id'     => '',
                                'admin_pass'     => '',
                                'admin_qx'     => '',
                            );
                $this->session->unset_userdata($admindata);
		exit("<script>window.location='".site_url('admin/')."';</script>");
        }
}

