package cokr.xit.fims.cvlc.web;

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

import javax.annotation.Resource;

import org.apache.poi.ss.usermodel.CellStyle;
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.docs.xls.CellDef;
import cokr.xit.base.docs.xls.Format;
import cokr.xit.base.docs.xls.Style;
import cokr.xit.base.docs.xls.XLSWriter;
import cokr.xit.base.user.dao.UserMapper;
import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.cmmn.service.bean.StngBean;
import cokr.xit.fims.cmmn.xls.FormatMaker;
import cokr.xit.fims.cmmn.xls.StyleMaker;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.service.CrdnService;
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/cvlc03 로 접근할 수 있다.
 * @author leebj
 */
@Controller
@RequestMapping(name="민원관리", value=Cvlc03Controller.CLASS_URL)
public class Cvlc03Controller extends ApplicationController {

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

	public class METHOD_URL {
		public static final String
		civilComplaintManagementMain = "/010/main.do",
		getCivilComplaintList = "/010/list.do"
		;
	}

	@Resource(name="crdnService")
	private CrdnService crdnService;
	@Resource(name="crdnCvlcptService")
	private CrdnCvlcptService crdnCvlcptService;
	@Resource(name="userMapper")
	protected UserMapper userMapper;
	@Resource(name = "stngBean")
	private StngBean stngBean;

	/** 민원 관리 메인화면을 연다.
	 * @return fims/cvlc/cvlc03010-main
	 */
	@RequestMapping(name="민원 관리 메인", value=METHOD_URL.civilComplaintManagementMain)
	public ModelAndView civilComplaintManagementMain() {
		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM022","FIM026", "FIM054");

		ModelAndView mav = new ModelAndView("fims/cvlc/cvlc03010-main")
			.addObject("pageName", "cvlc03010")
			.addObject("FIM054List", commonCodes.get("FIM054"))
			.addObject("TaskListForSgg", stngBean.filterTaskSectionCodeForSgg(commonCodes.get("FIM054")));
		return addCodes(commonCodes, mav, "FIM022", "FIM026")
			.addObject("currentUserName", UserInfo.current().getName());
	}

	/**민원자료 목록을 조회한다.<br />
	 * {@link CrdnService#getCrackdownList(CrdnQuery)} 참고
	 * @param query 민원자료 목록 조회 조건
	 * @return jsonView
	 */
	@Task
	@RequestMapping(name="민원자료 목록 조회", value=METHOD_URL.getCivilComplaintList)
	public ModelAndView getCivilComplaintList(CrdnQuery query) {
		setFetchSize(query.setSggCd(currentUser().getOrgID()));

		if("xls".equals(query.getDownload())) {
			List<CellDef> cellDefs = fromJson(query.getCellDefs(), CellDef.listType());

			XLSWriter xlsx = new XLSWriter().worksheet(0);
			Format format = new Format(xlsx);
			CellStyle center = format.cellStyle(Style.CENTER);
			CellStyle dateYMD = format.yyyy_mm_dd();
			CellStyle dateDT = format.yyyy_mm_dd_hh_mm_ss();

			List<DataObject> list = crdnCvlcptService.getCivilComplaintList(query.setFetchSize(0));

			Map<String, Object> valueMap = new HashMap<String, Object>();
			valueMap.put("접수번호", format.of("CVLCPT_RCPT_NO").style(center));
			valueMap.put("목록번호", format.of("CVLCPT_LIST_NO").style(center));
			valueMap.put("등록구분", format.of("CRDN_REG_SE_NM").style(center));
			valueMap.put("신고자", format.of("CVLCPT_APLCNT_NM").style(center));
			valueMap.put("담당자", format.of("CVLCPT_PRCS_PIC_NM").style(center));
			valueMap.put("담당자전화번호", format.of("CVLCPT_PRCS_PIC_TELNO").style(center));
			valueMap.put("접수일자", FormatMaker.yyyy_mm_dd(format, "CVLCPT_RCPT_YMD").style(dateYMD));
			valueMap.put("처리기한", FormatMaker.yyyy_mm_dd_hh_mm_ss(format, "CVLCPT_PRCS_PRNMNT_DT").style(dateDT));
			valueMap.put("위반일시", FormatMaker.yyyy_mm_dd_hh_mm_ss(format, "CRDN_YMD_TM").style(dateDT));
			valueMap.put("위반내용", format.of("VLTN_ARTCL").style(center));
			valueMap.put("차량번호", format.of("VHRNO"));
			valueMap.put("사진건수", format.of("ATCH_FILE_CNT").style(center));
			valueMap.put("서손/계고사유", format.of("LEVY_EXCL_RSN_NM").style(center));
			valueMap.put("특기사항", format.of("ETC_CN"));
			CellDef.setValues(cellDefs, valueMap);

			xlsx.cell(0, 0).value("민원자료 목록").value(center).merge(0, cellDefs.size()-1);
			xlsx.cell(3, 0).rowValues(CellDef.header(cellDefs, () -> StyleMaker.headerStyle(xlsx)));
			xlsx.cell(4, 0).values(list, CellDef.values(cellDefs.stream().map(i -> {if(i.getValue() instanceof Format) {i.setField(null);}; return i;}).toList()));

			return new ModelAndView("downloadView")
				.addObject("download", xlsx.getDownloadable().setFilename("민원자료 목록.xlsx"))
				.addObject("downloadData", list)
				.addObject("dataNames", cellDefs.stream().map(CellDef::getLabel).toList());
		}

		return setPagingInfo(new ModelAndView("jsonView"),crdnCvlcptService.getCivilComplaintList(query),"");
	}
}