<?php
class Login extends  CI_Controller {
 
    public function __construct(){
        parent::__construct();
        session_start();
        $this->load->model('login_model');
        $this->url = $this->config->item("base_url");
    }
    public function index(){
        $_data ['url'] = $this->url;
        if (isset($_SESSION['name'])){
            header("Location:" . $this->url . "html");
        } else{
            $_name=$this->input->post('name');
            if(isset($_name)){
                $_password= sha1($this->input->post('password'));
            
                if($this->login_model->get_name($_name)){
                  if(!!$_user=$this->login_model->get_user($_name,$_password)){
//                       $this->session->set_userdata('name', $_name);
//                       $this->session->set_userdata('password', $_password);
//                       $this->session->set_userdata('roleid', $_user['user_role']);
                        $_SESSION['name']=$_name;
                        $_SESSION['password']=$_password;
                        $_SESSION['roleid']=$_user['user_role'];
                        echo '{"success":true,"message":"登录成功！"}';
                  }else{
                        echo '{"success":false,"message":"密码错误！"}';
                  }
                }else{
                        echo'{"success":false,"message":"用户名不存在！"}';
                }
            }else{
                $this->load->view('login',$_data);
            }
        }       
    }
    
    public function out(){
        session_destroy(); 
        header("Location:" . $this->url . "login");
    }
}