package com.jwipc.sys.controller.pc;

import com.jwipc.sys.dto.ButtonDTO;
import com.jwipc.sys.dto.MenuTreeDTO;
import com.jwipc.sys.entity.Button;
import com.jwipc.sys.logic.ButtonLogic;
import com.jwipc.sys.logic.MenuLogic;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

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

/**
 * @author shenyang.ou
 * @date 2024/1/29 10:58
 * @desc
 */

@Controller
@RequestMapping(MODULE_ROOT + "/resource")
@RequiredArgsConstructor
public class ResourcePageController {
    private final MenuLogic resourceLogic;
    private final ButtonLogic buttonLogic;
    @GetMapping("index/{menuType}")
    public String index(Model model,@PathVariable(value = "menuType") String menuType) {
        model.addAttribute("menuType", menuType);
        return MODULE_ROOT + "/resources/list";
    }

    /**
     * 菜单编辑页面
     */
    @GetMapping("detail")
    public String detail(Long id, Long parentId, Model model,String menuType) {
        MenuTreeDTO menuTreeDTO = MenuTreeDTO.builder().build();
        if (id != null) {
            menuTreeDTO = resourceLogic.getById(id);
        } else {
            menuTreeDTO.setParentId(parentId);
        }
        model.addAttribute("resources", menuTreeDTO);
        model.addAttribute("menuType", menuType);
        return MODULE_ROOT + "/resources/detail";
    }

    /**
     * 菜单按钮分配页面
     */
    @GetMapping("/buttons")
    public String buttons(Long id, Model model) {
        model.addAttribute("id", id);
        List<ButtonDTO> list = buttonLogic.listByResourceId(id);
        List<String> buttons = list.stream().map(ButtonDTO::getButton).toList();
        model.addAttribute("buttons", buttons.toString());
        return MODULE_ROOT + "/resources/buttons";
    }


    /**
     * 菜单按钮分配页面
     */
    @GetMapping("/button/detail")
    public String buttonDetail(Long id, Model model) {
        Button button;
        if (id != null) {
            button = buttonLogic.getById(id);
        } else {
            button = new Button();
        }
        model.addAttribute("button", button);

        return MODULE_ROOT + "/resources/button_detail";
    }
}
