package incheon.res.rdm.dp.acceptreceiv.web;


import incheon.com.cmm.api.DefaultApiResponse;
import incheon.com.security.vo.LoginVO;
import incheon.res.rdm.com.code.service.RdmComCodeService;
import incheon.res.rdm.com.digplan.service.RdmComDigplanService;
import incheon.res.rdm.com.digplan.vo.RdmComDigplanVO;
import incheon.res.rdm.com.discuss.service.RdmComDiscussService;
import incheon.res.rdm.com.discuss.vo.RdmComDiscussVO;
import incheon.res.rdm.com.perinfo.service.RdmComPerinfoService;
import incheon.res.rdm.com.perinfo.vo.RdmComPerinfo;
import incheon.res.rdm.com.perinfo.vo.RdmComPerinfoVO;
import incheon.res.rdm.dp.acceptreceiv.service.RdmDpAcceptReceivService;
import incheon.res.rdm.dp.acceptreceiv.vo.RdmDpAcceptReceivVO;
import incheon.res.rdm.dp.confirm.service.RdmDpConfirmService;
import incheon.res.rdm.dp.confirm.vo.RdmDpConfirmVO;
import incheon.res.mng.govoff.vo.RdmOeGovoffVO;
import org.egovframe.rte.fdl.property.EgovPropertyService;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${api.rps-path}")
public class RdmDpAcceptReceivController {

	@Resource
    private RdmDpAcceptReceivService rdmDpAcceptReceivService;
	@Resource
    private RdmComPerinfoService rdmComPerinfoService;
	@Resource
    private RdmComDiscussService rdmComDiscussService;
	@Resource
    private RdmComDigplanService rdmComDigplanService;
	@Resource
    private RdmComCodeService rdmComCodeService;
    @Resource
    private RdmDpConfirmService rdmDpConfirmService;
	@Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;


	/**
	 * @MethodDESC : 접수전 협의상태 리스트
	 */
	@RequestMapping(value = "/RdmDpAcceptreceivL010.do")
	public String selectPerappList(
			@ModelAttribute("loginUser") LoginVO loginVO,
			@ModelAttribute RdmDpAcceptReceivVO vo,
			@RequestParam(defaultValue = "1") int pageIndex,
			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());

		String[] arrStr ={"RDH102","RDH103"};
		vo.setInCheck("Y");
		vo.setRoadDggPrcsSttsArray(arrStr);
		vo.setMngInstCd(loginVO.getRoadMngInstCd());
		vo.setExseStpoDngCd(vo.getStdgEmdCdSe());

        List resultList = rdmDpAcceptReceivService.selectPerappList(vo);

        int totCnt = rdmDpAcceptReceivService.countPerapp(vo);
        paginationInfo.setTotalRecordCount(totCnt);

        List yearList = rdmComCodeService.getYearList("Y");
		List bjdCdeList = rdmComCodeService.selectDocdList("Y");

        model.addAttribute("resultList", resultList);
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchVo", vo);
        model.addAttribute("yearList", yearList);
        model.addAttribute("bjdCdeList", bjdCdeList);

		return "res/rdm/dp/acceptreceiv/RdmDpAcceptReceivL010";
	}
	
	@RequestMapping(value = "/RdmDpAcceptreceivU010.do")
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> updataPassDiscuss(
			@ModelAttribute RdmDpAcceptReceivVO vo
	) throws Exception{

		RdmDpConfirmVO dpConfirmVO = new RdmDpConfirmVO();
		dpConfirmVO.setRoadDggMngNo(vo.getRoadDggMngNo());
		dpConfirmVO.setRoadDggPrcsStts("RDH103");

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

		int result = rdmDpConfirmService.goBackPrevStep(dpConfirmVO);
		if (result > 0) {
			resultMap.put("result", result);
		} else {
			return ResponseEntity.badRequest().build();
		}
		return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
	}


	/**
	 * @MethodDESC : 접수전 협의상세 정보
	 */
	@RequestMapping(value = "/RdmDpAcceptreceivR010.do")
	public String selectConfer(@ModelAttribute("loginUser") LoginVO loginVO, @ModelAttribute RdmDpAcceptReceivVO vo, ModelMap model)
	  throws Exception{

		//공사개요
		RdmComPerinfoVO perInfoVO = new RdmComPerinfoVO();
		perInfoVO.setRoadDggMngNo(vo.getRoadDggMngNo());
		RdmComPerinfo perInfo = rdmComPerinfoService.selectPerinfo(perInfoVO);

		if(!loginVO.getRoadMngInstCd().equals(perInfo.getMngInstCd())){
			return "redirect:/rps/RdmDpAcceptreceivL010.do";
		}

		RdmOeGovoffVO govoffVO = new RdmOeGovoffVO();
		govoffVO.setMngInstCd(perInfo.getMngInstCd());

		String rdiIdn = vo.getRoadDggMngNo();
		//협의 정보
		RdmComDiscussVO discussVO = new RdmComDiscussVO();
		discussVO.setRoadDggMngNo(rdiIdn);
		ArrayList discussList = (ArrayList)rdmComDiscussService.selectDiscussList(discussVO);
		//굴착예정지
		RdmComDigplanVO digplanVO = new RdmComDigplanVO();
		digplanVO.setRoadDggMngNo(rdiIdn);
		ArrayList digplanList = (ArrayList)rdmComDigplanService.selectDigplanList(digplanVO);

		model.addAttribute("perInfo", perInfo);
		model.addAttribute("discussList", discussList);
		model.addAttribute("digplanList", digplanList);
		model.addAttribute("searchVo", vo);

		return "res/rdm/dp/acceptreceiv/RdmDpAcceptReceivR010";
	}

}