package cn.feast.coding.controller.system;


import cn.feast.coding.model.generator.Page;
import cn.feast.coding.model.system.Permission;
import cn.feast.coding.model.system.Role;
import cn.feast.coding.model.system.RolePermission;
import cn.feast.coding.service.system.PermissionService;
import cn.feast.coding.service.system.RolePermissionService;
import cn.feast.coding.service.system.RoleService;
import cn.feast.coding.tools.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private RolePermissionService rolePermissionService;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Role role, Page page, ModelMap model){
		page.setMaxRows(5);
		List<Role> list = roleService.queryByRole(role);
		page.setStart(PageUtils.getPage(page.getPageNumber(), page.getTotalPage(), list.size(), page.getMaxRows()));
		page.setTotalPage(PageUtils.getTotalPage(page.getPageNumber(), page.getTotalPage(), list.size(), page.getMaxRows()));
		List<Role> roleList = roleService.queryByList(page, role);
		model.put("page", page);
		model.put("roleList", roleList);
		model.put("role", role);
		return "role/role_list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpServletRequest request, ModelMap model){
		List<Permission> menuList = permissionService.queryByMenuPermission();
		List<Permission> permissionList = permissionService.queryBySubPermission();
		model.put("menuList", menuList);
		model.put("permissionList", permissionList);
		return "role/role_add";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Role role, @RequestParam(value = "permission", required = false) Integer[] permission, ModelMap model, HttpServletRequest request){
		roleService.save(role);
		RolePermission rolePermission = new RolePermission();
		if(null != permission && permission.length > 0){
			for(Integer i : permission){
				rolePermission.setRoleid(role.getId());
				rolePermission.setPermissionid(i);
				rolePermissionService.save(rolePermission);
			}
		}
		model.put("flag", 1);
		return "role/role_add";
	}
	
	
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(Role role,ModelMap model){
		role = roleService.queryById(role.getId());
		List<RolePermission> rolePermissionList = rolePermissionService.queryByRole(role.getId());
		List<Permission> menuList = permissionService.queryByMenuPermission();
		List<Permission> permissionList = permissionService.queryBySubPermission();
		for(RolePermission rolePermission : rolePermissionList){
			Permission p = permissionService.queryById(rolePermission.getPermissionid());
			for(Permission p1 : permissionList){
				if(p.getId().equals(p1.getId())){
					p1.setChecked(1);
				}
			}
			for(Permission p2 : menuList){
				if(p.getId().equals(p2.getId())){
					p2.setChecked(1);
				}
			}
		}
		model.put("menuList", menuList);
		model.put("permissionList", permissionList);
		model.put("role", role);
		return "role/role_edit";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Role role,@RequestParam(value = "permission", required = false) Integer[] permission,ModelMap model,HttpServletRequest request){
		roleService.update(role);
		rolePermissionService.deleteByRole(role.getId());
		RolePermission rolePermission = new RolePermission();
		if(null != permission && permission.length > 0){
			for(Integer i : permission){
				rolePermission.setRoleid(role.getId());
				rolePermission.setPermissionid(i);
				rolePermissionService.save(rolePermission);
			}
		}
		model.put("flag", 1);
		return "role/role_edit";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public void delete(Role role){
		roleService.deleteById(role.getId());
		rolePermissionService.deleteByRole(role.getId());
	}
}
