package com.madeu.controller.web.webattendance;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.madeu.constants.Constants;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.web.webattendance.WebAttendanceService;
import com.madeu.service.web.webloghistory.WebLogHistoryService;
import com.madeu.util.HttpUtil;
import com.madeu.util.RequestLogUtil;
import com.madeu.view.web.WebAttendanceView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Controller
public class WebAttendanceController extends ManagerDraftAction{

	@Autowired
	private WebAttendanceService webAttendanceService;
	
	@Autowired
	private WebLogHistoryService webLogHistoryService;
	
	/**
	 * 근태/연차 정보 리스트 화면으로 이동.
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/selectListAttendanceIntro.do")
	public String  selectListAttendanceIntro(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model) {
		log.debug("WebAttendanceController  selectListAttendanceIntro START");

		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			if(!webCheckLogin(session)){
				return "/web/login/logout";
			}
			else {
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.selectListAttendanceIntro(paramMap);
				
				model.addAttribute("selectUseYn", map.get("selectUseYn"));
				model.addAttribute("insertUseYn", map.get("insertUseYn"));
				model.addAttribute("updateUseYn", map.get("updateUseYn"));
				model.addAttribute("deleteUseYn", map.get("deleteUseYn"));
				model.addAttribute("downloadUseYn", map.get("downloadUseYn"));
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return "/web/login/logout";
		}
		log.debug("WebAttendanceController  selectListAttendanceIntro END");
		return "/web/attendance/attendanceSelectList";
	}
	
	/**
	 * 근태관리 정보 리스트 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/selectListAttendance.do")
	public ModelAndView selectListAttendance(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebAttendanceController selectListAttendance START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.selectListAttendance(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {
				
			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
		}
		log.debug("WebAttendanceController selectListAttendance END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * <b>근태관리 정보 엑셀 다운로드 </b>
	 * 페이지 처리 없음 
	 * request: 
	 * response : msgCode, msgDesc
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/downloadAttendanceExcel.do")
	public ModelAndView downloadAttendanceExcel(HttpSession session, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file, Model model) {
		log.debug("WebAttendanceController downloadAttendanceExcel START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		Map<String, Object> selectListReport = new HashMap<String,Object>();
		Map<String, Object> map = new HashMap<String,Object>();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				selectListReport = webAttendanceService.selectListAttendanceExcel(paramMap);
				map.put("selectListAttendance", selectListReport);
				map.put("name", "attendance");
			}
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		log.debug("WebAttendanceController downloadAttendanceExcel END");
		return new ModelAndView(new WebAttendanceView(),"map",map);
	}
	

	
	/**
	 * 금앨 근무현황 리스트 정보 조회 (List)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/selectListTodayAttendance.do")
	public ModelAndView selectListTodayAttendance(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebAttendanceController selectListTodayAttendance START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.selectListTodayAttendance(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
		}
		log.debug("WebAttendanceController selectListTodayAttendance END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**	
	 * 근태관리 정보 등록
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/insertAttendance.do")
	public ModelAndView insertAttendance(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebAttendanceController insertAttendance START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.insertAttendance(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}

			try {
				HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
				HashMap<String, Object> insertMap = new HashMap<String, Object>();
	
				insertMap.put("url", "/webattendance/insertAttendance.do");
				insertMap.put("func", "insertAttendance");
				insertMap.put("funcName", "근태관리 등록");
				insertMap.put("service", "webAttendanceService");
				insertMap.put("serviceName", "근태관리 등록");
				insertMap.put("requestValue", String.valueOf(paramMap));
				insertMap.put("responseValue", String.valueOf(map));
				insertMap.put("tId", map.get("tId"));
				if((String.valueOf(errorMsg)).equals("") || (String.valueOf(errorMsg) == null) || String.valueOf(errorMsg).length() == 0){
					insertMap.put("resultCode", "SUCCESS");
				}
				else{
					insertMap.put("resultCode", "ERROR");
				}
				insertMap.put("resultMsg", String.valueOf(errorMsg));
				insertMap.put("muMemberId", paramMap.get("loginMemberId"));
	
				webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		log.debug("WebAttendanceController insertAttendance END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 근태관리 상세 정보 조회 (출/퇴근 조회)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/selectAttendanceCommute.do")
	public ModelAndView selectAttendanceCommute(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
			
		log.debug("WebAttendanceController selectAttendanceCommute START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.selectAttendanceCommute(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
		}
		log.debug("WebAttendanceController selectAttendanceCommute END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**	
	 * 근태관리 수정 (출/퇴근)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/updateAttendanceCommute.do")
	public ModelAndView updateAttendanceCommute(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebAttendanceController updateAttendanceCommute START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.updateAttendanceCommute(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}

			try {
				HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
				HashMap<String, Object> insertMap = new HashMap<String, Object>();
	
				insertMap.put("url", "/webattendance/updateAttendanceCommute.do");
				insertMap.put("func", "updateAttendanceCommute");
				insertMap.put("funcName", "근태관리 수정 (출/퇴근)");
				insertMap.put("service", "webAttendanceService");
				insertMap.put("serviceName", "근태관리 수정 (출/퇴근)");
				insertMap.put("requestValue", String.valueOf(paramMap));
				insertMap.put("responseValue", String.valueOf(map));
				insertMap.put("tId", map.get("tId"));
				if((String.valueOf(errorMsg)).equals("") || (String.valueOf(errorMsg) == null) || String.valueOf(errorMsg).length() == 0){
					insertMap.put("resultCode", "SUCCESS");
				}
				else{
					insertMap.put("resultCode", "ERROR");
				}
				insertMap.put("resultMsg", String.valueOf(errorMsg));
				insertMap.put("muMemberId", paramMap.get("loginMemberId"));
	
				webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		log.debug("WebAttendanceController updateAttendanceCommute END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 근태관리 상세 정보 조회 (연차 조회)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/selectAttendanceVacation.do")
	public ModelAndView selectAttendanceVacation(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebAttendanceController selectAttendanceVacation START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.selectAttendanceVacation(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
		}
		log.debug("WebAttendanceController selectAttendanceVacation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**	
	 * 근태관리 등록 (연차)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/insertAttendanceVacation.do")
	public ModelAndView insertAttendanceVacation(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebAttendanceController insertAttendanceVacation START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.insertAttendanceVacation(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}

			try {
				HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
				HashMap<String, Object> insertMap = new HashMap<String, Object>();
	
				insertMap.put("url", "/webattendance/insertAttendanceVacation.do");
				insertMap.put("func", "insertAttendanceVacation");
				insertMap.put("funcName", "근태관리 등록 (연차)");
				insertMap.put("service", "webAttendanceService");
				insertMap.put("serviceName", "근태관리 등록 (연차)");
				insertMap.put("requestValue", String.valueOf(paramMap));
				insertMap.put("responseValue", String.valueOf(map));
				insertMap.put("tId", map.get("tId"));
				if((String.valueOf(errorMsg)).equals("") || (String.valueOf(errorMsg) == null) || String.valueOf(errorMsg).length() == 0){
					insertMap.put("resultCode", "SUCCESS");
				}
				else{
					insertMap.put("resultCode", "ERROR");
				}
				insertMap.put("resultMsg", String.valueOf(errorMsg));
				insertMap.put("muMemberId", paramMap.get("loginMemberId"));
	
				webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		log.debug("WebAttendanceController insertAttendanceVacation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**	
	 * 근태관리 수정 (연차)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/updateAttendanceVacation.do")
	public ModelAndView updateAttendanceVacation(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebAttendanceController updateAttendanceVacation START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.updateAttendanceVacation(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}

			try {
				HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
				HashMap<String, Object> insertMap = new HashMap<String, Object>();
	
				insertMap.put("url", "/webattendance/updateAttendanceVacation.do");
				insertMap.put("func", "updateAttendanceVacation");
				insertMap.put("funcName", "근태관리 수정 (연차)");
				insertMap.put("service", "webAttendanceService");
				insertMap.put("serviceName", "근태관리 수정 (연차)");
				insertMap.put("requestValue", String.valueOf(paramMap));
				insertMap.put("responseValue", String.valueOf(map));
				insertMap.put("tId", map.get("tId"));
				if((String.valueOf(errorMsg)).equals("") || (String.valueOf(errorMsg) == null) || String.valueOf(errorMsg).length() == 0){
					insertMap.put("resultCode", "SUCCESS");
				}
				else{
					insertMap.put("resultCode", "ERROR");
				}
				insertMap.put("resultMsg", String.valueOf(errorMsg));
				insertMap.put("muMemberId", paramMap.get("loginMemberId"));
	
				webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		log.debug("WebAttendanceController updateAttendanceVacation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**	
	 * 근태관리 취소 (연차)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webattendance/updateAttendanceVacationCancel.do")
	public ModelAndView updateAttendanceVacationCancel(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebAttendanceController updateAttendanceVacationCancel START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			if(!webCheckLogin(session)){
				return null;
			}
			else{
				paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
				paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
				map = webAttendanceService.updateAttendanceVacationCancel(paramMap);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		finally {
			if(Constants.OK == map.get("msgCode")) {

			}
			else{
				map.put("msgCode", Constants.FAIL);
				map.put("success", false);
				if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
					map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}

			try {
				HashMap<String, Object> visitLogParamMap = RequestLogUtil.getVisitLogParameterMap(request);
				HashMap<String, Object> insertMap = new HashMap<String, Object>();
	
				insertMap.put("url", "/webattendance/updateAttendanceVacationCancel.do");
				insertMap.put("func", "updateAttendanceVacationCancel");
				insertMap.put("funcName", "근태관리 취소 (연차)");
				insertMap.put("service", "webAttendanceService");
				insertMap.put("serviceName", "근태관리 취소 (연차)");
				insertMap.put("requestValue", String.valueOf(paramMap));
				insertMap.put("responseValue", String.valueOf(map));
				insertMap.put("tId", map.get("tId"));
				if((String.valueOf(errorMsg)).equals("") || (String.valueOf(errorMsg) == null) || String.valueOf(errorMsg).length() == 0){
					insertMap.put("resultCode", "SUCCESS");
				}
				else{
					insertMap.put("resultCode", "ERROR");
				}
				insertMap.put("resultMsg", String.valueOf(errorMsg));
				insertMap.put("muMemberId", paramMap.get("loginMemberId"));
	
				webLogHistoryService.insertLogHistory(insertMap, visitLogParamMap);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		log.debug("WebAttendanceController updateAttendanceVacationCancel END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
}
