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

import incheon.com.cmm.ComDefaultVO;
import incheon.com.cmm.ResponseCode;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.res.mng.cd.lgar.service.MngCdLgarService;
import incheon.res.mng.cd.lgar.vo.MngCdLgar;
import incheon.res.mng.cd.lgar.vo.MngCdLgarVO;
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 MngCdLgarController {

    @Resource
    private MngCdLgarService mngCdLgarService;

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

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

    /**
     * @MethodDESC : 법정읍면동리코드 조회.
     */
    @RequestMapping(value = "/MngCdLgarL010.do")
    public String selectLgarList(@RequestParam(defaultValue = "1") int pageIndex, @ModelAttribute("mngCdLgarVO") MngCdLgarVO 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 = mngCdLgarService.countLgar(vo);
        paginationInfo.setTotalRecordCount(totCnt);
        model.addAttribute("paginationInfo", paginationInfo);


        List resultList = mngCdLgarService.selectLgarList(vo);

        model.addAttribute("resultList", resultList);
        model.addAttribute("resultCnt", totCnt);
        return "res/mng/cd/lgar/MngCdLgarL010";
    }

    /**
     * @MethodDESC : 법정읍면동리코드 삭제
     */
    @RequestMapping(value = "/MngCdLgarD010.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> deleteLgar(@ModelAttribute MngCdLgarVO vo) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();

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

    /**
     * @MethodDESC : 법정읍면동리코드 등록 하기위한 전 처리
     */
    @RequestMapping(value = "/MngCdLgarC010.do")
    public String insertLgarView(@ModelAttribute("mngCdLgarVO") MngCdLgarVO vo) throws Exception {
        return "res/mng/cd/lgar/MngCdLgarC010";
    }

    /**
     * @MethodDESC : 법정읍면동리코드 등록
     */
    @RequestMapping("/MngCdLgarC011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insertLgar(@ModelAttribute MngCdLgarVO vo) throws Exception {

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

        if(mngCdLgarService.chkLgar(vo) > 0){
            resultMap.put("message", "이미 존재하는 법정읍면동리코드 입니다.");
            return ResponseEntity.badRequest().body(DefaultApiResponse.error(500, "중복", "duplicated"));
        }

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


    /**
     * @MethodDESC : 법정읍면동코드 수정 하기위한 전 처리
     */
    @RequestMapping("/LayerPopMngCdLgarU010.do")
    public String updateLgarView(@ModelAttribute("mngCdLgarVO") MngCdLgarVO vo, ModelMap model) throws Exception {
        MngCdLgar result = mngCdLgarService.updateLgarView(vo);
        model.addAttribute("result", result);
        return "res/mng/cd/lgar/LayerPopMngCdLgarU010";
    }

    /**
     * @MethodDESC : 법정읍면동코드 수정
     */
    @RequestMapping("/MngCdLgarU011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> updateLgar(@ModelAttribute MngCdLgarVO vo) throws Exception {

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

        // 실제 수정 로직
        int result = mngCdLgarService.updateLgar(vo);

        resultMap.put("result", result);

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

        return ResponseEntity.ok(response);
    }
}
