package cn.com.liandisys.ycjt.ggcx.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.com.liandisys.ycjt.ggcx.admin.model.User;
import cn.com.liandisys.ycjt.ggcx.common.security.shiro.session.SessionDAO;
import cn.com.liandisys.ycjt.ggcx.common.util.UserUtils;

/**
 * 后台管理登录控制器
 * @author jiangchao
 * @version 2016-11-28
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminLoginController {
	
	protected Logger logger = LoggerFactory.getLogger(AdminLoginController.class);

	@Autowired
	private SessionDAO sessionDAO;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(HttpServletRequest request, HttpServletResponse response, Model model) {
		User principal = UserUtils.getPrincipal();
		if (logger.isDebugEnabled()){
			logger.debug("login, active session size: {}", sessionDAO.getActiveSessions(false).size());
		}
		if(principal != null){
			return "redirect:/admin/main";
		}
		return "admin/login";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(String loginName,String password,Model model) {
    	if (SecurityUtils.getSubject().isAuthenticated()) {
			return "redirect:/admin/main";
		}
    	model.addAttribute("message","用户名或密码不正确");
		return "admin/login";
    }
	
	@RequestMapping(value = "/exit", method = RequestMethod.GET)
	public String exit(Model model) {
	    SecurityUtils.getSubject().logout();
	    return "admin/login";
	}
}
