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

import incheon.com.cmm.ComDefaultVO;
import incheon.com.cmm.ResponseCode;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.res.mng.cd.code.service.MngCdCodeService;
import incheon.res.mng.cd.code.vo.MngCdCodeVO;
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.Model;
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.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 MngCdCodeController {
    @Resource
    private MngCdCodeService mngCdCodeService;

    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;

    @Autowired
    private DefaultBeanValidator beanValidator;
    Logger logger = Logger.getLogger(this.getClass());

    /**
     * @MethodDESC : 공통코드 조회.
     */

    @RequestMapping(value = "/MngCdCodeL010.do")
    public String selectCodeList(@RequestParam(defaultValue = "1") int pageIndex, @ModelAttribute("mngCdCodeVO") MngCdCodeVO 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 = mngCdCodeService.countCode(vo);
        paginationInfo.setTotalRecordCount(totCnt);

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

        //공통코드 페이지 리스트
        List resultList = mngCdCodeService.selectCodeList(vo);
        model.addAttribute("resultList", resultList);

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

        return "res/mng/cd/code/MngCdCodeL010";
    }

    /**
     * @MethodDESC : 공통코드 등록페이지 이동.
     */
    @RequestMapping(value = "/MngCdCodeC010.do")
    public String insertCodeView(@ModelAttribute("mngCdCodeVO") MngCdCodeVO vo, ModelMap model) throws Exception{
        //코드설명 select리스트
        List cdeExpList = mngCdCodeService.selectCdeexpList(vo);
        model.addAttribute("cdeExpList", cdeExpList);
        return "res/mng/cd/code/MngCdCodeC010";
    }

    /**
     * @MethodDESC : 공통코드 등록
     */
    @RequestMapping(value = "/MngCdCodeC011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insertCode(@ModelAttribute MngCdCodeVO vo) throws Exception{

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

        int result = mngCdCodeService.insertCode(vo);
        resultMap.put("result", result);
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }

    /**
     * @MethodDESC : 공통코드 삭제
     */
    @RequestMapping(value = "/MngCdCodeD010.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> deleteCode(@ModelAttribute MngCdCodeVO vo) throws Exception{
         Map<String, Object> resultMap = new HashMap<String, Object>();

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