package cokr.xit.fims.cvlc.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.code.CommonCode;
import cokr.xit.base.user.dao.UserMapper;
import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.base.FimsUser;
import cokr.xit.fims.cmmn.CmmnUtil;
import cokr.xit.fims.cmmn.service.bean.StngBean;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.cvlc.CrdnCvlcpt;
import cokr.xit.fims.cvlc.service.CrdnCvlcptService;
import cokr.xit.fims.task.Task;
import cokr.xit.foundation.UserInfo;
import cokr.xit.foundation.data.DataObject;

/**민원신고답변 문구 관리 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/cvlc/cvlc05 로 접근할 수 있다.
 * @author leebj
 */
@Controller
@RequestMapping(name="민원신고답변 문구 관리", value=Cvlc05Controller.CLASS_URL)
public class Cvlc05Controller extends ApplicationController {

	public static final String CLASS_URL = "/cvlc/cvlc05";

	public class METHOD_URL {
		public static final String
		cvlcptAnswerWordsManageMain = "/010/main.do",
		getCvlcptAnswerWordsInfo = "/010/info.do",
		saveCvlcptAnswerWordsInfo = "/010/save.do",
		removeCvlcptAnswerWordsInfo = "/010/remove.do"
		;
	}

	@Resource(name="crdnCvlcptService")
	private CrdnCvlcptService crdnCvlcptService;

	@Resource(name="userMapper")
	private UserMapper userMapper;

	@Resource(name = "stngBean")
	private StngBean stngBean;

	/** 민원신고 답변 문구 관리 메인화면을 연다.
	 * @return fims/cvlc/cvlc05010-main
	 */
	@RequestMapping(name="민원신고답변 문구 관리 메인", value=METHOD_URL.cvlcptAnswerWordsManageMain)
	public ModelAndView cvlcptAnswerWordsManageMain() {
		UserInfo user = currentUser();
		DataObject params = new DataObject()
			.set("account", user.getAccount())
			.set("orgID", user.getOrgID());
		FimsUser currentUser = userMapper.getUser(params);
		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM054");

		return new ModelAndView("fims/cvlc/cvlc05010-main")
			.addObject("pageName", "cvlc05010")
			.addObject("currentUserName", UserInfo.current().getName())
			.addObject("currentUserTelno", currentUser.getPhoneNo())
			.addObject("FIM054List", commonCodes.get("FIM054"))
			.addObject("TaskListForSgg", stngBean.filterTaskSectionCodeForSgg(commonCodes.get("FIM054")));
	}

	/** 민원신고 답변 문구 설정을 조회한다.
	 * @return fims/cvlc/cvlc05010-info
	 */
	@Task
	@RequestMapping(name="민원신고 답변 문구 설정 조회", value=METHOD_URL.getCvlcptAnswerWordsInfo)
	public ModelAndView getCvlcptAnswerWordsInfo(CrdnQuery crdnQuery) {
		crdnQuery.setSggCd(currentUser().getOrgID());
		Map<String, Object> info = crdnCvlcptService.getCvlcptAnswerWordsInfo(crdnQuery);

		DataObject basicInfo = (DataObject) info.get("basic");
		List<DataObject> levyCaseList = CmmnUtil.getDataObjectListFromMap(info,"levyCaseList");
		List<DataObject> warningCaseList = CmmnUtil.getDataObjectListFromMap(info,"warningCaseList");
		List<DataObject> nonlevyCaseList = CmmnUtil.getDataObjectListFromMap(info,"nonlevyCaseList");

		return new ModelAndView("jsonView")
			.addObject("headerCn", basicInfo == null ? "" : basicInfo.string("HEADER_CN"))
			.addObject("footerCn", basicInfo == null ? "" : basicInfo.string("FOOTER_CN"))
			.addObject("levyCaseList", levyCaseList)
			.addObject("warningCaseList", warningCaseList)
			.addObject("nonlevyCaseList", nonlevyCaseList);
	}

	/** 민원신고 답변 문구 설정을 저장한다.
	 * @param hReq 저장 요청
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	@Task
	@RequestMapping(name="민원신고 답변 문구 설정 저장", value=METHOD_URL.saveCvlcptAnswerWordsInfo)
	public ModelAndView saveCvlcptAnswerWordsInfo(HttpServletRequest hReq, CrdnCvlcpt crdnCvlcpt) {
		crdnCvlcpt.setSggCd(currentUser().getOrgID());

		String saveData = hReq.getParameter("saveData");
		boolean saved = false;
		if(saveData.equals("basic") || saveData.equals("body")) {
			String rtnMsg = crdnCvlcptService.saveAnswerWordsInfo(saveData, crdnCvlcpt);
			if(rtnMsg.contains("[S]")) {
				saved = true;
			}
		}

		return new ModelAndView("jsonView")
			.addObject("saved", saved);
	}

	/** 미부과처리건 답변 문구를 삭제한다.
	 * @param ansWordsId 답변 문구 ID
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	@Task
	@RequestMapping(name="민원신고 답변 문구 설정 삭제", value=METHOD_URL.removeCvlcptAnswerWordsInfo)
	public ModelAndView removeCvlcptAnswerWordsInfo(String ansWordsId) {
		String rtnMsg = crdnCvlcptService.removeCvlcptAnswerWordsInfo(ansWordsId);
		return new ModelAndView("jsonView")
			.addObject("saved", rtnMsg.contains("[S]"));
	}
}