package cn.com.qmth.stmms.admin.home;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.utils.RequestUtils;

@Controller
@RequestMapping("/admin/home")
public class HomeController {
    @Value("${app.index}")  
    private String appIndex;

    @RequestMapping
    public ModelAndView index(HttpServletRequest request) {
        WebUser user = RequestUtils.getWebUser(request);
        if (user.getRoles().contains(Role.SYS_ADMIN)) {
        	if("aopeng".equals(appIndex)){
        		return new ModelAndView("modules/sys/sysIndexAp");
        	}
            ModelAndView modelAndView = new ModelAndView("modules/sys/sysIndex");
            return modelAndView;
        } else {
        	if("aopeng".equals(appIndex)){
        		return new ModelAndView("modules/exam/examIndexAp");
        	}
            ModelAndView modelAndView = new ModelAndView("modules/exam/examIndex");
            return modelAndView;
        }
    }

    /**
     * 获取主题方案
     */
    @RequestMapping(value = "/theme/{theme}")
    public String getThemeInCookie(@PathVariable String theme, HttpServletRequest request,
            HttpServletResponse response) {
        if (StringUtils.isNotBlank(theme)) {
            RequestUtils.setCookie(response, "theme", theme, 60 * 60 * 24);
        } else {
            theme = RequestUtils.getCookie(request, "theme");
        }
        return "redirect:" + request.getParameter("url");
    }

}
