package com.jwipc.sys.controller.pc;

import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.sys.dto.ButtonDTO;
import com.jwipc.sys.dto.MenuTreeDTO;
import com.jwipc.sys.dto.TreeDateDTO;
import com.jwipc.sys.entity.Button;
import com.jwipc.sys.logic.ButtonLogic;
import com.jwipc.sys.logic.MenuLogic;
import com.jwipc.sys.model.query.ResourcesListQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

@RequestMapping(MODULE_ROOT +"/resource")
@RequiredArgsConstructor
@RestController
public class ResourceRestController {
    private final MenuLogic resourceLogic;
    private final ButtonLogic buttonLogic;

    /**
     * 菜单列表
     * @param query 查询条件
     * @return 信息
     */
    @GetMapping("/listAll")
    public List<MenuTreeDTO> listAll(ResourcesListQuery query){
        return resourceLogic.listAll(query);
    }

    /**
     * 按钮列表
     * @param resourceId 查询条件
     * @return 信息
     */
    @RequestMapping("/button/listAll")
    public List<ButtonDTO> buttonListAll(Long resourceId){
        return buttonLogic.listByResourceId(resourceId);
    }

    /**
     * 菜单树列表
     * @param query 查询条件
     * @return 信息
     */
    @GetMapping("/treeList")
    public ResponseEntity treeList(ResourcesListQuery query){
        return ResponseEntity.ok(resourceLogic.treeList(query));
    }

    @GetMapping("getSubMenu")
    public ResponseEntity getSubMenu(Long parentId){
        final CurrentUserDTO currentUser = LoginUtil.getCurrentUser();
        return ResponseEntity.ok(resourceLogic.getSubMenu(parentId,currentUser.getRoleIds()));
    }

    /**
     * 保存更新
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(MenuTreeDTO param){
        resourceLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 保存更新
     * @param param 参数
     */
    @RequestMapping("/button/saveOrUpdate")
    public ResponseEntity buttonSaveOrUpdate(ButtonDTO param){
        buttonLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 删除
     * @param id 参数
     */
    @PostMapping("/updateHideFlag")
    public ResponseEntity updateHideFlag(Long id){
        resourceLogic.updateHideFlag(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 删除按钮
     * @param id 参数
     */
    @PostMapping("/button/remove")
    public ResponseEntity buttonRemove(Long id){
        buttonLogic.remove(id);
        return ResponseEntity.ok(true);
    }


}
