package com.wsk.travelf.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsk.travelf.bean.RoleWithStatus;
import com.wsk.travelf.pojo.Admin;
import com.wsk.travelf.service.AdminService;
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/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

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

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

        return modelAndView;
    }

    //新增用户，并重定向到用户管理页面
    @RequestMapping("/add")
    public String add(Admin admin) {
        adminService.add(admin);

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

    //查询管理员，并跳转到修改页面
    @RequestMapping("/edit")
    public ModelAndView edit(Integer aid) {
        Admin admin = adminService.findById(aid);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("admin",admin);
        modelAndView.setViewName("/backstage/admin_edit");

        return modelAndView;
    }

    //根据用户id修改用户，并跳转到用户管理页面
    @RequestMapping("/update")
    public String update(Admin admin) {
        adminService.updateById(admin);

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

    //查询用户详情
    @RequestMapping("/desc")
    public ModelAndView desc(Integer aid) {
        Admin admin = adminService.findDesc(aid);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("admin",admin);
        modelAndView.setViewName("/backstage/admin_desc");

        return modelAndView;
    }

    //分配用户角色
    @RequestMapping("/findRole")
    public ModelAndView findRole(Integer aid) {
        //查询用户拥有的状态角色
        List<RoleWithStatus> roles = adminService.findRoleWithStatus(aid);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roles",roles);
        modelAndView.addObject("aid",aid);
        modelAndView.setViewName("/backstage/admin_findRole");

        return modelAndView;
    }

    //修改用户角色
    @RequestMapping("/updateRole")
    public String updateRole(Integer aid,Integer rids[]) {
        adminService.updateAdminRole(aid,rids);

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

    //修改用户状态
    @RequestMapping("/updateStatus")
    public String updateStatus(Integer aid) {
        adminService.updateStatus(aid);

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

}
