package incheon.ags.ias.logInq.web;

import incheon.ags.ias.logInq.service.LogInqService;
import incheon.ags.ias.logInq.vo.LogInqSearchVO;
import incheon.ags.ias.logInq.vo.LogInqVO;
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.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;

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

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

    @GetMapping("/ags/ias/logInq/logInqList.do")
    public String linkDataList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute LogInqSearchVO searchVO,
            ModelMap model) throws Exception {

        // 1. 전체 개수 조회
        int totalCount = logInqService.selectLogInqCnt(searchVO);

        // 2. 페이징 설정
        searchVO.setPageIndex(page);
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(page);
        paginationInfo.setRecordCountPerPage(searchVO.getRecordCountPerPage());
        paginationInfo.setPageSize(searchVO.getPageSize());
        paginationInfo.setTotalRecordCount(totalCount);

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

        List<Map<String,Object>> logInqList = logInqService.selectLogInqList(searchVO);

        model.addAttribute("logInqList", logInqList);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchVO", searchVO);

        return "ags/ias/logInq/logInqList";
    }

    @GetMapping("/ags/ias/logInq/logInqDetail.do")
    public String logInqDetail(
            @RequestParam(value = "webLogSn") Long webLogSn,
            ModelMap model) throws Exception {

        LogInqVO logInqVO = new LogInqVO();
        logInqVO.setWebLogSn(webLogSn);

        LogInqVO logInqDetail = logInqService.selectLogInqDetail(logInqVO);

        model.addAttribute("logInqDetail", logInqDetail);

        return "ags/ias/logInq/logInqDetail";
    }
}
