package cn.reghao.devops.mgr.admin.controller;

import cn.reghao.devops.mgr.admin.model.vo.MenuTree;
import cn.reghao.devops.mgr.admin.service.MenuService;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.devops.mgr.admin.model.dto.MenuDto;
import cn.reghao.devops.mgr.admin.model.po.Menu;
import cn.reghao.jutil.jdk.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "资源接口")
@RequestMapping("/api/rbac/menu")
@RestController
public class MenuController {
    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @ApiOperation(value = "添加资源", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String addMenu(@Validated Menu menu) {
        Result result = menuService.addMenu(menu);
        return WebResult.result(result);
    }

    @ApiOperation(value = "修改资源", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public String modifyMenu(@Validated MenuDto menuDto) {
        Result result = menuService.updateMenu(menuDto);
        return WebResult.result(result);
    }

    @ApiOperation(value = "修改资源状态", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/status/{enabled}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String menuStatus(@PathVariable("enabled") boolean enabled, @RequestParam("ids") List<Integer> ids) {
        menuService.updateMenusStatus(enabled, ids);
        return WebResult.success();
    }

    @ApiOperation(value = "删除资源", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteMenu(@PathVariable("id") Integer menuId) {
        /*Result result = menuService.deleteMenu(menuId);
        return WebResult.result(result);*/
        return WebResult.failWithMsg("接口未实现");
    }

    @ApiOperation(value = "获取指定状态的菜单", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping(value = "/ztree/{enabled}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String list(@PathVariable(value = "enabled") Boolean enabled) {
        List<MenuTree> list = menuService.getMenusByStatus(enabled);
        return WebResult.success(list);
    }

    @ApiOperation(value = "返回所有 dir menu", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping(value = "/ztree/parent", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getDirMenus() {
        List<MenuTree> list = menuService.getDirMenus();
        return WebResult.success(list);
    }

    @ApiOperation(value = "对同一 pid 组内的资源进行排序", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping(value = "/sorted/{pid}/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String sortList(@PathVariable(value = "pid") int pid,
                           @PathVariable(value = "id", required = false) Menu menu) {
        /*Map<Integer, String> map = menuService.getSortedChildGroupByPid(pid);
        // 排除当前 menu
        if (menu != null) {
            map.remove(menu.getPos());
        }*/
        return WebResult.failWithMsg("接口未实现");
    }
}
