package incheon.ags.ias.sys.web;

import incheon.ags.ias.menu.service.MenuService;
import incheon.ags.ias.sys.service.SysService;
import incheon.ags.ias.sys.vo.SysSearchVO;
import incheon.ags.ias.sys.vo.SysVO;
import incheon.com.security.annotation.RequireRole;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequiredArgsConstructor
@Slf4j
@RequireRole(system = "AGS", roles = "ROLE_SUPER_ADMIN", description = "통합관리자 역할 접근 제어")
public class SysController {
    private final MenuService menuService;
    private final SysService sysService;

    @GetMapping("/ags/ias/sys/sysList.do")
    public String sysList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute SysSearchVO vo,
            ModelMap model) throws Exception {

        vo.setPageIndex(page);

        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(page);
        paginationInfo.setRecordCountPerPage(vo.getRecordCountPerPage());
        paginationInfo.setPageSize(vo.getPageSize());

        // 3. 페이징 파라미터 설정
        vo.setFirstIndex(paginationInfo.getFirstRecordIndex());
        vo.setLastIndex(paginationInfo.getLastRecordIndex());
        vo.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());

        List<Map<String,Object>> sysList = sysService.selectSysList(vo);
        int totalCount = sysService.selectSysListCnt(vo);
        model.addAttribute("totalCount", totalCount);
        paginationInfo.setTotalRecordCount(totalCount);

        model.addAttribute("sysList", sysList);
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchVO", vo);

        return "ags/ias/sys/sysList";
    }

    @GetMapping("/ags/ias/sys/sysRegist.do")
    public String sysRegist(
            ModelMap model) throws Exception {

        List<Map<String,Object>> sysDeptList = sysService.selectSysDeptList();
        model.addAttribute("sysDeptList", sysDeptList);

        return "ags/ias/sys/sysRegist";
    }

    @GetMapping("/ags/ias/sys/sysDetail.do")
    public String sysDetail(
        @RequestParam(value = "sysCd", required = false) String sysCd,
        ModelMap model) throws Exception {

        SysVO sysVO = new SysVO();
        sysVO.setSysCd(sysCd);
        SysVO result = sysService.selectSysDetail(sysVO);
        model.addAttribute("sysDetail", result);

        List<Map<String,Object>> sysDeptList = sysService.selectSysDeptList();
        model.addAttribute("sysDeptList", sysDeptList);

        return "ags/ias/sys/sysDetail";
    }

    @GetMapping("/ags/ias/sys/sysModify.do")
    public String sysModify(
            @RequestParam(value = "sysCd", required = false) String sysCd,
            ModelMap model) throws Exception {

        SysVO sysVO = new SysVO();
        sysVO.setSysCd(sysCd);
        SysVO result = sysService.selectSysDetail(sysVO);
        model.addAttribute("sysDetail", result);

        List<Map<String,Object>> sysDeptList = sysService.selectSysDeptList();
        model.addAttribute("sysDeptList", sysDeptList);

        return "ags/ias/sys/sysModify";
    }
}
