package com.madeu.controller.web.webvacation;

import java.util.HashMap;

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.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.madeu.constants.Constants;
import com.madeu.common.service.LogHistoryService;
import com.madeu.service.web.webvacation.WebVacationService;
import com.madeu.util.HttpUtil;

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


@Slf4j
@Controller
public class WebVacationController{

	@Autowired
	private WebVacationService webVacationService;
	
	@Autowired
	private LogHistoryService logHistoryService;
	
	/**
	 * 연차설정 정보 리스트 조회 (option)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webvacation/selectListVacationOption.do")
	public ModelAndView selectListVacationOption(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebVacationController selectListVacationOption START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webVacationService.selectListVacationOption(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("WebVacationController selectListVacationOption END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 연차설정 정보 리스트 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webvacation/selectListVacation.do")
	public ModelAndView selectListVacation(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebVacationController selectListVacation START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webVacationService.selectListVacation(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("WebVacationController selectListVacation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 연차설정 정보 상세 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webvacation/selectVacation.do")
	public ModelAndView selectVacation(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebVacationController selectVacation START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webVacationService.selectVacation(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("WebVacationController selectVacation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**	
	 * 연차설정 정보 등록
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webvacation/insertVacation.do")
	public ModelAndView insertVacation(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebVacationController insertVacation START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			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 = webVacationService.insertVacation(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","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}

						logHistoryService.save("연차설정 등록", "연차설정 등록", paramMap, map, String.valueOf(errorMsg));
		}
		log.debug("WebVacationController insertVacation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 연차설정 정보 수정
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webvacation/updateVacation.do")
	public ModelAndView updateVacation(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebVacationController updateVacation START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();
		
		try{
			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 = webVacationService.updateVacation(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","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
			
						logHistoryService.save("연차설정 수정", "연차설정 수정", paramMap, map, String.valueOf(errorMsg));
		}
		log.debug("WebVacationController updateVacation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	

	/**
	 * 연차설정 정보 삭제
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webvacation/deleteVacation.do")
	public ModelAndView deleteVacation(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebVacationController deleteVacation START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();
		
		try{
			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 = webVacationService.deleteVacation(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","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
			
						logHistoryService.save("연차설정 삭제", "연차설정 삭제", paramMap, map, String.valueOf(errorMsg));
		}
		log.debug("WebVacationController deleteVacation END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
}
