package com.madeu.controller.web.webwork;

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.webwork.WebWorkService;
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 WebWorkController{

	@Autowired
	private WebWorkService webWorkService;
	
	@Autowired
	private LogHistoryService logHistoryService;
	
	/**
	 * 근무/연차 설정 정보 리스트 화면으로 이동.
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webwork/selectListWorkIntro.do")
	public String  selectListWorkIntro(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model) {
		log.debug("WebWorkController  selectListWorkIntro 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 = webWorkService.selectListWorkIntro(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("WebWorkController  selectListWorkIntro END");
		return "/web/work/workSelectList";
	}
	
	/**
	 * 근무설정 정보 리스트 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webwork/selectListWork.do")
	public ModelAndView selectListWork(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebWorkController selectListWork 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 = webWorkService.selectListWork(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("WebWorkController selectListWork END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 근무설정 정보 상세 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webwork/selectWork.do")
	public ModelAndView selectWork(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebWorkController selectWork 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 = webWorkService.selectWork(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("WebWorkController selectWork END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**	
	 * 근무설정 정보 등록
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webwork/insertWork.do")
	public ModelAndView insertWork(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebWorkController insertWork 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 = webWorkService.insertWork(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("WebWorkController insertWork END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 근무설정 정보 수정
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webwork/updateWork.do")
	public ModelAndView updateWork(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
		log.debug("WebWorkController updateWork 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 = webWorkService.updateWork(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("WebWorkController updateWork END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	

	/**
	 * 근무설정 정보 삭제
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webwork/deleteWork.do")
	public ModelAndView deleteWork(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebWorkController deleteWork 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 = webWorkService.deleteWork(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("WebWorkController deleteWork END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
}
