package incheon.res.mng.govoff.web;

import incheon.com.cmm.ComDefaultVO;
import incheon.com.cmm.ResponseCode;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.com.security.vo.LoginVO;
import incheon.res.mng.danga.service.MngDangaService;
import incheon.res.mng.govoff.service.RdmOeGovoffService;
import incheon.res.mng.govoff.vo.RdmOeGovoff;
import incheon.res.mng.govoff.vo.RdmOeGovoffVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.egovframe.rte.fdl.property.EgovPropertyService;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
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 javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description :  처리로직 - 자치단체 관리 Controller
 */
@Controller
@SessionAttributes(types = ComDefaultVO.class)
@RequestMapping("${api.rcs-path}")
public class RdmOeGovoffController {

	@Resource
    private RdmOeGovoffService rdmOeGovoffService;

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

	protected Log log = LogFactory.getLog(this.getClass());


	/**
	 * @MethodDESC : 자치단체 리스트 조회.
	 */
	@RequestMapping(value = "/RdmOeGovoffL010.do")
	public String rdmOeGovoffList(@RequestParam(defaultValue = "1") int pageIndex, @ModelAttribute("rdmOeGovoffVO") RdmOeGovoffVO 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());

		// 조회결과
        List resultList = rdmOeGovoffService.selectGovoffList(vo);
        model.addAttribute("resultList", resultList);

        int totCnt = rdmOeGovoffService.countList(vo);
        paginationInfo.setTotalRecordCount(totCnt);
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchVo", vo);

		return "res/mng/govoff/RdmOeGovoffL010";
	}

	/**
	 * @MethodDESC : 자치단체정보 상세조회
	 */
	@RequestMapping("/RdmOeGovoffR010.do")
	public String selectGovoffView (@ModelAttribute("rdmOeGovoffVO") RdmOeGovoffVO vo, ModelMap model) throws Exception {

		RdmOeGovoff result = rdmOeGovoffService.selectGovoff(vo);
		model.addAttribute("result", result);
		model.addAttribute("searchVO", vo);

		return "res/mng/govoff/RdmOeGovoffR010";
	}

	/**
	 * @MethodDESC : 자치단체정보 등록화면
	 */
	@RequestMapping("/RdmOeGovoffC010.do")
	public String insertGovoffView (@ModelAttribute("rdmOeGovoffVO") RdmOeGovoffVO vo, ModelMap model ) throws Exception {

		model.addAttribute("rdmOeGovoffVO", vo);
		model.addAttribute("searchVo", vo);

		return "res/mng/govoff/RdmOeGovoffC010";
	}

	/**
	 * @MethodDESC : 자치단체정보 등록처리
	 */
	@RequestMapping("/rdm/oe/govoff/insertGovoff.do")
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insertGovoff (@ModelAttribute("loginUser") LoginVO loginVO, @ModelAttribute RdmOeGovoffVO vo) throws Exception {

		vo.setUserAcntMngNo(loginVO.getUserUnqId());

		rdmOeGovoffService.insertGovoff(vo);


		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", "succ");

		return ResponseEntity.ok(DefaultApiResponse.success(resultMap));

	}

	/**
	 * @MethodDESC : 자치단체정보 수정화면
	 */
	@RequestMapping("/RdmOeGovoffU010.do")
	public String updateGovoffView (@ModelAttribute("rdmOeGovoffVO") RdmOeGovoffVO vo, ModelMap model) throws Exception {

		RdmOeGovoff result = rdmOeGovoffService.selectGovoff(vo);
		model.addAttribute("rdmOeGovoffVO", result);
		model.addAttribute("searchVO", vo);

		return "res/mng/govoff/RdmOeGovoffU010";
	}


	/**
	 * @MethodDESC : 자치단체정보 수정처리
	 */
	@RequestMapping("/RdmOeGovoffU011.do")
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> updateGovoff (@Valid @ModelAttribute RdmOeGovoffVO vo) throws Exception {
		rdmOeGovoffService.updateGovoff(vo);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", "succ");

		return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
	}

	/**
	 * @MethodDESC : 자치단체정보 삭제처리
	 */
	@RequestMapping("/RdmOeGovoffR011.do")
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> deleteGovoff (@ModelAttribute RdmOeGovoffVO vo) throws Exception {

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

		rdmOeGovoffService.deleteGovoff(vo);
		resultMap.put("success", true);
		return ResponseEntity.ok(DefaultApiResponse.success(resultMap));

	}



	/**
	 * @MethodDESC : 관리기관팝업
	 */
	@RequestMapping("/rdm/oe/govoff/RdmOeGovoffL020.do")
	public String selectMngListPopup (@RequestParam(defaultValue = "1") int pageIndex, @ModelAttribute("rdmOeGovoffVO") RdmOeGovoffVO 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());


        List resultList = rdmOeGovoffService.selectMngListPopup(vo);

        model.addAttribute("resultList", resultList);

        // 총카운트 For paging
        int totCnt = rdmOeGovoffService.countMngPopList(vo);

        paginationInfo.setTotalRecordCount(totCnt);
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchVo", vo);

		return "res/mng/govoff/LayerPopRdmOeGovoffL020";
	}

}
