package incheon.ags.ias.srvy.srvyRspns.web;

import incheon.ags.ias.srvy.srvyRspns.service.SrvyRspnsService;
import incheon.ags.ias.srvy.srvyRspns.vo.SrvyRspnsVO;
import incheon.ags.ias.srvy.srvyRspns.vo.SrvyRspnsSearchVO;
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
 * - 설문 응답 정보 관리 웹 페이지
 */
@Controller
@RequiredArgsConstructor
@Slf4j
@RequireRole(system = "AGS", roles = "ROLE_SUPER_ADMIN", description = "통합관리자 역할 접근 제어")
public class SrvyRspnsController {
    private final SrvyRspnsService srvyRspnsService;

    /**
     * 설문 응답 목록 조회
     */
    @GetMapping("/ags/ias/srvyRspns/srvyRspnsList.do")
    public String srvyRspnsList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute SrvyRspnsSearchVO searchVO,
            ModelMap model) throws Exception {


        searchVO.setPageIndex(page);

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

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

        int totalCount = srvyRspnsService.selectSrvyRspnsListTotCnt(searchVO);
        model.addAttribute("totalCount", totalCount);
        paginationInfo.setTotalRecordCount(totalCount);

        List<Map<String, Object>> srvyRspnsList = srvyRspnsService.selectSrvyRspnsList(searchVO);
        model.addAttribute("srvyRspnsList", srvyRspnsList);
        model.addAttribute("searchVO", searchVO);
        model.addAttribute("paginationInfo", paginationInfo);

        return "/ags/ias/srvy/srvyRspns/srvyRspnsList";
    }

    /**
     * 설문 응답 등록 페이지
     */
    @GetMapping("/ags/ias/srvyRspns/srvyRspnsRegist.do")
    public String srvyRspnsRegist(
            @RequestParam(value = "srvySn", required = false) String srvySn,
            @RequestParam(value = "srvyRspdntSn", required = false) String srvyRspdntSn,
            ModelMap model) throws Exception {

        List<Map<String, Object>> surveyList = srvyRspnsService.selectSrvyList(new SrvyRspnsVO());
        model.addAttribute("surveyList", surveyList);

        List<Map<String, Object>> rspdntList = srvyRspnsService.selectSrvyRspdnt();
        model.addAttribute("rspdntList", rspdntList);

        List<Map<String, Object>> qitemList = srvyRspnsService.selectSrvyQitem();
        model.addAttribute("qitemList", qitemList);

        List<Map<String, Object>> artclList = srvyRspnsService.selectSrvyQitemArtcl();
        model.addAttribute("artclList", artclList);

        model.addAttribute("srvySn", srvySn);
        model.addAttribute("srvyRspdntSn", srvyRspdntSn);

        return "/ags/ias/srvy/srvyRspns/srvyRspnsRegist";
    }


    /**
     * 설문 응답 상세 조회
     */
    @GetMapping("/ags/ias/srvyRspns/srvyRspnsDetail.do")
    public String srvyRspnsDetail(
            @RequestParam(value = "srvyRspnsSn", required = false) String srvyRspnsSn,
            ModelMap model) throws Exception {

        SrvyRspnsVO vo = new SrvyRspnsVO();
        vo.setRspnsSn(Integer.parseInt(srvyRspnsSn));
        SrvyRspnsVO srvyRspnsDetail = srvyRspnsService.selectSrvyRspnsDetail(vo);
        model.addAttribute("resultList", srvyRspnsDetail);

        return "/ags/ias/srvy/srvyRspns/srvyRspnsDetail";
    }

    /**
     * 설문 응답 수정 페이지
     */
    @GetMapping("/ags/ias/srvyRspns/srvyRspnsModify.do")
    public String srvyRspnsModify(
            @RequestParam(value = "srvyRspnsSn", required = false) String srvyRspnsSn,
            ModelMap model) throws Exception {

        SrvyRspnsVO vo = new SrvyRspnsVO();
        vo.setRspnsSn(Integer.parseInt(srvyRspnsSn));
        SrvyRspnsVO srvyRspnsDetail = srvyRspnsService.selectSrvyRspnsDetail(vo);
        model.addAttribute("resultList", srvyRspnsDetail);

        List<Map<String, Object>> surveyList = srvyRspnsService.selectSrvyList(new SrvyRspnsVO());
        model.addAttribute("surveyList", surveyList);

        List<Map<String, Object>> rspdntList = srvyRspnsService.selectSrvyRspdnt();
        model.addAttribute("rspdntList", rspdntList);

        List<Map<String, Object>> qitemList = srvyRspnsService.selectSrvyQitem();
        model.addAttribute("qitemList", qitemList);

        List<Map<String, Object>> artclList = srvyRspnsService.selectSrvyQitemArtcl();
        model.addAttribute("artclList", artclList);

        return "/ags/ias/srvy/srvyRspns/srvyRspnsModify";
    }

}
