package com.dbnt.sims.menuMgt;

import com.dbnt.sims.codeMgt.service.CodeMgtService;
import com.dbnt.sims.menuMgt.service.MenuMgtService;
import com.dbnt.sims.menuMgt.model.MenuMgt;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("/menuMgt")
public class MenuMgtController {

  private final MenuMgtService menuMgtService;
  private final CodeMgtService codeMgtService;

  @GetMapping("/menuMgtPage")
  public ModelAndView menuMgtPage(MenuMgt menuMgt) {
    ModelAndView mav = new ModelAndView("adminPage/menuMgt/menuMgt");
    menuMgt.setQueryInfo();
    mav.addObject("menuMgtList", menuMgtService.selectMenuMgtList(menuMgt));
    menuMgt.setContentCnt(menuMgtService.selectMenuMgtListCnt(menuMgt));
    menuMgt.setPaginationInfo();
    mav.addObject("searchParams", menuMgt);
    return mav;
  }

  @GetMapping("/menuEditModal")
  public ModelAndView menuEditModal(MenuMgt menuMgt){
    ModelAndView mav = new ModelAndView("adminPage/menuMgt/menuEditModal");
    mav.addObject("menuMgt", menuMgt);
    mav.addObject("cat1List", codeMgtService.selectCodeMgtList("CAT1"));
    mav.addObject("cat2List", codeMgtService.selectCodeMgtList("CAT2"));
    mav.addObject("cat3List", codeMgtService.selectCodeMgtList("CAT3"));
    return mav;
  }

  @PostMapping("/saveMenuMgt")
  public String saveMenuMgt(MenuMgt menuMgt){
    return menuMgtService.saveMenuMgt(menuMgt);
  }

  @PostMapping("/deleteMenuMgt")
  @ResponseBody
  public String deleteMenuMgt(@RequestBody List<MenuMgt> menuMgt){
    menuMgtService.deleteMenuMgt(menuMgt);
    return "";
  }

}
