package incheon.ags.ias.trms.web;

import incheon.ags.ias.trms.service.TrmsService;
import incheon.ags.ias.trms.vo.TrmsSearchVO;
import incheon.ags.ias.trms.vo.TrmsVO;
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
public class RcsTrmsController {

    private final TrmsService trmsService;

    @GetMapping("/rcs/ias/trms/trmsList.do")
    public String trmsList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute TrmsSearchVO searchVO,
            ModelMap model) throws Exception {

        // 1. 총 개수 조회
        int totalCount = trmsService.selectTrmsListTotCnt(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());

        // 4. 약관 리스트 조회
        List<Map<String,Object>> trmsList = trmsService.selectTrmsList(searchVO);

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

        log.info("페이지: {}, 시작: {}, 페이지당개수: {}, 총개수: {}, 조회개수: {}",
                page, searchVO.getFirstIndex(), searchVO.getRecordCountPerPage(),
                totalCount, trmsList.size());

        return "ags/ias/trms/trmsList";
    }

    @GetMapping("/rcs/ias/trms/trmsDetail.do")
    public String trmsDetail(
            @RequestParam Long trmsSn,
            ModelMap model) throws Exception {

        TrmsVO trmsVO = trmsService.selectTrmsDetail(trmsSn);
        model.addAttribute("trmsVO", trmsVO);

        return "ags/ias/trms/trmsDetail";
    }

    @GetMapping("/rcs/ias/trms/trmsRegist.do")
    public String trmsRegist(ModelMap model) throws Exception {

        model.addAttribute("mode", "insert");

        return "ags/ias/trms/trmsRegist";
    }

    @GetMapping("/rcs/ias/trms/trmsModify.do")
    public String trmsModify(
            @RequestParam Long trmsSn,
            ModelMap model) throws Exception {

        TrmsVO trmsVO = trmsService.selectTrmsDetail(trmsSn);
        model.addAttribute("trmsVO", trmsVO);

        model.addAttribute("mode", "update");

        return "ags/ias/trms/trmsModify";
    }
}
