package com.yihu.hos.system.controller;

import com.yihu.hos.system.service.AuthorityManager;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by chenweida on 2016/1/20.
 */
@Controller
@RequestMapping("/authority")
public class AuthorityController {
    @Resource(name = AuthorityManager.BEAN_ID)
    private AuthorityManager authorityManager;

    /**
     * 权限管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("initial")
    public String authorityInitial(Model model) {
        model.addAttribute("contentPage", "system/authority/authority");
        return "partView";
    }

    /**
     * 获取菜单列表
     */
    @RequestMapping("getMenuList")
    @ResponseBody
    public Result getMenuList(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            if (StringUtils.isEmpty(id)) {
                return new DataGridResult();
            }
            return authorityManager.getMenuList(id);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    @RequestMapping("changeRoleMenu")
    @ResponseBody
    public Result changeRoleMenu(HttpServletRequest request) {
        try {
            String roleId = request.getParameter("roleId");
            String menuId = request.getParameter("id");
            String flag = request.getParameter("flag");

            return authorityManager.changeRoleMenu(roleId, menuId, flag);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    @RequestMapping("changeRoleAction")
    @ResponseBody
    public Result changeRoleAction(HttpServletRequest request) {
        try {
            String roleId = request.getParameter("roleId");
            String actionId = request.getParameter("id");
            String flag = request.getParameter("flag");
            return authorityManager.changeRoleAction(roleId, actionId, flag);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    @RequestMapping("allowAll")
    @ResponseBody
    public Result allowAll(HttpServletRequest request) {
        try {
            String roleId = request.getParameter("roleId");
            return authorityManager.allowAll(roleId);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
    @RequestMapping("forbidAll")
    @ResponseBody
    public Result forbidAll(HttpServletRequest request) {
        try {
            String roleId = request.getParameter("roleId");
            return authorityManager.forbidAll(roleId);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
}
