package org.mynonglam.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.mynonglam.dao.AdminDAO;
import org.mynonglam.entitys.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AdminController {
	@Autowired
	private AdminDAO adminDAO;
	@RequestMapping(value = "/ad-login", produces = "text/plain;charset=UTF-8")
	public String listSinhVien(Model uiModel, HttpServletRequest request) {
		String user = request.getParameter("userName");
		String pass = request.getParameter("passWord");
		Admin ad = new Admin(user, pass);
		boolean checkLogin = adminDAO.chekLogin(ad);
		if(checkLogin == false){
			request.setAttribute("tb", "UserName or Password is wrong!");
			return "index";
		}
		else{
			HttpSession session = request.getSession();
			session.setAttribute("userName", ad.getAdminname());
			return "home";
		}
		
	}
	@RequestMapping(value = "/logout", produces = "text/plain;charset=UTF-8")
	public String logout(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.removeAttribute("userName");
		return "index";
	}
	@RequestMapping(value = "/changPass", produces = "text/plain;charset=UTF-8")
	public String changePass(HttpServletRequest request){
		String oldPass = request.getParameter("oldPass");
		String newPass = request.getParameter("newPass");
		HttpSession session = request.getSession();
		String user = (String)session.getAttribute("userName");
		boolean checkPass = adminDAO.checkPass(oldPass, user);
		if(checkPass == true){
			adminDAO.updatePass(newPass, user);
			request.setAttribute("tb", "Successfully change password !");
			return "home";
		}
		else{
			request.setAttribute("tb", "Old password is wrong!");
			return "home.jsp?rep=changepassword";
		}
	}
	@RequestMapping(value = "/updateisblock", produces = "text/plain;charset=UTF-8")
	public String updateIsbock(HttpServletRequest request){
		String id = request.getParameter("id");
		String status = request.getParameter("status");
		adminDAO.updateIsblock(id, status);
		return "home.jsp?rep=accmanager";
	}
	@RequestMapping(value = "/blockGroup", produces = "text/plain;charset=UTF-8")
	public String blockGroup(HttpServletRequest request){
		String id = request.getParameter("id");
		String status = request.getParameter("status");
		adminDAO.blockGroup(id, status);
		return "home.jsp?rep=groupmanager";
	}

}
