<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
	by it308.com
*/
class Admin extends Admin_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('mysession');
		$this->admin_name = $this->config->item('admin_name');
	}
	public function index(){
		$this->check_login();
		$data = array(
			'web_name'=>$this->admin_name."-管理首页",
		);
		$this->load->library('serverinfo');
		$serverinfo=$this->serverinfo->getserverinfo();
		$data['server']=$serverinfo;
		$this->load->view('header',$data);
		$this->load->view('index');
	}
	
	
	/**
	 * 都是AJAX请求返回网站
	 */
	public function serverinfo(){
		$this->ajax_login();
		$this->load->library('serverinfo');
		$serverinfo=$this->serverinfo->getserverinfo();
		$data['server']=$serverinfo;
		$this->load->view('index',$data);
	}
	public function setting(){
		$this->ajax_login();
		$this->load->model('setting_m');
		$setting = $this->setting_m->get();
		$data['setting']=$setting;
		$this->load->view('setting',$data);
	}
	/**********************************上面的都是AJAX请求的HTML*************************************************/
	
	//ajax检查登录
	public function ajax_login(){
		$suser = $this->mysession->userdata('emailsystemuser');
		if($suser==""){
			$res = array(
				'status' => -1,
				'msg' => '请登陆后在操作！',
			);
			exit(json_encode((object)$res));
			return false;
		}else{
			return true;
		}
	}
	public function check_login(){
		$suser = $this->mysession->userdata('emailsystemuser');
		if($suser==""){
			redirectmy('admin/login');
		}else{
			return true;
		}
	}
	public function login(){
		$data = array(
			'web_name'=>$this->admin_name."-用户登录",
		);
		$this->load->view('login',$data);
	}
	public function login_pro(){
		$user = $this->input->post('user');
		$pass = $this->input->post('pass');
		$code = $this->input->post('code');
		$scode = $this->mysession->userdata('emailsystemcode');
		if($code != $scode){
			$data = array(
				'web_name'=>$this->admin_name."-用户登录",
				'status'=>-1,
				'msg'=>'验证码错误!',
			);
			$this->load->view('login',$data);
			return false;
		}
		$this->load->model('admin_m');
		$res = $this->admin_m->check_admin($user,$pass);
		if($res){
			//登录成功
			$user=array(
				'user'=>$res[0]->user,
				'uid'=>$res[0]->id,
			);
			$this->mysession->set_userdata('emailsystemuser',$user);
			redirectmy('/admin/index');
		}else{
			$data = array(
				'web_name'=>$this->admin_name."-用户登录",
				'status'=>-1,
				'msg'=>'用户名或密码错误!',
			);
			$this->load->view('login',$data);
			return false;
		}
	}
	public function code(){
		$this->load->library('code');
		$this->code->session_var = 'emailsystemcode';
		$this->code->scale = 1;
		$this->code->CreateImage();
	}
}

