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

import incheon.ags.ias.srvy.srvyRspdnt.service.SrvyRspdntService;
import incheon.ags.ias.srvy.srvyRspdnt.vo.SrvyRspdntVO;
import incheon.ags.ias.srvy.srvyRspdnt.vo.SrvyRspdntSearchVO;
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;

/**
 * 설문 응답자 관리 Controller
 * - 설문 응답자 정보 관리 웹 페이지
 */
@Controller
@RequiredArgsConstructor
@Slf4j
@RequireRole(system = "AGS", roles = "ROLE_SUPER_ADMIN", description = "통합관리자 역할 접근 제어")
public class SrvyRspdntController {
    private final SrvyRspdntService srvyRspdntService;

    /**
     * 설문 응답자 목록 조회
     */
    @GetMapping("/ags/ias/srvyRspdnt/srvyRspdntList.do")
    public String srvyRspdntList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute SrvyRspdntSearchVO 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 = srvyRspdntService.selectSrvyRspdntListTotCnt(searchVO);
        model.addAttribute("totalCount", totalCount);
        paginationInfo.setTotalRecordCount(totalCount);

        List<SrvyRspdntVO> srvyRspdntList = srvyRspdntService.selectSrvyRspdntList(searchVO);
        model.addAttribute("srvyRspdntList", srvyRspdntList);
        model.addAttribute("searchVO", searchVO);
        model.addAttribute("paginationInfo", paginationInfo);

        return "/ags/ias/srvy/srvyRspdnt/srvyRspdntList";
    }

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

        model.addAttribute("srvySn", srvySn);

        return "/ags/ias/srvy/srvyRspdnt/srvyRspdntRegist";
    }


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


        SrvyRspdntVO vo = new SrvyRspdntVO();
        vo.setSrvyRspdntSn(Integer.parseInt(srvyRspdntSn));
        SrvyRspdntVO srvyRspdntDetail = srvyRspdntService.selectSrvyRspdntDetail(vo);
        model.addAttribute("resultList", srvyRspdntDetail);

        return "/ags/ias/srvy/srvyRspdnt/srvyRspdntDetail";
    }

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


        SrvyRspdntVO vo = new SrvyRspdntVO();
        vo.setSrvyRspdntSn(Integer.parseInt(srvyRspdntSn));
        SrvyRspdntVO srvyRspdntDetail = srvyRspdntService.selectSrvyRspdntDetail(vo);
        model.addAttribute("surveySrvyRspdntMngVO", srvyRspdntDetail);

        return "/ags/ias/srvy/srvyRspdnt/srvyRspdntModify";
    }

}