package com.wsk.travelf.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsk.travelf.pojo.Permission;
import com.wsk.travelf.service.PermissionService;
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/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

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

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

        return modelAndView;
    }

    //新增权限，并重定向到权限管理页面
    @RequestMapping("/add")
    public String add(Permission permission) {
        permissionService.add(permission);

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

    //查询要修改的权限，并跳转到修改页面
    @RequestMapping("/edit")
    public ModelAndView edit(Integer pid) {
        Permission permission = permissionService.findById(pid);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("permission",permission);
        modelAndView.setViewName("/backstage/permission_edit");

        return modelAndView;
    }

    //根据权限id修改权限，并跳转到权限管理页面
    @RequestMapping("/update")
    public String update(Permission permission) {
        permissionService.updateById(permission);

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

    //根据权限id删除权限，并跳转到权限管理页面
    @RequestMapping("/delete")
    public String delete(Integer pid) {
        permissionService.deleteById(pid);

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

    //分配权限权限
//    @RequestMapping("/findPermission")
//    public ModelAndView findPermission(Integer rid) {
//        //查询权限拥有的状态权限
//        List<PermissionWithStatus> permissions = permissionService.findPermission(rid);
//
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("permissions",permissions);
//        modelAndView.addObject("rid",rid);
//        modelAndView.setViewName("/backstage/permission_findPermission");
//
//        return modelAndView;
//    }

    //修改用户权限
//    @RequestMapping("/updatePermission")
//    public String updatePermission(Integer rid,Integer pids[]) {
//        permissionService.updatePermission(rid, pids);
//
//        return "redirect:/backstage/permission/all";
//    }

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

}
