package incheon.res.mng.cd.cchk.web;

import incheon.com.cmm.ComDefaultVO;
import incheon.com.cmm.ResponseCode;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.res.mng.cd.cchk.service.MngCdCchkService;
import incheon.res.mng.cd.cchk.vo.MngCdCchkVO;
import org.apache.log4j.Logger;
import org.egovframe.rte.fdl.property.EgovPropertyService;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springmodules.validation.commons.DefaultBeanValidator;

import javax.annotation.Resource;
import javax.enterprise.inject.Model;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SessionAttributes(types = ComDefaultVO.class)
@RequestMapping("${api.rcs-path}")
public class MngCdCchkController {
    @Resource
    private MngCdCchkService mngCdCchkService;

    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
    @Autowired
    private DefaultBeanValidator beanValidator;
    Logger logger = Logger.getLogger(this.getClass());

    /**
     * @MethodDESC : 체크코드 조회.
     */
    @RequestMapping(value = "/MngCdCchkL010.do")
    public String selectCchkList(@RequestParam(defaultValue = "1") int pageIndex, @ModelAttribute("mngCdCchkVO") MngCdCchkVO vo, ModelMap model) throws Exception{

        vo.setPageIndex(pageIndex);

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

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

        int totCnt = mngCdCchkService.countCchk(vo);
        paginationInfo.setTotalRecordCount(totCnt);

        model.addAttribute("resultCnt", totCnt);
        model.addAttribute("paginationInfo", paginationInfo);

        //체크코드 페이지 리스트
        List resultList = mngCdCchkService.selectCchkList(vo);
        model.addAttribute("resultList", resultList);

        //코드설명 select리스트
        List cdeExpList = mngCdCchkService.selectCdeexpList(vo);
        model.addAttribute("cdeExpList", cdeExpList);

        return "res/mng/cd/cchk/MngCdCchkL010";
    }

    /**
     * @MethodDESC : 체크코드 등록페이지 이동.
     */

    @RequestMapping(value = "/MngCdCchkC010.do")
    public String insertCchkView(@ModelAttribute("mngCdCchkVO") MngCdCchkVO vo, ModelMap model) throws Exception{
        //코드설명 select리스트
        List cdeExpList = mngCdCchkService.selectCdeexpList(vo);
        model.addAttribute("cdeExpList", cdeExpList);

        return "res/mng/cd/cchk/MngCdCchkC010";
    }

    /**
     *
     * @MethodDESC : 체크코드 등록
     */

    @RequestMapping(value = "/MngCdCchkC011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insertCchk(@ModelAttribute MngCdCchkVO vo) throws Exception{

        Map<String, Object> resultMap = new HashMap<String, Object>();

        int result = mngCdCchkService.insertCchk(vo);
        resultMap.put("result", result);
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));

    }

    /**
     *
     * @MethodDESC : 체크코드 삭제
     */

    @RequestMapping(value = "/MngCdCchkD010.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> deleteCchk(@ModelAttribute MngCdCchkVO vo) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();

        mngCdCchkService.deleteCchk(vo);
        resultMap.put("success", true);
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }
}
