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

import incheon.com.cmm.ComDefaultVO;
import incheon.com.cmm.ResponseCode;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.res.mng.cd.post.service.MngCdPostService;
import incheon.res.mng.cd.post.vo.MngCdPost;
import incheon.res.mng.cd.post.vo.MngCdPostVO;
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.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SessionAttributes(types = ComDefaultVO.class)
@RequestMapping("${api.rcs-path}")
public class MngCdPostController {

    @Resource
    private MngCdPostService mngCdPostService;

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

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

    /**
     * @MethodDESC : 우편번호 조회.
     */
    @RequestMapping(value = "/MngCdPostL010.do")
    public String selectPostList(@RequestParam(defaultValue = "1") int pageIndex, @ModelAttribute("mngCdPostVO") MngCdPostVO 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 = mngCdPostService.countPost(vo);
        paginationInfo.setTotalRecordCount(totCnt);
        model.addAttribute("paginationInfo", paginationInfo);

        List resultList = mngCdPostService.selectPostList(vo);

        model.addAttribute("resultList", resultList);
        model.addAttribute("resultCnt", totCnt);
        model.addAttribute("searchVo", vo);

        return "res/mng/cd/post/MngCdPostL010";
    }

    /**
     * @MethodDESC : 우편번호 삭제
     */
    @RequestMapping(value = "/MngCdPostD010.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> deletePost(@ModelAttribute MngCdPostVO vo) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();

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


    /**
     * @MethodDESC : 우편번호 등록 하기위한 전 처리
     */
    @RequestMapping(value = "/MngCdPostC010.do")
    public String insertPostView(@ModelAttribute("mngCdPostVO") MngCdPostVO vo) throws Exception {
        return "res/mng/cd/post/MngCdPostC010";
    }

    /**
     * @MethodDESC : 우편번호 등록
     */
    @RequestMapping("/MngCdPostC011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insertPost(@ModelAttribute MngCdPostVO vo) throws Exception {

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

        if(mngCdPostService.chkPost(vo) > 0){
            resultMap.put("message", "이미 존재하는 우편번호 입니다.");
            return ResponseEntity.badRequest().body(DefaultApiResponse.error(500, "중복", "duplicated"));
        }

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

    /**
     * @MethodDESC : 우편번호 수정 하기위한 전 처리
     */
    @RequestMapping("/LayerPopMngCdPostU010.do")
    public String updatePostView(@ModelAttribute("mngCdPostVO") MngCdPostVO vo, ModelMap model) throws Exception {

        MngCdPost result = mngCdPostService.updatePostView(vo);
        model.addAttribute("result", result);
        return "res/mng/cd/post/LayerPopMngCdPostU010";
    }


    /**
     * @MethodDESC : 우편번호 수정
     */
    @RequestMapping("/MngCdPostU011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> updatePost(@ModelAttribute MngCdPostVO vo) throws Exception {

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

        int result = mngCdPostService.updatePost(vo);

        resultMap.put("result", result);

        DefaultApiResponse<Map<String, Object>> response = DefaultApiResponse.success(resultMap);

        return ResponseEntity.ok(response);
    }
}
