package incheon.ags.ias.dept.web;

import incheon.ags.ias.dept.service.DeptService;
import incheon.ags.ias.dept.vo.DeptSearchVO;
import incheon.ags.ias.dept.vo.DeptVO;
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 DeptController {
    private final DeptService deptService;

    @GetMapping("/ags/ias/dept/deptList.do")
    public String deptList(
            @RequestParam(defaultValue = "1") int page,
            @ModelAttribute DeptSearchVO searchVO,
            ModelMap model) throws Exception {

        // 1. 총 개수 조회
        int totalCount = deptService.selectDeptCnt(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>> deptList = deptService.selectDeptList(searchVO);

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

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

        return "ags/ias/dept/deptList";
    }

    @GetMapping("/ags/ias/dept/deptDetail.do")
    public String deptDetail(
            @RequestParam(defaultValue = "1") int page,
            ModelMap model, @RequestParam(required = false) String deptCd) throws Exception {

        DeptVO vo = new DeptVO();
        vo.setDeptCd(deptCd);
        DeptVO deptDetail = deptService.selectDeptDetail(vo);

        model.addAttribute("deptDetail", deptDetail);

        return "ags/ias/dept/deptDetail";
    }


    @GetMapping("/ags/ias/dept/deptRegist.do")
    public String deptRegist(
            @RequestParam(defaultValue = "1") int page,
            ModelMap model, @RequestParam(required = false) String deptCd) throws Exception {

        return "ags/ias/dept/deptRegist";
    }

    @GetMapping("/ags/ias/dept/deptModify.do")
    public String deptModify(
            @RequestParam(defaultValue = "1") int page,
            ModelMap model, @RequestParam(required = false) String deptCd) throws Exception {

        DeptVO vo = new DeptVO();
        vo.setDeptCd(deptCd);
        DeptVO deptDetail = deptService.selectDeptDetail(vo);

        model.addAttribute("deptDetail", deptDetail);

        return "ags/ias/dept/deptModify";
    }

    /**
     * 부서 선택 팝업 화면
     * AJAX 레이어 팝업으로 사용됨
     */
    @GetMapping("/ags/ias/dept/deptSelectPopup.do")
    public String deptSelectPopup(ModelMap model) throws Exception {
        log.debug("부서 선택 팝업 화면 요청");
        return "/ags/ias/dept/deptSelectPopup";
    }
}
