package com.wsk.travelf.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsk.travelf.bean.PermissionWithStatus;
import com.wsk.travelf.pojo.Role;
import com.wsk.travelf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/backstage/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    //分页查询所有用户
    @RequestMapping("/all")
    @PreAuthorize("hasAnyAuthority('/role/all')")
    public ModelAndView all(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "10") int size) {

        ModelAndView modelAndView = new ModelAndView();
        Page<Role> rolePage = roleService.findPage(page, size);
        modelAndView.addObject("rolePage",rolePage);  //传入查到的数据
        modelAndView.setViewName("/backstage/role_all");           //设置跳转的页面

        return modelAndView;
    }

    //新增角色，并重定向到角色管理页面
    @RequestMapping("/add")
    public String add(Role role) {
        roleService.add(role);

        return "redirect:/backstage/role/all";
    }

    //查询要修改的角色，并跳转到修改页面
    @RequestMapping("/edit")
    public ModelAndView edit(Integer rid) {
        Role role = roleService.findById(rid);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.setViewName("/backstage/role_edit");

        return modelAndView;
    }

    //根据角色id修改角色，并跳转到角色管理页面
    @RequestMapping("/update")
    public String update(Role role) {
        roleService.updateById(role);

        return "redirect:/backstage/role/all";
    }

    //根据角色id删除角色，并跳转到角色管理页面
    @RequestMapping("/delete")
    public String delete(Integer rid) {
        roleService.deleteById(rid);

        return "redirect:/backstage/role/all";
    }

    //分配角色权限
    @RequestMapping("/findPermission")
    public ModelAndView findPermission(Integer rid) {
        //查询角色拥有的状态权限
        List<PermissionWithStatus> permissions = roleService.findPermission(rid);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("permissions",permissions);
        modelAndView.addObject("rid",rid);
        modelAndView.setViewName("/backstage/role_findPermission");

        return modelAndView;
    }

    //修改用户角色
    @RequestMapping("/updatePermission")
    public String updatePermission(Integer rid,Integer pids[]) {
        roleService.updatePermission(rid, pids);

        return "redirect:/backstage/role/all";
    }

    //修改用户状态
//    @RequestMapping("/updateStatus")
//    public String updateStatus(Integer aid) {
//        roleService.updateStatus(aid);
//
//        return "redirect:/backstage/role/all";
//    }

}
