package com.bohuikeji.frame.module.admin.controller;

import com.bohuikeji.frame.module.admin.base.PageTemplateControllerBase;
import com.bohuikeji.frame.module.admin.entity.back.PageEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysConfig;
import com.bohuikeji.frame.module.admin.entity.sys.SysMenu;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.entity.web.template.TableTemplate;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.bohuikeji.frame.module.admin.service.ISysMenuService;
import com.bohuikeji.frame.module.admin.service.ISysPageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Random;

/**
 * 统一页面映射
 * 所有页面访问请求都在此类处理,禁止处理其他请求
 * /views对应resources下的templates文件夹
 * @author 10552
 */
@Controller
@RequestMapping("/views")
@RequiredArgsConstructor
public class FramePageController extends PageTemplateControllerBase {
    final ISysConfigService configService;
    /*ISysPageService pageService;
    ISysMenuService menuService;*/
    /**
     * 统一页面转发
     * @param request
     * @return
     */
    @GetMapping("/**")
    public PageEntity toPage(HttpServletRequest request){
        Enumeration<String> paramaters=request.getParameterNames();
        //session.setAttribute("pageTemplate",createPage(TableTemplate.class));
        while(paramaters.hasMoreElements()){
            String parameterName = paramaters.nextElement();
            request.setAttribute(parameterName,request.getParameter(parameterName));
        }
        /**SysUser user=getLoginUser();
        SysMenu menu=(SysMenu)session.getAttribute("activeMenu");
        if(menu!=null){
            if(!menuService.AuthCheck(user.getRoleId(),request.getRequestURI())){
                return false;
            }
        }**/


        return PageEntity.page(request.getServletPath().replaceFirst("/views",""));
    }
    /**
     * 后台登陆页面
     * @param request
     * @return
     */
    @GetMapping("/login")
    public PageEntity toLogin(HttpServletRequest request){
        session.setAttribute("config_project_name", configService.getOneByKey("project_name"));
        session.setAttribute("config_comp_name",configService.getOneByKey("comp_name"));
        session.setAttribute("config_project_logo",configService.getOneByKey("project_logo"));
        List<SysConfig> configList=configService.getListByGroup("login_bg_image");
        return PageEntity.page(request.getServletPath().replaceFirst("/views","")).add("bgimg",configList.get((new Random()).nextInt(configList.size())).getConfigValue());
    }

    /**
     * 后台授权与登陆页面
     * @param request
     * @return
     */
    @GetMapping("/authorized")
    public PageEntity toLoginAndAuthorized(HttpServletRequest request){
        session.setAttribute("config_project_name", configService.getOneByKey("project_name"));
        session.setAttribute("config_comp_name",configService.getOneByKey("comp_name"));
        session.setAttribute("config_project_logo",configService.getOneByKey("project_logo"));
        List<SysConfig> configList=configService.getListByGroup("login_bg_image");
        return PageEntity.page(request.getServletPath().replaceFirst("/views","")).add("bgimg",configList.get((new Random()).nextInt(configList.size())).getConfigValue());
    }

    /**
     * 默认模板页面
     * @param request
     * @return
     */
    @GetMapping("/table/index")
    public PageEntity toTabletIndex(HttpServletRequest request){
        session.setAttribute("pageTemplate",createPage(TableTemplate.class));
        return PageEntity.page("/table/index");
    }
    @GetMapping("/test/index")
    public PageEntity test(){
        session.setAttribute("pageTemplate",createPage(TableTemplate.class));
        return PageEntity.page("/table/index");
    }
}
