package com.madeu.controller.web.webgroup;

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.service.web.webgroup.WebGroupService;
import com.madeu.common.service.LogHistoryService;
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 WebGroupController{

	@Autowired
	private WebGroupService webGroupService;
	
	@Autowired
	private LogHistoryService logHistoryService;
	
	/**
	 * 조직 정보 리스트 조회 (option)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webgroup/selectListGroupOption.do")
	public ModelAndView selectListGroupOption(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebGroupController selectListGroupOption 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 = webGroupService.selectListGroupOption(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("WebGroupController selectListGroupOption END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 조직/직무/권한 설정 정보 리스트 화면으로 이동.
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webgroup/selectListGroupIntro.do")
	public String  selectListGroupIntro(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model) {
		log.debug("WebGroupController  selectListGroupIntro 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 = webGroupService.selectListGroupIntro(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("WebGroupController  selectListGroupIntro END");
		return "/web/group/groupSelectList";
	}
	
	/**
	 * 조직 정보 리스트 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webgroup/selectListGroup.do")
	public ModelAndView selectListGroup(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
			
		log.debug("WebGroupController selectListGroup 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 = webGroupService.selectListGroup(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("WebGroupController selectListGroup END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 조직 정보 상세 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webgroup/selectGroup.do")
	public ModelAndView selectGroup(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
			
		log.debug("WebGroupController selectGroup 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 = webGroupService.selectGroup(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("WebGroupController selectGroup END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**	
	 * 조직 정보 등록
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webgroup/insertGroup.do")
	public ModelAndView insertGroup(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
				
		log.debug("WebGroupController insertGroup 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 = webGroupService.insertGroup(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("WebGroupController insertGroup END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 조직 정보 수정
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webgroup/updateGroup.do")
	public ModelAndView updateGroup(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
				
		log.debug("WebGroupController updateGroup 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 = webGroupService.updateGroup(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("WebGroupController updateGroup END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	

	/**
	 * 조직 정보 삭제
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webgroup/deleteGroup.do")
	public ModelAndView deleteGroup(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
				
		log.debug("WebGroupController deleteGroup 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 = webGroupService.deleteGroup(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("WebGroupController deleteGroup END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
}
