package incheon.ags.ias.tmap.web;

import incheon.ags.ias.tmap.service.TmapService;
import incheon.ags.ias.tmap.vo.TmapSearchVO;
import incheon.ags.ias.tmap.vo.TmapVO;
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 (JSP)
 */
@Controller
@RequiredArgsConstructor
@Slf4j
public class TmapController {

    private final TmapService tmapService;

    /**
     * 주제도 목록 페이지
     */
    @GetMapping("/ags/ias/tmap/tmapList.do")
    public String tmapList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute TmapSearchVO vo,
            ModelMap model) throws Exception {

        vo.setPageIndex(page);

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

        vo.setFirstIndex(paginationInfo.getFirstRecordIndex());
        vo.setLastIndex(paginationInfo.getLastRecordIndex());
        vo.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());

        List<Map<String, Object>> tmapList = tmapService.selectTmapList(vo);
        int totalCount = tmapService.selectTmapListCnt(vo);

        paginationInfo.setTotalRecordCount(totalCount);

        model.addAttribute("tmapList", tmapList);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchVO", vo);

        return "ags/ias/tmap/tmapList";
    }

    /**
     * 주제도 등록 페이지
     */
    @GetMapping("/ags/ias/tmap/tmapRegist.do")
    public String tmapRegist(ModelMap model) throws Exception {
        return "ags/ias/tmap/tmapRegist";
    }

    /**
     * 주제도 상세 페이지
     */
    @GetMapping("/ags/ias/tmap/tmapDetail.do")
    public String tmapDetail(
            @RequestParam(value = "tmapSn", required = false) Long tmapSn,
            ModelMap model) throws Exception {

        TmapVO result = tmapService.selectTmapDetail(tmapSn);
        model.addAttribute("tmapDetail", result);

        return "ags/ias/tmap/tmapDetail";
    }

    /**
     * 주제도 수정 페이지
     */
    @GetMapping("/ags/ias/tmap/tmapModify.do")
    public String tmapModify(
            @RequestParam(value = "tmapSn", required = false) Long tmapSn,
            ModelMap model) throws Exception {

        TmapVO result = tmapService.selectTmapDetail(tmapSn);
        model.addAttribute("tmapDetail", result);

        return "ags/ias/tmap/tmapModify";
    }
}
